summaryrefslogtreecommitdiffstats
path: root/commands/hugo.go
diff options
context:
space:
mode:
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2017-01-03 17:28:51 +0100
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2017-01-07 17:06:35 +0100
commit45e3ed517a17648d54e8ce33618a8f251cfec603 (patch)
tree160a1bbd03a107be08ae76018fb382268365e2a1 /commands/hugo.go
parent24a286791f37cbf6638b37f29386949045d0bba2 (diff)
all: Refactor to non-global logger
Note that this looks like overkill for just the logger, and that is correct, but this will make sense once we start with the template handling etc. Updates #2701
Diffstat (limited to 'commands/hugo.go')
-rw-r--r--commands/hugo.go127
1 files changed, 71 insertions, 56 deletions
diff --git a/commands/hugo.go b/commands/hugo.go
index 9347a5081..f4204cad1 100644
--- a/commands/hugo.go
+++ b/commands/hugo.go
@@ -18,6 +18,7 @@ package commands
import (
"fmt"
"io/ioutil"
+ "log"
"net/http"
"os"
"path/filepath"
@@ -113,16 +114,17 @@ built with love by spf13 and friends in Go.
Complete documentation is available at http://gohugo.io/.`,
RunE: func(cmd *cobra.Command, args []string) error {
- if err := InitializeConfig(); err != nil {
+ cfg, err := InitializeConfig()
+ if err != nil {
return err
}
if buildWatch {
viper.Set("disableLiveReload", true)
- watchConfig()
+ watchConfig(cfg)
}
- return build()
+ return build(cfg)
},
}
@@ -266,9 +268,12 @@ func init() {
}
// InitializeConfig initializes a config file with sensible default configuration flags.
-func InitializeConfig(subCmdVs ...*cobra.Command) error {
+func InitializeConfig(subCmdVs ...*cobra.Command) (hugolib.DepsCfg, error) {
+
+ var cfg hugolib.DepsCfg
+
if err := hugolib.LoadGlobalConfig(source, cfgFile); err != nil {
- return err
+ return cfg, err
}
for _, cmdV := range append([]*cobra.Command{hugoCmdV}, subCmdVs...) {
@@ -333,37 +338,6 @@ func InitializeConfig(subCmdVs ...*cobra.Command) error {
viper.Set("cacheDir", helpers.GetTempDir("hugo_cache", hugofs.Source()))
}
- logFile := ioutil.Discard
-
- if verboseLog || logging || (viper.IsSet("logFile") && viper.GetString("logFile") != "") {
-
- var err error
- if viper.IsSet("logFile") && viper.GetString("logFile") != "" {
- path := viper.GetString("logFile")
- logFile, err = os.OpenFile(path, os.O_RDWR|os.O_APPEND|os.O_CREATE, 0666)
- if err != nil {
- return newSystemError("Failed to open log file:", path, err)
- }
- } else {
- logFile, err = ioutil.TempFile(os.TempDir(), "hugo")
- if err != nil {
- return newSystemError(err)
- }
- }
- }
-
- jww.SetLogOutput(logFile)
-
- if quiet {
- jww.SetStdoutThreshold(jww.LevelError)
- } else if viper.GetBool("verbose") {
- jww.SetStdoutThreshold(jww.LevelInfo)
- }
-
- if verboseLog {
- jww.SetLogThreshold(jww.LevelInfo)
- }
-
jww.INFO.Println("Using config file:", viper.ConfigFileUsed())
// Init file systems. This may be changed at a later point.
@@ -372,7 +346,7 @@ func InitializeConfig(subCmdVs ...*cobra.Command) error {
themeDir := helpers.GetThemeDir()
if themeDir != "" {
if _, err := hugofs.Source().Stat(themeDir); os.IsNotExist(err) {
- return newSystemError("Unable to find theme Directory:", themeDir)
+ return cfg, newSystemError("Unable to find theme Directory:", themeDir)
}
}
@@ -383,8 +357,49 @@ func InitializeConfig(subCmdVs ...*cobra.Command) error {
helpers.HugoReleaseVersion(), minVersion)
}
- return nil
+ logger, err := createLogger()
+ if err != nil {
+ return cfg, err
+ }
+
+ cfg.Logger = logger
+
+ return cfg, nil
+
+}
+
+func createLogger() (*jww.Notepad, error) {
+ var (
+ logHandle = ioutil.Discard
+ outHandle = os.Stdout
+ stdoutThreshold = jww.LevelError
+ logThreshold = jww.LevelWarn
+ )
+
+ if verboseLog || logging || (viper.IsSet("logFile") && viper.GetString("logFile") != "") {
+
+ var err error
+ if viper.IsSet("logFile") && viper.GetString("logFile") != "" {
+ path := viper.GetString("logFile")
+ logHandle, err = os.OpenFile(path, os.O_RDWR|os.O_APPEND|os.O_CREATE, 0666)
+ if err != nil {
+ return nil, newSystemError("Failed to open log file:", path, err)
+ }
+ } else {
+ logHandle, err = ioutil.TempFile(os.TempDir(), "hugo")
+ if err != nil {
+ return nil, newSystemError(err)
+ }
+ }
+ } else if !quiet && viper.GetBool("verbose") {
+ stdoutThreshold = jww.LevelInfo
+ }
+
+ if verboseLog {
+ logThreshold = jww.LevelInfo
+ }
+ return jww.NewNotepad(stdoutThreshold, logThreshold, outHandle, logHandle, "", log.Ldate|log.Ltime), nil
}
func initializeFlags(cmd *cobra.Command) {
@@ -432,12 +447,12 @@ func flagChanged(flags *flag.FlagSet, key string) bool {
return flag.Changed
}
-func watchConfig() {
+func watchConfig(cfg hugolib.DepsCfg) {
viper.WatchConfig()
viper.OnConfigChange(func(e fsnotify.Event) {
jww.FEEDBACK.Println("Config file changed:", e.Name)
// Force a full rebuild
- utils.CheckErr(recreateAndBuildSites(true))
+ utils.CheckErr(recreateAndBuildSites(cfg, true))
if !viper.GetBool("disableLiveReload") {
// Will block forever trying to write to a channel that nobody is reading if livereload isn't initialized
livereload.ForceRefresh()
@@ -445,7 +460,7 @@ func watchConfig() {
})
}
-func build(watches ...bool) error {
+func build(cfg hugolib.DepsCfg, watches ...bool) error {
// Hugo writes the output to memory instead of the disk.
// This is only used for benchmark testing. Cause the content is only visible
// in memory.
@@ -462,14 +477,14 @@ func build(watches ...bool) error {
if len(watches) > 0 && watches[0] {
watch = true
}
- if err := buildSites(buildWatch || watch); err != nil {
+ if err := buildSites(cfg, buildWatch || watch); err != nil {
return fmt.Errorf("Error building site: %s", err)
}
if buildWatch {
jww.FEEDBACK.Println("Watching for changes in", helpers.AbsPathify(viper.GetString("contentDir")))
jww.FEEDBACK.Println("Press Ctrl+C to stop")
- utils.CheckErr(NewWatcher(0))
+ utils.CheckErr(newWatcher(cfg, 0))
}
return nil
@@ -644,8 +659,8 @@ func getDirList() []string {
return a
}
-func recreateAndBuildSites(watching bool) (err error) {
- if err := initSites(); err != nil {
+func recreateAndBuildSites(cfg hugolib.DepsCfg, watching bool) (err error) {
+ if err := initSites(cfg); err != nil {
return err
}
if !quiet {
@@ -654,8 +669,8 @@ func recreateAndBuildSites(watching bool) (err error) {
return Hugo.Build(hugolib.BuildCfg{CreateSitesFromConfig: true, Watching: watching, PrintStats: !quiet})
}
-func resetAndBuildSites(watching bool) (err error) {
- if err := initSites(); err != nil {
+func resetAndBuildSites(cfg hugolib.DepsCfg, watching bool) (err error) {
+ if err := initSites(cfg); err != nil {
return err
}
if !quiet {
@@ -664,12 +679,12 @@ func resetAndBuildSites(watching bool) (err error) {
return Hugo.Build(hugolib.BuildCfg{ResetState: true, Watching: watching, PrintStats: !quiet})
}
-func initSites() error {
+func initSites(cfg hugolib.DepsCfg) error {
if Hugo != nil {
return nil
}
- h, err := hugolib.NewHugoSitesFromConfiguration()
+ h, err := hugolib.NewHugoSitesFromConfiguration(cfg)
if err != nil {
return err
@@ -679,8 +694,8 @@ func initSites() error {
return nil
}
-func buildSites(watching bool) (err error) {
- if err := initSites(); err != nil {
+func buildSites(cfg hugolib.DepsCfg, watching bool) (err error) {
+ if err := initSites(cfg); err != nil {
return err
}
if !quiet {
@@ -689,15 +704,15 @@ func buildSites(watching bool) (err error) {
return Hugo.Build(hugolib.BuildCfg{Watching: watching, PrintStats: !quiet})
}
-func rebuildSites(events []fsnotify.Event) error {
- if err := initSites(); err != nil {
+func rebuildSites(cfg hugolib.DepsCfg, events []fsnotify.Event) error {
+ if err := initSites(cfg); err != nil {
return err
}
return Hugo.Build(hugolib.BuildCfg{PrintStats: !quiet, Watching: true}, events...)
}
-// NewWatcher creates a new watcher to watch filesystem events.
-func NewWatcher(port int) error {
+// newWatcher creates a new watcher to watch filesystem events.
+func newWatcher(cfg hugolib.DepsCfg, port int) error {
if runtime.GOOS == "darwin" {
tweakLimit()
}
@@ -910,7 +925,7 @@ func NewWatcher(port int) error {
const layout = "2006-01-02 15:04 -0700"
jww.FEEDBACK.Println(time.Now().Format(layout))
- rebuildSites(dynamicEvents)
+ rebuildSites(cfg, dynamicEvents)
if !buildWatch && !viper.GetBool("disableLiveReload") {
// Will block forever trying to write to a channel that nobody is reading if livereload isn't initialized