fix error handling

This commit is contained in:
2023-03-29 14:59:04 +02:00
parent 0e572f0aa4
commit 50d25fe7b7
14 changed files with 96 additions and 48 deletions

View File

@@ -33,8 +33,7 @@ func FormCommand(conf *cfg.Config) *cobra.Command {
// errors at this stage do not cause the usage to be shown
//cmd.SilenceUsage = true
if len(args) == 0 {
cmd.Help()
os.Exit(0)
return cmd.Help()
}
return nil
},

View File

@@ -130,13 +130,12 @@ func initConfig(cmd *cobra.Command, cfg *cfg.Config) error {
v.SetEnvPrefix("upctl")
// map flags to viper
bindFlags(cmd, v)
return nil
return bindFlags(cmd, v)
}
// bind flags to viper settings (env+cfgfile)
func bindFlags(cmd *cobra.Command, v *viper.Viper) {
func bindFlags(cmd *cobra.Command, v *viper.Viper) error {
var fail error
cmd.Flags().VisitAll(func(f *pflag.Flag) {
// map flag name to config variable
configName := f.Name
@@ -144,7 +143,11 @@ func bindFlags(cmd *cobra.Command, v *viper.Viper) {
// use config variable if flag is not set and config is set
if !f.Changed && v.IsSet(configName) {
val := v.Get(configName)
cmd.Flags().Set(f.Name, fmt.Sprintf("%v", val))
if err := cmd.Flags().Set(f.Name, fmt.Sprintf("%v", val)); err != nil {
fail = err
}
}
})
return fail
}