simplified buildSite & better error handling around it

This commit is contained in:
spf13 2013-10-09 19:06:47 -04:00
parent 0318f7c149
commit f5fda80486
4 changed files with 12 additions and 14 deletions

View file

@ -46,6 +46,6 @@ func bench(cmd *cobra.Command, args []string) {
defer pprof.StopCPUProfile() defer pprof.StopCPUProfile()
for i := 0; i < benchmarkTimes; i++ { for i := 0; i < benchmarkTimes; i++ {
_, _ = buildSite() _ = buildSite()
} }
} }

View file

@ -49,7 +49,7 @@ var Source, Destination, BaseUrl, CfgFile string
func Execute() { func Execute() {
AddCommands() AddCommands()
Hugo := HugoCmd.ToCommander() Hugo := HugoCmd.ToCommander()
utils.CheckErrExit(Hugo.Execute()) utils.StopOnErr(Hugo.Execute())
} }
func AddCommands() { func AddCommands() {
@ -86,9 +86,7 @@ func InitializeConfig() {
func build() { func build() {
utils.CheckErr(copyStatic(), fmt.Sprintf("Error copying static files to %s", Config.GetAbsPath(Config.PublishDir))) utils.CheckErr(copyStatic(), fmt.Sprintf("Error copying static files to %s", Config.GetAbsPath(Config.PublishDir)))
utils.StopOnErr(buildSite())
_, e := buildSite()
utils.CheckErrExit(e)
if BuildWatch { if BuildWatch {
fmt.Println("Watching for changes in", Config.GetAbsPath(Config.ContentDir)) fmt.Println("Watching for changes in", Config.GetAbsPath(Config.ContentDir))
@ -123,16 +121,16 @@ func getDirList() []string {
return a return a
} }
func buildSite() (site *hugolib.Site, err error) { func buildSite() (err error) {
startTime := time.Now() startTime := time.Now()
site = &hugolib.Site{Config: *Config} site := &hugolib.Site{Config: *Config}
err = site.Build() err = site.Build()
if err != nil { if err != nil {
return return
} }
site.Stats() site.Stats()
fmt.Printf("in %v ms\n", int(1000*time.Since(startTime).Seconds())) fmt.Printf("in %v ms\n", int(1000*time.Since(startTime).Seconds()))
return site, nil return nil
} }
func NewWatcher(port int) error { func NewWatcher(port int) error {
@ -181,9 +179,9 @@ func NewWatcher(port int) error {
func watchChange(ev *fsnotify.FileEvent) { func watchChange(ev *fsnotify.FileEvent) {
if strings.HasPrefix(ev.Name, Config.GetAbsPath(Config.StaticDir)) { if strings.HasPrefix(ev.Name, Config.GetAbsPath(Config.StaticDir)) {
fmt.Println("Static file changed, syncing\n") fmt.Println("Static file changed, syncing\n")
copyStatic() utils.CheckErr(copyStatic(), fmt.Sprintf("Error copying static files to %s", Config.GetAbsPath(Config.PublishDir)))
} else { } else {
fmt.Println("Change detected, rebuilding site\n") fmt.Println("Change detected, rebuilding site\n")
buildSite() utils.StopOnErr(buildSite())
} }
} }

View file

@ -19,9 +19,9 @@ import (
) )
type Node struct { type Node struct {
RSSlink template.HTML RSSlink template.HTML
Site SiteInfo Site SiteInfo
// layout string // layout string
Data map[string]interface{} Data map[string]interface{}
Title string Title string
Description string Description string

View file

@ -14,7 +14,7 @@ func CheckErr(err error, s ...string) {
} }
} }
func CheckErrExit(err error, s ...string) { func StopOnErr(err error, s ...string) {
if err != nil { if err != nil {
CheckErr(err, s...) CheckErr(err, s...)
os.Exit(-1) os.Exit(-1)