Repository: gohugoio/hugo (language: go)

Issue:
Command 'hugo new theme': better error handling
The command `new theme` panics if no argument `name` is given for the name of the theme:

### hugo v.0.114.1 (panic)

<pre>
$ hugo new theme
panic: runtime error: index out of range [0] with length 0

goroutine 1 [running]:
github.com/gohugoio/hugo/commands.newNewCommand.func5({0x0?, 0x0?}, 0x0?, 0xc000dbaa00, {0x3aa2188, 0x0, 0x0?})
        /root/project/hugo/commands/new.go:170 +0xa48
github.com/gohugoio/hugo/commands.(*simpleCommand).Run(0x0?, {0x2ab0228?, 0xc0000380f8?}, 0x0?, {0x3aa2188?, 0x0?, 0x0?})
        /root/project/hugo/commands/commandeer.go:597 +0x43
github.com/bep/simplecobra.(*Commandeer).compile.func1(0xc000005800?, {0x3aa2188?, 0x0?, 0x0?})
        /root/project/gomodcache/github.com/bep/simplecobra@v0.3.2/simplecobra.go:113 +0x52
github.com/spf13/cobra.(*Command).execute(0xc000005800, {0x3aa2188, 0x0, 0x0})
        /root/project/gomodcache/github.com/spf13/cobra@v1.7.0/command.go:940 +0x862
github.com/spf13/cobra.(*Command).ExecuteC(0xc000d7c300)
        /root/project/gomodcache/github.com/spf13/cobra@v1.7.0/command.go:1068 +0x3bd
github.com/spf13/cobra.(*Command).ExecuteContextC(...)
        /root/project/gomodcache/github.com/spf13/cobra@v1.7.0/command.go:1001
github.com/bep/simplecobra.(*Exec).Execute(0xc00000a738, {0x2ab0228?, 0xc0000380f8?}, {0xc000058090?, 0x195b28?, 0xc000081138?})
        /root/project/gomodcache/github.com/bep/simplecobra@v0.3.2/simplecobra.go:155 +0xd9
github.com/gohugoio/hugo/commands.Execute({0xc000058090, 0x2, 0x3})
        /root/project/hugo/commands/commandeer.go:66 +0x312
main.main()
        /root/project/hugo/main.go:25 +0x65
</pre>

This wasn't the case with previous versions:

### hugo v.0.101 (error message, no panic)

<pre>
$ hugo_0.101.0.exe new theme
Error: theme name needs to be provided


Usage:
  hugo new theme [name] [flags]

Flags:
  -h, --help   help for theme

Global Flags:
      --clock string               set the clock used by Hugo, e.g. --clock 2021-11-06T22:30:00.00+09:00
      --config string              config file (default is path/config.yaml|json|toml)
      --configDir string           config dir (default "config")
      --debug                      debug output
  -e, --environment string         build environment
      --ignoreVendorPaths string   ignores any _vendor for module paths matching the given Glob pattern
      --log                        enable Logging
      --logFile string             log File path (if set, logging enabled automatically)
      --quiet                      build in quiet mode
  -s, --source string              filesystem path to read files relative from
      --themesDir string           filesystem path to themes directory
  -v, --verbose                    verbose output
      --verboseLog                 verbose logging
</pre>

### Does this issue reproduce with the latest release?

Yes
Command 'hugo new theme': better error handling
The command `new theme` panics if no argument `name` is given for the name of the theme:

### hugo v.0.114.1 (panic)

<pre>
$ hugo new theme
panic: runtime error: index out of range [0] with length 0

goroutine 1 [running]:
github.com/gohugoio/hugo/commands.newNewCommand.func5({0x0?, 0x0?}, 0x0?, 0xc000dbaa00, {0x3aa2188, 0x0, 0x0?})
        /root/project/hugo/commands/new.go:170 +0xa48
github.com/gohugoio/hugo/commands.(*simpleCommand).Run(0x0?, {0x2ab0228?, 0xc0000380f8?}, 0x0?, {0x3aa2188?, 0x0?, 0x0?})
        /root/project/hugo/commands/commandeer.go:597 +0x43
