diff options
author | Martin Polden <mpolden@mpolden.no> | 2022-02-15 16:38:39 +0100 |
---|---|---|
committer | Martin Polden <mpolden@mpolden.no> | 2022-02-15 19:06:50 +0100 |
commit | 88e4d177ebd95613ed87fe7fa9575aad0656056b (patch) | |
tree | 92a746608c3d237509e8aa502c34a70266cc4c43 /client/go/cmd/config.go | |
parent | 9f2d97647e3813708e929ec2e9d1747454a34bcc (diff) |
Refactor error handling
Diffstat (limited to 'client/go/cmd/config.go')
-rw-r--r-- | client/go/cmd/config.go | 33 |
1 files changed, 15 insertions, 18 deletions
diff --git a/client/go/cmd/config.go b/client/go/cmd/config.go index bd8353cad3f..a195a3e1975 100644 --- a/client/go/cmd/config.go +++ b/client/go/cmd/config.go @@ -46,10 +46,10 @@ instead. Configuration is written to $HOME/.vespa by default. This path can be overridden by setting the VESPA_CLI_HOME environment variable.`, DisableAutoGenTag: true, - Run: func(cmd *cobra.Command, args []string) { - // Root command does nothing - cmd.Help() - exitFunc(1) + SilenceUsage: false, + Args: cobra.MinimumNArgs(1), + RunE: func(cmd *cobra.Command, args []string) error { + return fmt.Errorf("invalid command: %s", args[0]) }, } @@ -58,20 +58,17 @@ var setConfigCmd = &cobra.Command{ Short: "Set a configuration option.", Example: "$ vespa config set target cloud", DisableAutoGenTag: true, + SilenceUsage: true, Args: cobra.ExactArgs(2), - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { cfg, err := LoadConfig() if err != nil { - fatalErr(err, "Could not load config") - return + return err } if err := cfg.Set(args[0], args[1]); err != nil { - fatalErr(err) - } else { - if err := cfg.Write(); err != nil { - fatalErr(err) - } + return err } + return cfg.Write() }, } @@ -82,13 +79,12 @@ var getConfigCmd = &cobra.Command{ $ vespa config get target`, Args: cobra.MaximumNArgs(1), DisableAutoGenTag: true, - Run: func(cmd *cobra.Command, args []string) { + SilenceUsage: true, + RunE: func(cmd *cobra.Command, args []string) error { cfg, err := LoadConfig() if err != nil { - fatalErr(err, "Could not load config") - return + return err } - if len(args) == 0 { // Print all values var flags []string for flag := range flagToConfigBindings { @@ -101,6 +97,7 @@ $ vespa config get target`, } else { printOption(cfg, args[0]) } + return nil }, } @@ -118,11 +115,11 @@ type KeyPair struct { func LoadConfig() (*Config, error) { home, err := vespaCliHome() if err != nil { - return nil, err + return nil, fmt.Errorf("could not detect config directory: %w", err) } c := &Config{Home: home, createDirs: true} if err := c.load(); err != nil { - return nil, err + return nil, fmt.Errorf("could not load config: %w", err) } return c, nil } |