diff --git a/cmd/config/config.go b/cmd/config/config.go index 2a6bcbe6..7430b860 100644 --- a/cmd/config/config.go +++ b/cmd/config/config.go @@ -133,7 +133,7 @@ func run(service config.Service) func(*cobra.Command, []string) error { rawConfig, v, err := getRawConfig() if err != nil { - return errs.NewError(output.CmdOutputError(viper.GetString(cliflags.OutputFlag), err)) + return newCmdErr(err) } // add arg pairs to config where each argument is --set arg1 val1 --set arg2 val2 @@ -163,13 +163,13 @@ func run(service config.Service) func(*cobra.Command, []string) error { errorMessage += errs.AccessTokenInvalidErrMessage(viper.GetString(cliflags.BaseURIFlag)) err := errors.New(errorMessage) - return errs.NewError(output.CmdOutputError(viper.GetString(cliflags.OutputFlag), err)) + return newCmdErr(err) } } configFile, err := config.NewConfig(rawConfig) if err != nil { - return errs.NewError(output.CmdOutputError(viper.GetString(cliflags.OutputFlag), err)) + return newCmdErr(err) } setKeyFn := func(key string, value interface{}, v *viper.Viper) { @@ -177,7 +177,7 @@ func run(service config.Service) func(*cobra.Command, []string) error { } err = writeConfig(configFile, v, setKeyFn) if err != nil { - return errs.NewError(output.CmdOutputError(viper.GetString(cliflags.OutputFlag), err)) + return newCmdErr(err) } output, err := outputSetAction(newFields) @@ -194,7 +194,7 @@ func run(service config.Service) func(*cobra.Command, []string) error { config, v, err := getConfig() if err != nil { - return errs.NewError(output.CmdOutputError(viper.GetString(cliflags.OutputFlag), err)) + return newCmdErr(err) } unsetKeyFn := func(key string, value interface{}, v *viper.Viper) { @@ -204,7 +204,7 @@ func run(service config.Service) func(*cobra.Command, []string) error { } err = writeConfig(config, v, unsetKeyFn) if err != nil { - return errs.NewError(output.CmdOutputError(viper.GetString(cliflags.OutputFlag), err)) + return newCmdErr(err) } output, err := outputUnsetAction(viper.GetString(UnsetFlag)) @@ -327,7 +327,11 @@ func newErr(flag string) error { ), ) - return errs.NewError(output.CmdOutputError(viper.GetString(cliflags.OutputFlag), err)) + return newCmdErr(err) +} + +func newCmdErr(err error) error { + return output.NewCmdOutputError(err, viper.GetString(cliflags.OutputFlag)) } func writeAlphabetizedFlags(sb *strings.Builder) { diff --git a/cmd/flags/archive.go b/cmd/flags/archive.go index b3b2a20d..48d79483 100644 --- a/cmd/flags/archive.go +++ b/cmd/flags/archive.go @@ -48,7 +48,7 @@ func makeArchiveRequest(client resources.Client) func(*cobra.Command, []string) false, ) if err != nil { - return errors.NewError(output.CmdOutputError(viper.GetString(cliflags.OutputFlag), err)) + return output.NewCmdOutputError(err, viper.GetString(cliflags.OutputFlag)) } output, err := output.CmdOutput("update", viper.GetString(cliflags.OutputFlag), res) diff --git a/cmd/flags/toggle.go b/cmd/flags/toggle.go index 352ae451..926800d4 100644 --- a/cmd/flags/toggle.go +++ b/cmd/flags/toggle.go @@ -69,7 +69,7 @@ func runE(client resources.Client) func(*cobra.Command, []string) error { false, ) if err != nil { - return errors.NewError(output.CmdOutputError(viper.GetString(cliflags.OutputFlag), err)) + return output.NewCmdOutputError(err, viper.GetString(cliflags.OutputFlag)) } output, err := output.CmdOutput("update", viper.GetString(cliflags.OutputFlag), res) diff --git a/cmd/login/login.go b/cmd/login/login.go index 1f2e5e1b..4cc3fd1f 100644 --- a/cmd/login/login.go +++ b/cmd/login/login.go @@ -10,7 +10,6 @@ import ( cmdAnalytics "github.com/launchdarkly/ldcli/cmd/analytics" "github.com/launchdarkly/ldcli/cmd/cliflags" "github.com/launchdarkly/ldcli/internal/analytics" - "github.com/launchdarkly/ldcli/internal/errors" "github.com/launchdarkly/ldcli/internal/login" "github.com/launchdarkly/ldcli/internal/output" ) @@ -62,7 +61,7 @@ func run(client login.Client) func(*cobra.Command, []string) error { viper.GetString(cliflags.BaseURIFlag), ) if err != nil { - return errors.NewError(output.CmdOutputError(viper.GetString(cliflags.OutputFlag), err)) + return output.NewCmdOutputError(err, viper.GetString(cliflags.OutputFlag)) } var b strings.Builder @@ -76,7 +75,7 @@ func run(client login.Client) func(*cobra.Command, []string) error { ), ) - fmt.Fprintf(cmd.OutOrStdout(), b.String()) + fmt.Fprintln(cmd.OutOrStdout(), b.String()) return nil } diff --git a/cmd/members/invite.go b/cmd/members/invite.go index a1299370..ae2c6d11 100644 --- a/cmd/members/invite.go +++ b/cmd/members/invite.go @@ -59,7 +59,7 @@ func runE(client resources.Client) func(*cobra.Command, []string) error { false, ) if err != nil { - return errors.NewError(output.CmdOutputError(viper.GetString(cliflags.OutputFlag), err)) + return output.NewCmdOutputError(err, viper.GetString(cliflags.OutputFlag)) } output, err := output.CmdOutput("update", viper.GetString(cliflags.OutputFlag), res) diff --git a/cmd/resources/resources.go b/cmd/resources/resources.go index b354d02b..6a12584a 100644 --- a/cmd/resources/resources.go +++ b/cmd/resources/resources.go @@ -339,7 +339,7 @@ func (op *OperationCmd) makeRequest(cmd *cobra.Command, args []string) error { op.IsBeta, ) if err != nil { - return errors.NewError(output.CmdOutputError(viper.GetString(cliflags.OutputFlag), err)) + return output.NewCmdOutputError(err, viper.GetString(cliflags.OutputFlag)) } if string(res) == "" { diff --git a/internal/output/resource_output.go b/internal/output/resource_output.go index 7e3d59c7..a6dc68f1 100644 --- a/internal/output/resource_output.go +++ b/internal/output/resource_output.go @@ -136,6 +136,11 @@ func CmdOutputError(outputKind string, err error) string { return ErrorPlaintextOutputFn(r) } +// NewCmdOutputError builds error output based on the error and output kind. +func NewCmdOutputError(err error, outputKind string) error { + return errs.NewError(CmdOutputError(outputKind, err)) +} + func errJSON(s string) string { return fmt.Sprintf( `{