diff --git a/internal/cli/config/init.go b/internal/cli/config/init.go index 83f3301..c27bf9c 100644 --- a/internal/cli/config/init.go +++ b/internal/cli/config/init.go @@ -5,6 +5,7 @@ import ( "fmt" "log/slog" "os" + "strings" "git.avsdev.uk/Go/job-search/internal/config" "github.com/spf13/cobra" @@ -36,9 +37,13 @@ func commandInit() *cobra.Command { // failures: report them without cobra's usage text. cmd.SilenceUsage = true - cfgFile, _ := cmd.Flags().GetString("config") + // With no path, the default locations are used. + cfgFile := "" + if len(args) > 0 { + cfgFile = strings.TrimSpace(args[0]) + } if cfgFile != "" { - // Use config file from the flag. + // Use the config file given. viper.SetConfigFile(cfgFile) } else { // Search config in /etc or local directory. @@ -76,5 +81,10 @@ func commandInit() *cobra.Command { return nil }, } + // Config flag should be hidden as the config path should be provided + cmd.SetHelpFunc(func(command *cobra.Command, strings []string) { + command.Flags().MarkHidden("config") //nolint:errcheck,gosec // a root flag, so it always exists + command.Parent().HelpFunc()(command, strings) + }) return cmd }