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:
1 parent
045492a148
commit
8f0e72821f
1 file changed
+12
-2
@@ -5,6 +5,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"os"
|
"os"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"git.avsdev.uk/Go/job-search/internal/config"
|
"git.avsdev.uk/Go/job-search/internal/config"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
@@ -36,9 +37,13 @@ func commandInit() *cobra.Command {
|
|||||||
// failures: report them without cobra's usage text.
|
// failures: report them without cobra's usage text.
|
||||||
cmd.SilenceUsage = true
|
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 != "" {
|
if cfgFile != "" {
|
||||||
// Use config file from the flag.
|
// Use the config file given.
|
||||||
viper.SetConfigFile(cfgFile)
|
viper.SetConfigFile(cfgFile)
|
||||||
} else {
|
} else {
|
||||||
// Search config in /etc or local directory.
|
// Search config in /etc or local directory.
|
||||||
@@ -76,5 +81,10 @@ func commandInit() *cobra.Command {
|
|||||||
return nil
|
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
|
return cmd
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user