diff options
Diffstat (limited to 'commands')
-rw-r--r-- | commands/commands.go | 12 | ||||
-rw-r--r-- | commands/commands_test.go | 20 | ||||
-rw-r--r-- | commands/hugo.go | 50 |
3 files changed, 69 insertions, 13 deletions
diff --git a/commands/commands.go b/commands/commands.go index aad47abcd..86486d2a4 100644 --- a/commands/commands.go +++ b/commands/commands.go @@ -22,10 +22,10 @@ import ( ) // newHugoCompleteCmd builds the complete set of Hugo CLI commands. -func newHugoCompleteCmd() *cobra.Command { - hugoCmd := newHugoCmd().getCommand() - addAllCommands(hugoCmd) - return hugoCmd +func newHugoCompleteCmd() *hugoCmd { + h := newHugoCmd() + addAllCommands(h.getCommand()) + return h } // addAllCommands adds child commands to the root command HugoCmd. @@ -81,6 +81,9 @@ func (c *baseCmd) flagsToConfig(cfg config.Provider) { type hugoCmd struct { *baseBuilderCmd + + // Need to get the sites once built. + c *commandeer } func newHugoCmd() *hugoCmd { @@ -107,6 +110,7 @@ Complete documentation is available at http://gohugo.io/.`, if err != nil { return err } + cc.c = c return c.build() }, diff --git a/commands/commands_test.go b/commands/commands_test.go index 7e590bea0..ea9d3f74d 100644 --- a/commands/commands_test.go +++ b/commands/commands_test.go @@ -23,6 +23,24 @@ import ( "github.com/stretchr/testify/require" ) +func TestExecute(t *testing.T) { + + assert := require.New(t) + + dir, err := createSimpleTestSite(t) + assert.NoError(err) + + defer func() { + os.RemoveAll(dir) + }() + + resp := Execute([]string{"-s=" + dir}) + assert.NoError(resp.Err) + result := resp.Result + assert.True(len(result.Sites) == 1) + assert.True(len(result.Sites[0].RegularPages) == 1) +} + func TestCommands(t *testing.T) { assert := require.New(t) @@ -72,7 +90,7 @@ func TestCommands(t *testing.T) { for _, test := range tests { - hugoCmd := newHugoCompleteCmd() + hugoCmd := newHugoCompleteCmd().getCommand() test.flags = append(test.flags, "--quiet") hugoCmd.SetArgs(append(test.commands, test.flags...)) diff --git a/commands/hugo.go b/commands/hugo.go index 4639c0f3b..c1cf9833f 100644 --- a/commands/hugo.go +++ b/commands/hugo.go @@ -51,20 +51,55 @@ import ( jww "github.com/spf13/jwalterweatherman" ) -// TODO(bep) cli refactor consider a exported Hugo() method to fix the API +// The Response value from Execute. +type Response struct { + // The build Result will only be set in the hugo build command. + Result *hugolib.HugoSites + + // Err is set when the command failed to execute. + Err error + + // The command that was executed. + Cmd *cobra.Command +} + +func (r Response) IsUserError() bool { + return r.Err != nil && isUserError(r.Err) +} // Execute adds all child commands to the root command HugoCmd and sets flags appropriately. -func Execute() { +// The args are usually filled with os.Args[1:]. +func Execute(args []string) Response { hugoCmd := newHugoCompleteCmd() + cmd := hugoCmd.getCommand() + cmd.SetArgs(args) + + c, err := cmd.ExecuteC() + + var resp Response - if c, err := hugoCmd.ExecuteC(); err != nil { - if isUserError(err) { - c.Println("") - c.Println(c.UsageString()) + if c == cmd && hugoCmd.c != nil { + // Root command executed + resp.Result = hugoCmd.c.hugo + } + + if err == nil { + errCount := jww.LogCountForLevelsGreaterThanorEqualTo(jww.LevelError) + if errCount > 0 { + err = fmt.Errorf("logged %d errors", errCount) + } else if resp.Result != nil { + errCount = resp.Result.Log.LogCountForLevelsGreaterThanorEqualTo(jww.LevelError) + if errCount > 0 { + err = fmt.Errorf("logged %d errors", errCount) + } } - os.Exit(-1) } + + resp.Err = err + resp.Cmd = c + + return resp } // InitializeConfig initializes a config file with sensible default configuration flags. @@ -612,7 +647,6 @@ func (c *commandeer) resetAndBuildSites() (err error) { func (c *commandeer) initSites() error { if c.hugo != nil { - // TODO(bep) cli refactor check c.hugo.Cfg = c.Cfg c.hugo.Log.ResetLogCounters() return nil |