github.com/bep/simplecobra.(*Commandeer).compile.func1(0xc000005800?, {0x3aa2188?, 0x0?, 0x0?})
        /root/project/gomodcache/github.com/bep/simplecobra@v0.3.2/simplecobra.go:113 +0x52
github.com/spf13/cobra.(*Command).execute(0xc000005800, {0x3aa2188, 0x0, 0x0})
        /root/project/gomodcache/github.com/spf13/cobra@v1.7.0/command.go:940 +0x862
github.com/spf13/cobra.(*Command).ExecuteC(0xc000d7c300)
        /root/project/gomodcache/github.com/spf13/cobra@v1.7.0/command.go:1068 +0x3bd
github.com/spf13/cobra.(*Command).ExecuteContextC(...)
        /root/project/gomodcache/github.com/spf13/cobra@v1.7.0/command.go:1001
github.com/bep/simplecobra.(*Exec).Execute(0xc00000a738, {0x2ab0228?, 0xc0000380f8?}, {0xc000058090?, 0x195b28?, 0xc000081138?})
        /root/project/gomodcache/github.com/bep/simplecobra@v0.3.2/simplecobra.go:155 +0xd9
github.com/gohugoio/hugo/commands.Execute({0xc000058090, 0x2, 0x3})
        /root/project/hugo/commands/commandeer.go:66 +0x312
main.main()
        /root/project/hugo/main.go:25 +0x65
</pre>

This wasn't the case with previous versions:

### hugo v.0.101 (error message, no panic)

<pre>
$ hugo_0.101.0.exe new theme
Error: theme name needs to be provided


Usage:
  hugo new theme [name] [flags]

Flags:
  -h, --help   help for theme

Global Flags:
      --clock string               set the clock used by Hugo, e.g. --clock 2021-11-06T22:30:00.00+09:00
      --config string              config file (default is path/config.yaml|json|toml)
      --configDir string           config dir (default "config")
      --debug                      debug output
  -e, --environment string         build environment
      --ignoreVendorPaths string   ignores any _vendor for module paths matching the given Glob pattern
      --log                        enable Logging
      --logFile string             log File path (if set, logging enabled automatically)
      --quiet                      build in quiet mode
  -s, --source string              filesystem path to read files relative from
      --themesDir string           filesystem path to themes directory
  -v, --verbose                    verbose output
      --verboseLog                 verbose logging
</pre>

### Does this issue reproduce with the latest release?

Yes


Discussion / hints:

Fix patch:
diff --git a/commands/new.go b/commands/new.go
index 633f1becb78..902072d4e87 100644
--- a/commands/new.go
+++ b/commands/new.go
@@ -56,7 +56,7 @@ If archetypes are provided in your theme or site, they will be used.
 Ensure you run this within the root directory of your site.`,
 				run: func(ctx context.Context, cd *simplecobra.Commandeer, r *rootCommand, args []string) error {
 					if len(args) < 1 {
-						return errors.New("path needs to be provided")
+						return newUserError("path needs to be provided")
 					}
 					h, err := r.Hugo(flagsToCfg(cd, nil))
 					if err != nil {
@@ -82,7 +82,7 @@ The new site will have the correct structure, but no content or theme yet.
 Use ` + "`hugo new [contentPath]`" + ` to create new content.`,
 				run: func(ctx context.Context, cd *simplecobra.Commandeer, r *rootCommand, args []string) error {
 					if len(args) < 1 {
-						return errors.New("path needs to be provided")
+						return newUserError("path needs to be provided")
 					}
 					createpath, err := filepath.Abs(filepath.Clean(args[0]))
 					if err != nil {
@@ -162,6 +162,9 @@ New theme is a skeleton. Please add content to the touched files. Add your
 name to the copyright line in the license and adjust the theme.toml file
 according to your needs.`,
 				run: func(ctx context.Context, cd *simplecobra.Commandeer, r *rootCommand, args []string) error {
+					if len(args) < 1 {
+						return newUserError("theme name needs to be provided")
+					}
 					h, err := r.Hugo(flagsToCfg(cd, nil))
 					if err != nil {
 						return err


Test patch: