Updated config init to check the command arguments instead of using the --config flag (which is now hidden from it)

This commit is contained in:
bwilliams committed 2026-10-13 13:13:11 +01:00
1 parent 045492a148
commit 8f0e72821f
1 file changed
+12 -2
+12 -2
View File
@@ -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
}