Permalink
Browse files

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
  • Loading branch information...
1 parent 24a2867 commit 45e3ed517a17648d54e8ce33618a8f251cfec603 @bep bep committed Jan 3, 2017
@@ -48,8 +48,8 @@ func init() {
}
func benchmark(cmd *cobra.Command, args []string) error {
- var err error
- if err = InitializeConfig(benchmarkCmd); err != nil {
+ cfg, err := InitializeConfig(benchmarkCmd)
+ if err != nil {
return err
}
@@ -79,7 +79,7 @@ func benchmark(cmd *cobra.Command, args []string) error {
t := time.Now()
for i := 0; i < benchmarkTimes; i++ {
- if err = resetAndBuildSites(false); err != nil {
+ if err = resetAndBuildSites(cfg, false); err != nil {
return err
}
}
View
@@ -80,13 +80,13 @@ func init() {
convertCmd.PersistentFlags().SetAnnotation("source", cobra.BashCompSubdirsInDir, []string{})
}
-func convertContents(mark rune) (err error) {
- if err := InitializeConfig(); err != nil {
+func convertContents(mark rune) error {
+ cfg, err := InitializeConfig()
+ if err != nil {
return err
}
- h, err := hugolib.NewHugoSitesFromConfiguration()
-
+ h, err := hugolib.NewHugoSitesFromConfiguration(cfg)
if err != nil {
return err
}
@@ -108,7 +108,7 @@ func convertContents(mark rune) (err error) {
jww.FEEDBACK.Println("processing", len(site.Source.Files()), "content files")
for _, file := range site.Source.Files() {
jww.INFO.Println("Attempting to convert", file.LogicalName())
- page, err := hugolib.NewPage(file.LogicalName())
+ page, err := site.NewPage(file.LogicalName())
if err != nil {
return err
}
@@ -157,5 +157,5 @@ func convertContents(mark rune) (err error) {
}
}
}
- return
+ return nil
}
View
@@ -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,20 +447,20 @@ 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()
}
})
}
-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
@@ -83,8 +83,9 @@ func importFromJekyll(cmd *cobra.Command, args []string) error {
}
forceImport, _ := cmd.Flags().GetBool("force")
- if err := createSiteFromJekyll(jekyllRoot, targetDir, forceImport); err != nil {
- return newUserError(err)
+ site, err := createSiteFromJekyll(jekyllRoot, targetDir, forceImport)
+ if err != nil {
+ return err
}
jww.FEEDBACK.Println("Importing...")
@@ -118,7 +119,7 @@ func importFromJekyll(cmd *cobra.Command, args []string) error {
}
fileCount++
- return convertJekyllPost(path, relPath, targetDir, draft)
+ return convertJekyllPost(site, path, relPath, targetDir, draft)
}
err = helpers.SymbolicWalk(hugofs.Os(), jekyllRoot, callback)
@@ -135,17 +136,17 @@ func importFromJekyll(cmd *cobra.Command, args []string) error {
}
// TODO: Consider calling doNewSite() instead?
-func createSiteFromJekyll(jekyllRoot, targetDir string, force bool) error {
+func createSiteFromJekyll(jekyllRoot, targetDir string, force bool) (*hugolib.Site, error) {
fs := hugofs.Source()
if exists, _ := helpers.Exists(targetDir, fs); exists {
if isDir, _ := helpers.IsDir(targetDir, fs); !isDir {
- return errors.New("Target path \"" + targetDir + "\" already exists but not a directory")
+ return nil, errors.New("Target path \"" + targetDir + "\" already exists but not a directory")
}
isEmpty, _ := helpers.IsEmpty(targetDir, fs)
if !isEmpty && !force {
- return errors.New("Target path \"" + targetDir + "\" already exists and is not empty")
+ return nil, errors.New("Target path \"" + targetDir + "\" already exists and is not empty")
}
}
@@ -166,7 +167,7 @@ func createSiteFromJekyll(jekyllRoot, targetDir string, force bool) error {
}
}
if !hasPostsOrDrafts {
- return errors.New("Your Jekyll root contains neither posts nor drafts, aborting.")
+ return nil, errors.New("Your Jekyll root contains neither posts nor drafts, aborting.")
}
mkdir(targetDir, "layouts")
@@ -179,8 +180,9 @@ func createSiteFromJekyll(jekyllRoot, targetDir string, force bool) error {
createConfigFromJekyll(targetDir, "yaml", jekyllConfig)
copyJekyllFilesAndFolders(jekyllRoot, filepath.Join(targetDir, "static"))
+ site := hugolib.NewSiteDefaultLang()
- return nil
+ return site, nil
}
func loadJekyllConfig(jekyllRoot string) map[string]interface{} {
@@ -379,7 +381,7 @@ func parseJekyllFilename(filename string) (time.Time, string, error) {
return postDate, postName, nil
}
-func convertJekyllPost(path, relPath, targetDir string, draft bool) error {
+func convertJekyllPost(s *hugolib.Site, path, relPath, targetDir string, draft bool) error {
jww.TRACE.Println("Converting", path)
filename := filepath.Base(path)
@@ -422,7 +424,7 @@ func convertJekyllPost(path, relPath, targetDir string, draft bool) error {
jww.TRACE.Println(newmetadata)
content := convertJekyllContent(newmetadata, string(psr.Content()))
- page, err := hugolib.NewPage(filename)
+ page, err := s.NewPage(filename)
if err != nil {
jww.ERROR.Println("New page error", filename)
return err
Oops, something went wrong.

0 comments on commit 45e3ed5

Please sign in to comment.