shithub: hugo

Download patch

ref: b110d0ae04e13fb45c739bcebb580709745082e6
parent: 73825cfc1c0b007830b24bb1947a565175b52d36
author: Bjørn Erik Pedersen <[email protected]>
date: Wed Apr 11 04:39:39 EDT 2018

commands: Remove the Hugo global

There are still some cleaning to do, but that felt good.

See #4598

--- a/commands/commandeer.go
+++ b/commands/commandeer.go
@@ -40,6 +40,8 @@
 type commandeer struct {
 	*deps.DepsCfg
 
+	hugo *hugolib.HugoSites
+
 	h    *hugoBuilderCommon
 	ftch flagsToConfigHandler
 
--- a/commands/commands_test.go
+++ b/commands/commands_test.go
@@ -50,8 +50,7 @@
 		{[]string{"version"}, nil, ""},
 		// no args = hugo build
 		{nil, []string{sourceFlag}, ""},
-		// TODO(bep) cli refactor remove the HugoSites global and enable the below
-		//{nil, []string{sourceFlag, "--renderToMemory"},false},
+		{nil, []string{sourceFlag, "--renderToMemory"}, ""},
 		{[]string{"benchmark"}, []string{sourceFlag, "-n=1"}, ""},
 		{[]string{"convert", "toTOML"}, []string{sourceFlag, "-o=" + filepath.Join(dirOut, "toml")}, ""},
 		{[]string{"convert", "toYAML"}, []string{sourceFlag, "-o=" + filepath.Join(dirOut, "yaml")}, ""},
--- a/commands/hugo.go
+++ b/commands/hugo.go
@@ -51,15 +51,19 @@
 	jww "github.com/spf13/jwalterweatherman"
 )
 
+// TODO(bep) cli refactor consider a exported Hugo() method to fix the API
+
 // 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.
-var Hugo *hugolib.HugoSites
+// TODO(bep)  cli refactor remove this
+//var Hugo *hugolib.HugoSites
 
 // Reset resets Hugo ready for a new full build. This is mainly only useful
 // for benchmark testing etc. via the CLI commands.
+// TODO(bep) cli refactor check usage
 func Reset() error {
-	Hugo = nil
+	//Hugo = nil
 	return nil
 }
 
@@ -259,16 +263,16 @@
 		}
 	}
 
-	for _, s := range Hugo.Sites {
+	for _, s := range c.hugo.Sites {
 		s.ProcessingStats.Static = langCount[s.Language.Lang]
 	}
 
 	if c.h.gc {
-		count, err := Hugo.GC()
+		count, err := c.hugo.GC()
 		if err != nil {
 			return err
 		}
-		for _, s := range Hugo.Sites {
+		for _, s := range c.hugo.Sites {
 			// We have no way of knowing what site the garbage belonged to.
 			s.ProcessingStats.Cleaned = uint64(count)
 		}
@@ -288,7 +292,7 @@
 	// TODO(bep) Feedback?
 	if !c.h.quiet {
 		fmt.Println()
-		Hugo.PrintProcessingStats(os.Stdout)
+		c.hugo.PrintProcessingStats(os.Stdout)
 		fmt.Println()
 	}
 
@@ -322,7 +326,7 @@
 	// TODO(bep) Feedback?
 	if !c.h.quiet {
 		fmt.Println()
-		Hugo.PrintProcessingStats(os.Stdout)
+		c.hugo.PrintProcessingStats(os.Stdout)
 		fmt.Println()
 	}
 
@@ -607,7 +611,7 @@
 	if !c.h.quiet {
 		c.Logger.FEEDBACK.Println("Started building sites ...")
 	}
-	return Hugo.Build(hugolib.BuildCfg{CreateSitesFromConfig: true})
+	return c.hugo.Build(hugolib.BuildCfg{CreateSitesFromConfig: true})
 }
 
 func (c *commandeer) resetAndBuildSites() (err error) {
@@ -617,22 +621,25 @@
 	if !c.h.quiet {
 		c.Logger.FEEDBACK.Println("Started building sites ...")
 	}
-	return Hugo.Build(hugolib.BuildCfg{ResetState: true})
+	return c.hugo.Build(hugolib.BuildCfg{ResetState: true})
 }
 
 func (c *commandeer) initSites() error {
-	if Hugo != nil {
-		Hugo.Cfg = c.Cfg
-		Hugo.Log.ResetLogCounters()
+	if c.hugo != nil {
+		// TODO(bep) cli refactor check
+		c.hugo.Cfg = c.Cfg
+		c.hugo.Log.ResetLogCounters()
 		return nil
 	}
+
 	h, err := hugolib.NewHugoSites(*c.DepsCfg)
 
 	if err != nil {
 		return err
 	}
-	Hugo = h
 
+	c.hugo = h
+
 	return nil
 }
 
@@ -640,7 +647,7 @@
 	if err := c.initSites(); err != nil {
 		return err
 	}
-	return Hugo.Build(hugolib.BuildCfg{})
+	return c.hugo.Build(hugolib.BuildCfg{})
 }
 
 func (c *commandeer) rebuildSites(events []fsnotify.Event) error {
@@ -664,7 +671,7 @@
 		}
 
 	}
-	return Hugo.Build(hugolib.BuildCfg{RecentlyVisited: visited}, events...)
+	return c.hugo.Build(hugolib.BuildCfg{RecentlyVisited: visited}, events...)
 }
 
 func (c *commandeer) fullRebuild() {
@@ -738,7 +745,7 @@
 					}
 
 					// Check the most specific first, i.e. files.
