Skip to content

Commit

Permalink
chore: Refactor function (#322)
Browse files Browse the repository at this point in the history
Refactor function
  • Loading branch information
dbolson authored Jun 17, 2024
1 parent abc720f commit f24a1ea
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 14 deletions.
18 changes: 11 additions & 7 deletions cmd/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -163,21 +163,21 @@ 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) {
v.Set(key, value)
}
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)
Expand All @@ -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) {
Expand All @@ -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))
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion cmd/flags/archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion cmd/flags/toggle.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 2 additions & 3 deletions cmd/login/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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
Expand All @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/members/invite.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion cmd/resources/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) == "" {
Expand Down
5 changes: 5 additions & 0 deletions internal/output/resource_output.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
`{
Expand Down

0 comments on commit f24a1ea

Please sign in to comment.