diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2017-02-05 10:20:06 +0700 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2017-02-17 17:15:26 +0100 |
commit | 93ca7c9e958e34469a337e4efcc7c75774ec50fd (patch) | |
tree | 5dfa296cfe74fd5ef8f0d41ea4078704f453aa04 /commands | |
parent | e34af6ee30f70f5780a281e2fd8f4ed9b487ee61 (diff) |
all: Refactor to nonglobal Viper, i18n etc.
This is a final rewrite that removes all the global state in Hugo, which also enables
the use if `t.Parallel` in tests.
Updates #2701
Fixes #3016
Diffstat (limited to 'commands')
-rw-r--r-- | commands/benchmark.go | 2 | ||||
-rw-r--r-- | commands/commandeer.go | 46 | ||||
-rw-r--r-- | commands/convert.go | 19 | ||||
-rw-r--r-- | commands/hugo.go | 326 | ||||
-rw-r--r-- | commands/list.go | 19 | ||||
-rw-r--r-- | commands/list_config.go | 10 | ||||
-rw-r--r-- | commands/new.go | 19 | ||||
-rw-r--r-- | commands/new_test.go | 24 | ||||
-rw-r--r-- | commands/server.go | 38 | ||||
-rw-r--r-- | commands/server_test.go | 8 |
10 files changed, 286 insertions, 225 deletions
diff --git a/commands/benchmark.go b/commands/benchmark.go index 42966c67a..4f9ab828b 100644 --- a/commands/benchmark.go +++ b/commands/benchmark.go @@ -54,7 +54,7 @@ func benchmark(cmd *cobra.Command, args []string) error { return err } - c := commandeer{cfg} + c := newCommandeer(cfg) var memProf *os.File if memProfileFile != "" { diff --git a/commands/commandeer.go b/commands/commandeer.go new file mode 100644 index 000000000..e7fd70651 --- /dev/null +++ b/commands/commandeer.go @@ -0,0 +1,46 @@ +// Copyright 2017 The Hugo Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package commands + +import ( + "github.com/spf13/hugo/deps" + "github.com/spf13/hugo/helpers" +) + +type commandeer struct { + *deps.DepsCfg + pathSpec *helpers.PathSpec + configured bool +} + +func (c *commandeer) Set(key string, value interface{}) { + if c.configured { + panic("commandeer cannot be changed") + } + c.Cfg.Set(key, value) +} + +// PathSpec lazily creates a new PathSpec, as all the paths must +// be configured before it is created. +func (c *commandeer) PathSpec() *helpers.PathSpec { + c.configured = true + if c.pathSpec == nil { + c.pathSpec = helpers.NewPathSpec(c.Fs, c.Cfg) + } + return c.pathSpec +} + +func newCommandeer(cfg *deps.DepsCfg) *commandeer { + return &commandeer{DepsCfg: cfg} +} diff --git a/commands/convert.go b/commands/convert.go index 79c3c75e8..c24282550 100644 --- a/commands/convert.go +++ b/commands/convert.go @@ -21,11 +21,8 @@ import ( "github.com/spf13/cast" "github.com/spf13/cobra" - "github.com/spf13/hugo/helpers" "github.com/spf13/hugo/hugolib" "github.com/spf13/hugo/parser" - jww "github.com/spf13/jwalterweatherman" - "github.com/spf13/viper" ) var outputDir string @@ -86,7 +83,7 @@ func convertContents(mark rune) error { return err } - h, err := hugolib.NewHugoSitesFromConfiguration(cfg) + h, err := hugolib.NewHugoSites(*cfg) if err != nil { return err } @@ -104,10 +101,10 @@ func convertContents(mark rune) error { return errors.New("No source files found") } - contentDir := helpers.AbsPathify(viper.GetString("contentDir")) - jww.FEEDBACK.Println("processing", len(site.Source.Files()), "content files") + contentDir := site.PathSpec.AbsPathify(site.Cfg.GetString("contentDir")) + site.Log.FEEDBACK.Println("processing", len(site.Source.Files()), "content files") for _, file := range site.Source.Files() { - jww.INFO.Println("Attempting to convert", file.LogicalName()) + site.Log.INFO.Println("Attempting to convert", file.LogicalName()) page, err := site.NewPage(file.LogicalName()) if err != nil { return err @@ -115,12 +112,12 @@ func convertContents(mark rune) error { psr, err := parser.ReadFrom(file.Contents) if err != nil { - jww.ERROR.Println("Error processing file:", file.Path()) + site.Log.ERROR.Println("Error processing file:", file.Path()) return err } metadata, err := psr.Metadata() if err != nil { - jww.ERROR.Println("Error processing file:", file.Path()) + site.Log.ERROR.Println("Error processing file:", file.Path()) return err } @@ -139,7 +136,7 @@ func convertContents(mark rune) error { page.SetDir(filepath.Join(contentDir, file.Dir())) page.SetSourceContent(psr.Content()) if err = page.SetSourceMetaData(metadata, mark); err != nil { - jww.ERROR.Printf("Failed to set source metadata for file %q: %s. For more info see For more info see https://github.com/spf13/hugo/issues/2458", page.FullFilePath(), err) + site.Log.ERROR.Printf("Failed to set source metadata for file %q: %s. For more info see For more info see https://github.com/spf13/hugo/issues/2458", page.FullFilePath(), err) continue } @@ -153,7 +150,7 @@ func convertContents(mark rune) error { return fmt.Errorf("Failed to save file %q: %s", page.FullFilePath(), err) } } else { - jww.FEEDBACK.Println("Unsafe operation not allowed, use --unsafe or set a different output path") + site.Log.FEEDBACK.Println("Unsafe operation not allowed, use --unsafe or set a different output path") } } } diff --git a/commands/hugo.go b/commands/hugo.go index 566e68603..3e8351658 100644 --- a/commands/hugo.go +++ b/commands/hugo.go @@ -27,8 +27,7 @@ import ( "sync" "time" - "github.com/spf13/hugo/tpl" - + "github.com/spf13/hugo/config" "github.com/spf13/hugo/hugofs" "github.com/spf13/hugo/parser" @@ -51,10 +50,6 @@ import ( "github.com/spf13/viper" ) -type commandeer struct { - deps.DepsCfg -} - // Hugo represents the Hugo sites to build. This variable is exported as it // is used by at least one external library (the Hugo caddy plugin). We should // provide a cleaner external API, but until then, this is it. @@ -64,7 +59,6 @@ var Hugo *hugolib.HugoSites // for benchmark testing etc. via the CLI commands. func Reset() error { Hugo = nil - viper.Reset() return nil } @@ -124,10 +118,10 @@ Complete documentation is available at http://gohugo.io/.`, return err } - c := commandeer{cfg} + c := newCommandeer(cfg) if buildWatch { - viper.Set("disableLiveReload", true) + cfg.Cfg.Set("disableLiveReload", true) c.watchConfig() } @@ -148,16 +142,17 @@ var ( ) var ( - baseURL string - cacheDir string - contentDir string - layoutDir string - cfgFile string - destination string - logFile string - theme string - themesDir string - source string + baseURL string + cacheDir string + contentDir string + layoutDir string + cfgFile string + destination string + logFile string + theme string + themesDir string + source string + logI18nWarnings bool ) // Execute adds all child commands to the root command HugoCmd and sets flags appropriately. @@ -242,7 +237,7 @@ func initHugoBuildCommonFlags(cmd *cobra.Command) { cmd.Flags().BoolP("forceSyncStatic", "", false, "Copy all files when static is changed.") cmd.Flags().BoolP("noTimes", "", false, "Don't sync modification time of files") cmd.Flags().BoolP("noChmod", "", false, "Don't sync permission mode of files") - cmd.Flags().BoolVarP(&tpl.Logi18nWarnings, "i18n-warnings", "", false, "Print missing translations") + cmd.Flags().BoolVarP(&logI18nWarnings, "i18n-warnings", "", false, "Print missing translations") // Set bash-completion. // Each flag must first be defined before using the SetAnnotation() call. @@ -275,39 +270,56 @@ func init() { } // InitializeConfig initializes a config file with sensible default configuration flags. -func InitializeConfig(subCmdVs ...*cobra.Command) (deps.DepsCfg, error) { +func InitializeConfig(subCmdVs ...*cobra.Command) (*deps.DepsCfg, error) { - var cfg deps.DepsCfg + var cfg *deps.DepsCfg = &deps.DepsCfg{} - if err := hugolib.LoadGlobalConfig(source, cfgFile); err != nil { + // Init file systems. This may be changed at a later point. + osFs := hugofs.Os + + config, err := hugolib.LoadConfig(osFs, source, cfgFile) + if err != nil { return cfg, err } + cfg.Cfg = config + + c := newCommandeer(cfg) + for _, cmdV := range append([]*cobra.Command{hugoCmdV}, subCmdVs...) { - initializeFlags(cmdV) + c.initializeFlags(cmdV) + } + + logger, err := createLogger(cfg.Cfg) + if err != nil { + return cfg, err } + cfg.Logger = logger + + config.Set("logI18nWarnings", logI18nWarnings) + if baseURL != "" { if !strings.HasSuffix(baseURL, "/") { baseURL = baseURL + "/" } - viper.Set("baseURL", baseURL) + config.Set("baseURL", baseURL) } - if !viper.GetBool("relativeURLs") && viper.GetString("baseURL") == "" { - jww.ERROR.Println("No 'baseURL' set in configuration or as a flag. Features like page menus will not work without one.") + if !config.GetBool("relativeURLs") && config.GetString("baseURL") == "" { + cfg.Logger.ERROR.Println("No 'baseURL' set in configuration or as a flag. Features like page menus will not work without one.") } if theme != "" { - viper.Set("theme", theme) + config.Set("theme", theme) } if themesDir != "" { - viper.Set("themesDir", themesDir) + config.Set("themesDir", themesDir) } if destination != "" { - viper.Set("publishDir", destination) + config.Set("publishDir", destination) } var dir string @@ -316,24 +328,32 @@ func InitializeConfig(subCmdVs ...*cobra.Command) (deps.DepsCfg, error) { } else { dir, _ = os.Getwd() } - viper.Set("workingDir", dir) + config.Set("workingDir", dir) + + cfg.Fs = hugofs.NewFrom(osFs, config) + + // 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. + if renderToMemory { + c.Fs.Destination = new(afero.MemMapFs) + // Rendering to memoryFS, publish to Root regardless of publishDir. + c.Set("publishDir", "/") + } if contentDir != "" { - viper.Set("contentDir", contentDir) + config.Set("contentDir", contentDir) } if layoutDir != "" { - viper.Set("layoutDir", layoutDir) + config.Set("layoutDir", layoutDir) } if cacheDir != "" { - viper.Set("cacheDir", cacheDir) + config.Set("cacheDir", cacheDir) } - // Init file systems. This may be changed at a later point. - cfg.Fs = hugofs.NewDefault() - - cacheDir = viper.GetString("cacheDir") + cacheDir = config.GetString("cacheDir") if cacheDir != "" { if helpers.FilePathSeparator != cacheDir[len(cacheDir)-1:] { cacheDir = cacheDir + helpers.FilePathSeparator @@ -343,39 +363,32 @@ func InitializeConfig(subCmdVs ...*cobra.Command) (deps.DepsCfg, error) { if !isDir { mkdir(cacheDir) } - viper.Set("cacheDir", cacheDir) + config.Set("cacheDir", cacheDir) } else { - viper.Set("cacheDir", helpers.GetTempDir("hugo_cache", cfg.Fs.Source)) + config.Set("cacheDir", helpers.GetTempDir("hugo_cache", cfg.Fs.Source)) } - jww.INFO.Println("Using config file:", viper.ConfigFileUsed()) + cfg.Logger.INFO.Println("Using config file:", viper.ConfigFileUsed()) - themeDir := helpers.GetThemeDir() + themeDir := c.PathSpec().GetThemeDir() if themeDir != "" { if _, err := cfg.Fs.Source.Stat(themeDir); os.IsNotExist(err) { return cfg, newSystemError("Unable to find theme Directory:", themeDir) } } - themeVersionMismatch, minVersion := isThemeVsHugoVersionMismatch(cfg.Fs.Source) + themeVersionMismatch, minVersion := c.isThemeVsHugoVersionMismatch() if themeVersionMismatch { - jww.ERROR.Printf("Current theme does not support Hugo version %s. Minimum version required is %s\n", + cfg.Logger.ERROR.Printf("Current theme does not support Hugo version %s. Minimum version required is %s\n", helpers.HugoReleaseVersion(), minVersion) } - logger, err := createLogger() - if err != nil { - return cfg, err - } - - cfg.Logger = logger - return cfg, nil } -func createLogger() (*jww.Notepad, error) { +func createLogger(cfg config.Provider) (*jww.Notepad, error) { var ( logHandle = ioutil.Discard outHandle = os.Stdout @@ -383,11 +396,11 @@ func createLogger() (*jww.Notepad, error) { logThreshold = jww.LevelWarn ) - if verboseLog || logging || (viper.IsSet("logFile") && viper.GetString("logFile") != "") { + if verboseLog || logging || (cfg.GetString("logFile") != "") { var err error - if viper.IsSet("logFile") && viper.GetString("logFile") != "" { - path := viper.GetString("logFile") + if cfg.GetString("logFile") != "" { + path := cfg.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) @@ -398,7 +411,7 @@ func createLogger() (*jww.Notepad, error) { return nil, newSystemError(err) } } - } else if !quiet && viper.GetBool("verbose") { + } else if !quiet && cfg.GetBool("verbose") { stdoutThreshold = jww.LevelInfo } @@ -409,7 +422,7 @@ func createLogger() (*jww.Notepad, error) { return jww.NewNotepad(stdoutThreshold, logThreshold, outHandle, logHandle, "", log.Ldate|log.Ltime), nil } -func initializeFlags(cmd *cobra.Command) { +func (c *commandeer) initializeFlags(cmd *cobra.Command) { persFlagKeys := []string{"verbose", "logFile"} flagKeys := []string{ "cleanDestinationDir", @@ -432,21 +445,21 @@ func initializeFlags(cmd *cobra.Command) { } for _, key := range persFlagKeys { - setValueFromFlag(cmd.PersistentFlags(), key) + c.setValueFromFlag(cmd.PersistentFlags(), key) } for _, key := range flagKeys { - setValueFromFlag(cmd.Flags(), key) + c.setValueFromFlag(cmd.Flags(), key) } } -func setValueFromFlag(flags *flag.FlagSet, key string) { - if flagChanged(flags, key) { +func (c *commandeer) setValueFromFlag(flags *flag.FlagSet, key string) { + if c.flagChanged(flags, key) { f := flags.Lookup(key) - viper.Set(key, f.Value.String()) + c.Set(key, f.Value.String()) } } -func flagChanged(flags *flag.FlagSet, key string) bool { +func (c *commandeer) flagChanged(flags *flag.FlagSet, key string) bool { flag := flags.Lookup(key) if flag == nil { return false @@ -454,31 +467,23 @@ func flagChanged(flags *flag.FlagSet, key string) bool { return flag.Changed } -func (c commandeer) watchConfig() { - viper.WatchConfig() - viper.OnConfigChange(func(e fsnotify.Event) { - jww.FEEDBACK.Println("Config file changed:", e.Name) +func (c *commandeer) watchConfig() { + v := c.Cfg.(*viper.Viper) + v.WatchConfig() + v.OnConfigChange(func(e fsnotify.Event) { + c.Logger.FEEDBACK.Println("Config file changed:", e.Name) // Force a full rebuild utils.CheckErr(c.recreateAndBuildSites(true)) - if !viper.GetBool("disableLiveReload") { + if !c.Cfg.GetBool("disableLiveReload") { // Will block forever trying to write to a channel that nobody is reading if livereload isn't initialized livereload.ForceRefresh() } }) } -func (c commandeer) build(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. - if renderToMemory { - c.Fs.Destination = new(afero.MemMapFs) - // Rendering to memoryFS, publish to Root regardless of publishDir. - viper.Set("publishDir", "/") - } - +func (c *commandeer) build(watches ...bool) error { if err := c.copyStatic(); err != nil { - return fmt.Errorf("Error copying static files to %s: %s", helpers.AbsPathify(viper.GetString("publishDir")), err) + return fmt.Errorf("Error copying static files to %s: %s", c.PathSpec().AbsPathify(c.Cfg.GetString("publishDir")), err) } watch := false if len(watches) > 0 && watches[0] { @@ -489,37 +494,35 @@ func (c commandeer) build(watches ...bool) error { } if buildWatch { - jww.FEEDBACK.Println("Watching for changes in", helpers.AbsPathify(viper.GetString("contentDir"))) - jww.FEEDBACK.Println("Press Ctrl+C to stop") + c.Logger.FEEDBACK.Println("Watching for changes in", c.PathSpec().AbsPathify(c.Cfg.GetString("contentDir"))) + c.Logger.FEEDBACK.Println("Press Ctrl+C to stop") utils.CheckErr(c.newWatcher(0)) } return nil } -func (c commandeer) getStaticSourceFs() afero.Fs { +func (c *commandeer) getStaticSourceFs() afero.Fs { source := c.Fs.Source - pathSpec := helpers.NewPathSpec(c.Fs, viper.GetViper()) - themeDir, err := pathSpec.GetThemeStaticDirPath() - staticDir := helpers.GetStaticDirPath() + helpers.FilePathSeparator - + themeDir, err := c.PathSpec().GetThemeStaticDirPath() + staticDir := c.PathSpec().GetStaticDirPath() + helpers.FilePathSeparator useTheme := true useStatic := true if err != nil { if err != helpers.ErrThemeUndefined { - jww.WARN.Println(err) + c.Logger.WARN.Println(err) } useTheme = false } else { if _, err := source.Stat(themeDir); os.IsNotExist(err) { - jww.WARN.Println("Unable to find Theme Static Directory:", themeDir) + c.Logger.WARN.Println("Unable to find Theme Static Directory:", themeDir) useTheme = false } } if _, err := source.Stat(staticDir); os.IsNotExist(err) { - jww.WARN.Println("Unable to find Static Directory:", staticDir) + c.Logger.WARN.Println("Unable to find Static Directory:", staticDir) useStatic = false } @@ -528,25 +531,25 @@ func (c commandeer) getStaticSourceFs() afero.Fs { } if !useStatic { - jww.INFO.Println(themeDir, "is the only static directory available to sync from") + c.Logger.INFO.Println(themeDir, "is the only static directory available to sync from") return afero.NewReadOnlyFs(afero.NewBasePathFs(source, themeDir)) } if !useTheme { - jww.INFO.Println(staticDir, "is the only static directory available to sync from") + c.Logger.INFO.Println(staticDir, "is the only static directory available to sync from") return afero.NewReadOnlyFs(afero.NewBasePathFs(source, staticDir)) } - jww.INFO.Println("using a UnionFS for static directory comprised of:") - jww.INFO.Println("Base:", themeDir) - jww.INFO.Println("Overlay:", staticDir) + c.Logger.INFO.Println("using a UnionFS for static directory comprised of:") + c.Logger.INFO.Println("Base:", themeDir) + c.Logger.INFO.Println("Overlay:", staticDir) base := afero.NewReadOnlyFs(afero.NewBasePathFs(source, themeDir)) overlay := afero.NewReadOnlyFs(afero.NewBasePathFs(source, staticDir)) return afero.NewCopyOnWriteFs(base, overlay) } -func (c commandeer) copyStatic() error { - publishDir := helpers.AbsPathify(viper.GetString("publishDir")) + helpers.FilePathSeparator +func (c *commandeer) copyStatic() error { + publishDir := c.PathSpec().AbsPathify(c.Cfg.GetString("publishDir")) + helpers.FilePathSeparator // If root, remove the second '/' if publishDir == "//" { @@ -557,22 +560,22 @@ func (c commandeer) copyStatic() error { staticSourceFs := c.getStaticSourceFs() if staticSourceFs == nil { - jww.WARN.Println("No static directories found to sync") + c.Logger.WARN.Println("No static directories found to sync") return nil } syncer := fsync.NewSyncer() - syncer.NoTimes = viper.GetBool("noTimes") - syncer.NoChmod = viper.GetBool("noChmod") + syncer.NoTimes = c.Cfg.GetBool("noTimes") + syncer.NoChmod = c.Cfg.GetBool("noChmod") syncer.SrcFs = staticSourceFs syncer.DestFs = c.Fs.Destination // Now that we are using a unionFs for the static directories // We can effectively clean the publishDir on initial sync - syncer.Delete = viper.GetBool("cleanDestinationDir") + syncer.Delete = c.Cfg.GetBool("cleanDestinationDir") if syncer.Delete { - jww.INFO.Println("removing all files from destination that don't exist in static dirs") + c.Logger.INFO.Println("removing all files from destination that don't exist in static dirs") } - jww.INFO.Println("syncing static files to", publishDir) + c.Logger.INFO.Println("syncing static files to", publishDir) // because we are using a baseFs (to get the union right). // set sync src to root @@ -580,37 +583,37 @@ func (c commandeer) copyStatic() error { } // getDirList provides NewWatcher() with a list of directories to watch for changes. -func (c commandeer) getDirList() []string { +func (c *commandeer) getDirList() []string { var a []string - dataDir := helpers.AbsPathify(viper.GetString("dataDir")) - i18nDir := helpers.AbsPathify(viper.GetString("i18nDir")) - layoutDir := helpers.AbsPathify(viper.GetString("layoutDir")) - staticDir := helpers.AbsPathify(viper.GetString("staticDir")) + dataDir := c.PathSpec().AbsPathify(c.Cfg.GetString("dataDir")) + i18nDir := c.PathSpec().AbsPathify(c.Cfg.GetString("i18nDir")) + layoutDir := c.PathSpec().AbsPathify(c.Cfg.GetString("layoutDir")) + staticDir := c.PathSpec().AbsPathify(c.Cfg.GetString("staticDir")) var themesDir string - if helpers.ThemeSet() { - themesDir = helpers.AbsPathify(viper.GetString("themesDir") + "/" + viper.GetString("theme")) + if c.PathSpec().ThemeSet() { + themesDir = c.PathSpec().AbsPathify(c.Cfg.GetString("themesDir") + "/" + c.Cfg.GetString("theme")) } walker := func(path string, fi os.FileInfo, err error) error { if err != nil { if path == dataDir && os.IsNotExist(err) { - jww.WARN.Println("Skip dataDir:", err) + c.Logger.WARN.Println("Skip dataDir:", err) return nil } if path == i18nDir && os.IsNotExist(err) { - jww.WARN.Println("Skip i18nDir:", err) + c.Logger.WARN.Println("Skip i18nDir:", err) return nil } if path == layoutDir && os.IsNotExist(err) { - jww.WARN.Println("Skip layoutDir:", err) + c.Logger.WARN.Println("Skip layoutDir:", err) return nil } if path == staticDir && os.IsNotExist(err) { - jww.WARN.Println("Skip staticDir:", err) + c.Logger.WARN.Println("Skip staticDir:", err) return nil } @@ -619,23 +622,23 @@ func (c commandeer) getDirList() []string { return nil } - jww.ERROR.Println("Walker: ", err) + c.Logger.ERROR.Println("Walker: ", err) return nil } if fi.Mode()&os.ModeSymlink == os.ModeSymlink { link, err := filepath.EvalSymlinks(path) if err != nil { - jww.ERROR.Printf("Cannot read symbolic link '%s', error was: %s", path, err) + c.Logger.ERROR.Printf("Cannot read symbolic link '%s', error was: %s", path, err) return nil } linkfi, err := c.Fs.Source.Stat(link) if err != nil { - jww.ERROR.Printf("Cannot stat '%s', error was: %s", link, err) + c.Logger.ERROR.Printf("Cannot stat '%s', error was: %s", link, err) return nil } if !linkfi.Mode().IsRegular() { - jww.ERROR.Printf("Symbolic links for directories not supported, skipping '%s'", path) + c.Logger.ERROR.Printf("Symbolic links for directories not supported, skipping '%s'", path) } return nil } @@ -651,12 +654,12 @@ func (c commandeer) getDirList() []string { } helpers.SymbolicWalk(c.Fs.Source, dataDir, walker) - helpers.SymbolicWalk(c.Fs.Source, helpers.AbsPathify(viper.GetString("contentDir")), walker) + helpers.SymbolicWalk(c.Fs.Source, c.PathSpec().AbsPathify(c.Cfg.GetString("contentDir")), walker) helpers.SymbolicWalk(c.Fs.Source, i18nDir, walker) - helpers.SymbolicWalk(c.Fs.Source, helpers.AbsPathify(viper.GetString("layoutDir")), walker) + helpers.SymbolicWalk(c.Fs.Source, c.PathSpec().AbsPathify(c.Cfg.GetString("layoutDir")), walker) helpers.SymbolicWalk(c.Fs.Source, staticDir, walker) - if helpers.ThemeSet() { + if c.PathSpec().ThemeSet() { helpers.SymbolicWalk(c.Fs.Source, filepath.Join(themesDir, "layouts"), walker) helpers.SymbolicWalk(c.Fs.Source, filepath.Join(themesDir, "static"), walker) helpers.SymbolicWalk(c.Fs.Source, filepath.Join(themesDir, "i18n"), walker) @@ -667,32 +670,31 @@ func (c commandeer) getDirList() []string { return a } -func (c commandeer) recreateAndBuildSites(watching bool) (err error) { +func (c *commandeer) recreateAndBuildSites(watching bool) (err error) { if err := c.initSites(); err != nil { return err } if !quiet { - jww.FEEDBACK.Println("Started building sites ...") + c.Logger.FEEDBACK.Println("Started building sites ...") } return Hugo.Build(hugolib.BuildCfg{CreateSitesFromConfig: true, Watching: watching, PrintStats: !quiet}) } -func (c commandeer) resetAndBuildSites(watching bool) (err error) { +func (c *commandeer) resetAndBuildSites(watching bool) (err error) { if err = c.initSites(); err != nil { return } if !quiet { - jww.FEEDBACK.Println("Started building sites ...") + c.Logger.FEEDBACK.Println("Started building sites ...") } return Hugo.Build(hugolib.BuildCfg{ResetState: true, Watching: watching, PrintStats: !quiet}) } -func (c commandeer) initSites() error { +func (c *commandeer) initSites() error { if Hugo != nil { return nil } - - h, err := hugolib.NewHugoSitesFromConfiguration(c.DepsCfg) + h, err := hugolib.NewHugoSites(*c.DepsCfg) if err != nil { return err @@ -702,17 +704,17 @@ func (c commandeer) initSites() error { return nil } -func (c commandeer) buildSites(watching bool) (err error) { +func (c *commandeer) buildSites(watching bool) (err error) { if err := c.initSites(); err != nil { return err } if !quiet { - jww.FEEDBACK.Println("Started building sites ...") + c.Logger.FEEDBACK.Println("Started building sites ...") } return Hugo.Build(hugolib.BuildCfg{Watching: watching, PrintStats: !quiet}) } -func (c commandeer) rebuildSites(events []fsnotify.Event) error { +func (c *commandeer) rebuildSites(events []fsnotify.Event) error { if err := c.initSites(); err != nil { return err } @@ -720,13 +722,11 @@ func (c commandeer) rebuildSites(events []fsnotify.Event) error { } // newWatcher creates a new watcher to watch filesystem events. -func (c commandeer) newWatcher(port int) error { +func (c *commandeer) newWatcher(port int) error { if runtime.GOOS == "darwin" { tweakLimit() } - pathSpec := helpers.NewPathSpec(c.Fs, viper.GetViper()) - watcher, err := watcher.New(1 * time.Second) var wg sync.WaitGroup @@ -748,7 +748,7 @@ func (c commandeer) newWatcher(port int) error { for { select { case evs := <-watcher.Events: - jww.INFO.Println("Received System Events:", evs) + c.Logger.INFO.Println("Received System Events:", evs) staticEvents := []fsnotify.Event{} dynamicEvents := []fsnotify.Event{} @@ -794,7 +794,7 @@ func (c commandeer) newWatcher(port int) error { walkAdder := func(path string, f os.FileInfo, err error) error { if f.IsDir() { - jww.FEEDBACK.Println("adding created directory to watchlist", path) + c.Logger.FEEDBACK.Println("adding created directory to watchlist", path) watcher.Add(path) } return nil @@ -808,7 +808,7 @@ func (c commandeer) newWatcher(port int) error { } } - isstatic := strings.HasPrefix(ev.Name, helpers.GetStaticDirPath()) || (len(pathSpec.GetThemesDirPath()) > 0 && strings.HasPrefix(ev.Name, pathSpec.GetThemesDirPath())) + isstatic := strings.HasPrefix(ev.Name, c.PathSpec().GetStaticDirPath()) || (len(c.PathSpec().GetThemesDirPath()) > 0 && strings.HasPrefix(ev.Name, c.PathSpec().GetThemesDirPath())) if isstatic { staticEvents = append(staticEvents, ev) @@ -818,19 +818,19 @@ func (c commandeer) newWatcher(port int) error { } if len(staticEvents) > 0 { - publishDir := helpers.AbsPathify(viper.GetString("publishDir")) + helpers.FilePathSeparator + publishDir := c.PathSpec().AbsPathify(c.Cfg.GetString("publishDir")) + helpers.FilePathSeparator // If root, remove the second '/' if publishDir == "//" { publishDir = helpers.FilePathSeparator } - jww.FEEDBACK.Println("\nStatic file changes detected") + c.Logger.FEEDBACK.Println("\nStatic file changes detected") const layout = "2006-01-02 15:04 -0700" - jww.FEEDBACK.Println(time.Now().Format(layout)) + c.Logger.FEEDBA |