-					contentMapped := Hugo.ContentChanges.GetSymbolicLinkMappings(ev.Name)
+					contentMapped := c.hugo.ContentChanges.GetSymbolicLinkMappings(ev.Name)
 					if len(contentMapped) > 0 {
 						for _, mapped := range contentMapped {
 							filtered = append(filtered, fsnotify.Event{Name: mapped, Op: ev.Op})
@@ -750,7 +757,7 @@
 
 					dir, name := filepath.Split(ev.Name)
 
-					contentMapped = Hugo.ContentChanges.GetSymbolicLinkMappings(dir)
+					contentMapped = c.hugo.ContentChanges.GetSymbolicLinkMappings(dir)
 
 					if len(contentMapped) == 0 {
 						filtered = append(filtered, ev)
@@ -888,7 +895,7 @@
 
 						if navigate {
 							if onePageName != "" {
-								p = Hugo.GetContentPage(onePageName)
+								p = c.hugo.GetContentPage(onePageName)
 							}
 
 						}
--- a/commands/new.go
+++ b/commands/new.go
@@ -109,15 +109,15 @@
 			return nil, err
 		}
 
-		if err := Hugo.Build(hugolib.BuildCfg{SkipRender: true}); err != nil {
+		if err := c.hugo.Build(hugolib.BuildCfg{SkipRender: true}); err != nil {
 			return nil, err
 		}
 
-		s = Hugo.Sites[0]
+		s = c.hugo.Sites[0]
 
-		if len(Hugo.Sites) > 1 {
+		if len(c.hugo.Sites) > 1 {
 			// Find the best match.
-			for _, ss := range Hugo.Sites {
+			for _, ss := range c.hugo.Sites {
 				if strings.Contains(createPath, "."+ss.Language.Lang) {
 					s = ss
 					break
--- a/commands/server.go
+++ b/commands/server.go
@@ -39,8 +39,6 @@
 )
 
 type serverCmd struct {
-	hugoBuilderCommon
-
 	disableLiveReload bool
 	navigateToChanged bool
 	renderToDisk      bool
@@ -53,13 +51,13 @@
 
 	disableFastRender bool
 
-	*baseCmd
+	*baseBuilderCmd
 }
 
 func newServerCmd() *serverCmd {
 	cc := &serverCmd{}
 
-	cc.baseCmd = newBaseCmd(&cobra.Command{
+	cc.baseBuilderCmd = newBuilderCmd(&cobra.Command{
 		Use:     "server",
 		Aliases: []string{"serve"},
 		Short:   "A high performance webserver",
@@ -232,7 +230,7 @@
 		return err
 	}
 
-	for _, s := range Hugo.Sites {
+	for _, s := range c.hugo.Sites {
 		s.RegisterMediaTypes()
 	}
 
@@ -345,7 +343,7 @@
 // TODO(bep) cli refactor
 func (c *commandeer) serve(s *serverCmd) error {
 
-	isMultiHost := Hugo.IsMultihost()
+	isMultiHost := c.hugo.IsMultihost()
 
 	var (
 		baseURLs []string
@@ -353,12 +351,12 @@
 	)
 
 	if isMultiHost {
-		for _, s := range Hugo.Sites {
+		for _, s := range c.hugo.Sites {
 			baseURLs = append(baseURLs, s.BaseURL.String())
 			roots = append(roots, s.Language.Lang)
 		}
 	} else {
-		s := Hugo.Sites[0]
+		s := c.hugo.Sites[0]
 		baseURLs = []string{s.BaseURL.String()}
 		roots = []string{""}
 	}
--- a/main.go
+++ b/main.go
@@ -31,9 +31,10 @@
 		os.Exit(-1)
 	}
 
-	if commands.Hugo != nil {
+	// TODO(bep) cli refactor
+	/*if commands.Hugo != nil {
 		if commands.Hugo.Log.LogCountForLevelsGreaterThanorEqualTo(jww.LevelError) > 0 {
 			os.Exit(-1)
 		}
-	}
+	}*/
 }