Skip to content

Commit

Permalink
commands: Remove the Hugo global
Browse files Browse the repository at this point in the history
There are still some cleaning to do, but that felt good.

See #4598
  • Loading branch information
bep committed Apr 11, 2018
1 parent 73825cf commit b110d0a
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 34 deletions.
2 changes: 2 additions & 0 deletions commands/commandeer.go
Expand Up @@ -40,6 +40,8 @@ import (
type commandeer struct { type commandeer struct {
*deps.DepsCfg *deps.DepsCfg


hugo *hugolib.HugoSites

h *hugoBuilderCommon h *hugoBuilderCommon
ftch flagsToConfigHandler ftch flagsToConfigHandler


Expand Down
3 changes: 1 addition & 2 deletions commands/commands_test.go
Expand Up @@ -50,8 +50,7 @@ func TestCommands(t *testing.T) {
{[]string{"version"}, nil, ""}, {[]string{"version"}, nil, ""},
// no args = hugo build // no args = hugo build
{nil, []string{sourceFlag}, ""}, {nil, []string{sourceFlag}, ""},
// TODO(bep) cli refactor remove the HugoSites global and enable the below {nil, []string{sourceFlag, "--renderToMemory"}, ""},
//{nil, []string{sourceFlag, "--renderToMemory"},false},
{[]string{"benchmark"}, []string{sourceFlag, "-n=1"}, ""}, {[]string{"benchmark"}, []string{sourceFlag, "-n=1"}, ""},
{[]string{"convert", "toTOML"}, []string{sourceFlag, "-o=" + filepath.Join(dirOut, "toml")}, ""}, {[]string{"convert", "toTOML"}, []string{sourceFlag, "-o=" + filepath.Join(dirOut, "toml")}, ""},
{[]string{"convert", "toYAML"}, []string{sourceFlag, "-o=" + filepath.Join(dirOut, "yaml")}, ""}, {[]string{"convert", "toYAML"}, []string{sourceFlag, "-o=" + filepath.Join(dirOut, "yaml")}, ""},
Expand Down
43 changes: 25 additions & 18 deletions commands/hugo.go
Expand Up @@ -51,15 +51,19 @@ import (
jww "github.com/spf13/jwalterweatherman" 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 // 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 // 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. // 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 // Reset resets Hugo ready for a new full build. This is mainly only useful
// for benchmark testing etc. via the CLI commands. // for benchmark testing etc. via the CLI commands.
// TODO(bep) cli refactor check usage
func Reset() error { func Reset() error {
Hugo = nil //Hugo = nil
return nil return nil
} }


Expand Down Expand Up @@ -259,16 +263,16 @@ func (c *commandeer) fullBuild() error {
} }
} }


for _, s := range Hugo.Sites { for _, s := range c.hugo.Sites {
s.ProcessingStats.Static = langCount[s.Language.Lang] s.ProcessingStats.Static = langCount[s.Language.Lang]
} }


if c.h.gc { if c.h.gc {
count, err := Hugo.GC() count, err := c.hugo.GC()
if err != nil { if err != nil {
return err 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. // We have no way of knowing what site the garbage belonged to.
s.ProcessingStats.Cleaned = uint64(count) s.ProcessingStats.Cleaned = uint64(count)
} }
Expand All @@ -288,7 +292,7 @@ func (c *commandeer) build() error {
// TODO(bep) Feedback? // TODO(bep) Feedback?
if !c.h.quiet { if !c.h.quiet {
fmt.Println() fmt.Println()
Hugo.PrintProcessingStats(os.Stdout) c.hugo.PrintProcessingStats(os.Stdout)
fmt.Println() fmt.Println()
} }


Expand Down Expand Up @@ -322,7 +326,7 @@ func (c *commandeer) serverBuild() error {
// TODO(bep) Feedback? // TODO(bep) Feedback?
if !c.h.quiet { if !c.h.quiet {
fmt.Println() fmt.Println()
Hugo.PrintProcessingStats(os.Stdout) c.hugo.PrintProcessingStats(os.Stdout)
fmt.Println() fmt.Println()
} }


Expand Down Expand Up @@ -607,7 +611,7 @@ func (c *commandeer) recreateAndBuildSites(watching bool) (err error) {
if !c.h.quiet { if !c.h.quiet {
c.Logger.FEEDBACK.Println("Started building sites ...") 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) { func (c *commandeer) resetAndBuildSites() (err error) {
Expand All @@ -617,21 +621,24 @@ func (c *commandeer) resetAndBuildSites() (err error) {
if !c.h.quiet { if !c.h.quiet {
c.Logger.FEEDBACK.Println("Started building sites ...") 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 { func (c *commandeer) initSites() error {
if Hugo != nil { if c.hugo != nil {
Hugo.Cfg = c.Cfg // TODO(bep) cli refactor check
Hugo.Log.ResetLogCounters() c.hugo.Cfg = c.Cfg
c.hugo.Log.ResetLogCounters()
return nil return nil
} }

h, err := hugolib.NewHugoSites(*c.DepsCfg) h, err := hugolib.NewHugoSites(*c.DepsCfg)


if err != nil { if err != nil {
return err return err
} }
Hugo = h
c.hugo = h


return nil return nil
} }
Expand All @@ -640,7 +647,7 @@ func (c *commandeer) buildSites() (err error) {
if err := c.initSites(); err != nil { if err := c.initSites(); err != nil {
return err return err
} }
return Hugo.Build(hugolib.BuildCfg{}) return c.hugo.Build(hugolib.BuildCfg{})
} }


func (c *commandeer) rebuildSites(events []fsnotify.Event) error { func (c *commandeer) rebuildSites(events []fsnotify.Event) error {
Expand All @@ -664,7 +671,7 @@ func (c *commandeer) rebuildSites(events []fsnotify.Event) error {
} }


} }
return Hugo.Build(hugolib.BuildCfg{RecentlyVisited: visited}, events...) return c.hugo.Build(hugolib.BuildCfg{RecentlyVisited: visited}, events...)
} }


func (c *commandeer) fullRebuild() { func (c *commandeer) fullRebuild() {
Expand Down Expand Up @@ -738,7 +745,7 @@ func (c *commandeer) newWatcher(dirList ...string) (*watcher.Batcher, error) {
} }


// Check the most specific first, i.e. files. // 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 { if len(contentMapped) > 0 {
for _, mapped := range contentMapped { for _, mapped := range contentMapped {
filtered = append(filtered, fsnotify.Event{Name: mapped, Op: ev.Op}) filtered = append(filtered, fsnotify.Event{Name: mapped, Op: ev.Op})
Expand All @@ -750,7 +757,7 @@ func (c *commandeer) newWatcher(dirList ...string) (*watcher.Batcher, error) {


dir, name := filepath.Split(ev.Name) dir, name := filepath.Split(ev.Name)


contentMapped = Hugo.ContentChanges.GetSymbolicLinkMappings(dir) contentMapped = c.hugo.ContentChanges.GetSymbolicLinkMappings(dir)


if len(contentMapped) == 0 { if len(contentMapped) == 0 {
filtered = append(filtered, ev) filtered = append(filtered, ev)
Expand Down Expand Up @@ -888,7 +895,7 @@ func (c *commandeer) newWatcher(dirList ...string) (*watcher.Batcher, error) {


if navigate { if navigate {
if onePageName != "" { if onePageName != "" {
p = Hugo.GetContentPage(onePageName) p = c.hugo.GetContentPage(onePageName)
} }


} }
Expand Down
8 changes: 4 additions & 4 deletions commands/new.go
Expand Up @@ -109,15 +109,15 @@ func (n *newCmd) newContent(cmd *cobra.Command, args []string) error {
return nil, err 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 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. // Find the best match.
for _, ss := range Hugo.Sites { for _, ss := range c.hugo.Sites {
if strings.Contains(createPath, "."+ss.Language.Lang) { if strings.Contains(createPath, "."+ss.Language.Lang) {
s = ss s = ss
break break
Expand Down
14 changes: 6 additions & 8 deletions commands/server.go
Expand Up @@ -39,8 +39,6 @@ import (
) )


type serverCmd struct { type serverCmd struct {
hugoBuilderCommon

disableLiveReload bool disableLiveReload bool
navigateToChanged bool navigateToChanged bool
renderToDisk bool renderToDisk bool
Expand All @@ -53,13 +51,13 @@ type serverCmd struct {


disableFastRender bool disableFastRender bool


*baseCmd *baseBuilderCmd
} }


func newServerCmd() *serverCmd { func newServerCmd() *serverCmd {
cc := &serverCmd{} cc := &serverCmd{}


cc.baseCmd = newBaseCmd(&cobra.Command{ cc.baseBuilderCmd = newBuilderCmd(&cobra.Command{
Use: "server", Use: "server",
Aliases: []string{"serve"}, Aliases: []string{"serve"},
Short: "A high performance webserver", Short: "A high performance webserver",
Expand Down Expand Up @@ -232,7 +230,7 @@ func (s *serverCmd) server(cmd *cobra.Command, args []string) error {
return err return err
} }


for _, s := range Hugo.Sites { for _, s := range c.hugo.Sites {
s.RegisterMediaTypes() s.RegisterMediaTypes()
} }


Expand Down Expand Up @@ -345,20 +343,20 @@ func (f *fileServer) createEndpoint(i int) (*http.ServeMux, string, string, erro
// TODO(bep) cli refactor // TODO(bep) cli refactor
func (c *commandeer) serve(s *serverCmd) error { func (c *commandeer) serve(s *serverCmd) error {


isMultiHost := Hugo.IsMultihost() isMultiHost := c.hugo.IsMultihost()


var ( var (
baseURLs []string baseURLs []string
roots []string roots []string
) )


if isMultiHost { if isMultiHost {
for _, s := range Hugo.Sites { for _, s := range c.hugo.Sites {
baseURLs = append(baseURLs, s.BaseURL.String()) baseURLs = append(baseURLs, s.BaseURL.String())
roots = append(roots, s.Language.Lang) roots = append(roots, s.Language.Lang)
} }
} else { } else {
s := Hugo.Sites[0] s := c.hugo.Sites[0]
baseURLs = []string{s.BaseURL.String()} baseURLs = []string{s.BaseURL.String()}
roots = []string{""} roots = []string{""}
} }
Expand Down
5 changes: 3 additions & 2 deletions main.go
Expand Up @@ -31,9 +31,10 @@ func main() {
os.Exit(-1) os.Exit(-1)
} }


if commands.Hugo != nil { // TODO(bep) cli refactor
/*if commands.Hugo != nil {
if commands.Hugo.Log.LogCountForLevelsGreaterThanorEqualTo(jww.LevelError) > 0 { if commands.Hugo.Log.LogCountForLevelsGreaterThanorEqualTo(jww.LevelError) > 0 {
os.Exit(-1) os.Exit(-1)
} }
} }*/
} }

0 comments on commit b110d0a

Please sign in to comment.