Skip to content

Commit

Permalink
feat: output flag config cmd (#202)
Browse files Browse the repository at this point in the history
* Show plaintext response for config --list

* Use map for output type

* Refactor types

* Organization

* Remove extra type

* feat: output flag errors (#204)

* Add plain text error handling

* Output flag errors as plain text or JSON

* Remove unused code

* Update all commands to return plaintext or JSON

* Refactor outputter

* Backfill tests

* Reorganize

* Refactor

* Remove comments

* Renamed functions
  • Loading branch information
dbolson authored Apr 24, 2024
1 parent 947cf1d commit 5de73ec
Show file tree
Hide file tree
Showing 25 changed files with 382 additions and 288 deletions.
12 changes: 9 additions & 3 deletions cmd/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"ldcli/cmd/cliflags"
"ldcli/internal/analytics"
"ldcli/internal/config"
"ldcli/internal/output"
)

const (
Expand Down Expand Up @@ -63,11 +64,16 @@ func run() func(*cobra.Command, []string) error {
return err
}

if string(configJSON) == "{}" {
return nil
output, err := output.CmdOutputSingular(
viper.GetString(cliflags.OutputFlag),
configJSON,
output.ConfigPlaintextOutputFn,
)
if err != nil {
return err
}

fmt.Fprint(cmd.OutOrStdout(), string(configJSON)+"\n")
fmt.Fprintf(cmd.OutOrStdout(), output+"\n")
case viper.GetBool(SetFlag):
// flag needs two arguments: a key and value
if len(args)%2 != 0 {
Expand Down
19 changes: 15 additions & 4 deletions cmd/environments/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"ldcli/cmd/cliflags"
"ldcli/cmd/validators"
"ldcli/internal/environments"
"ldcli/internal/errors"
"ldcli/internal/output"
)

Expand Down Expand Up @@ -63,15 +64,25 @@ func runGet(
viper.GetString(cliflags.ProjectFlag),
)
if err != nil {
return err
output, err := output.CmdOutputSingular(
viper.GetString(cliflags.OutputFlag),
[]byte(err.Error()),
output.ErrorPlaintextOutputFn,
)
if err != nil {
return errors.NewError(err.Error())
}

return errors.NewError(output)
}

output, err := output.CmdOutput(
output, err := output.CmdOutputSingular(
viper.GetString(cliflags.OutputFlag),
output.NewSingularOutputterFn(response),
response,
output.SingularPlaintextOutputFn,
)
if err != nil {
return err
return errors.NewError(err.Error())
}

fmt.Fprintf(cmd.OutOrStdout(), output+"\n")
Expand Down
3 changes: 1 addition & 2 deletions cmd/environments/get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,14 @@ func TestGet(t *testing.T) {
client := environments.MockClient{}
client.
On("Get", mockArgs...).
Return([]byte(`{}`), errors.NewError("An error"))
Return([]byte(`{}`), errors.NewError(`{"message": "An error"}`))
clients := cmd.APIClients{
EnvironmentsClient: &client,
}
args := []string{
"environments", "get",
"--access-token", "testAccessToken",
"--base-uri", "http://test.com",
"--output", "json",
"--environment", "test-env",
"--project", "test-proj",
}
Expand Down
23 changes: 15 additions & 8 deletions cmd/flags/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"ldcli/cmd/cliflags"
"ldcli/cmd/validators"
"ldcli/internal/errors"
"ldcli/internal/flags"
"ldcli/internal/output"
)
Expand Down Expand Up @@ -53,10 +54,6 @@ type inputData struct {

func runCreate(client flags.Client) func(*cobra.Command, []string) error {
return func(cmd *cobra.Command, args []string) error {
// rebind flags used in other subcommands
_ = viper.BindPFlag(cliflags.DataFlag, cmd.Flags().Lookup(cliflags.DataFlag))
_ = viper.BindPFlag(cliflags.ProjectFlag, cmd.Flags().Lookup(cliflags.ProjectFlag))

var data inputData
err := json.Unmarshal([]byte(viper.GetString(cliflags.DataFlag)), &data)
if err != nil {
Expand All @@ -72,15 +69,25 @@ func runCreate(client flags.Client) func(*cobra.Command, []string) error {
viper.GetString(cliflags.ProjectFlag),
)
if err != nil {
return err
output, err := output.CmdOutputSingular(
viper.GetString(cliflags.OutputFlag),
[]byte(err.Error()),
output.ErrorPlaintextOutputFn,
)
if err != nil {
return errors.NewError(err.Error())
}

return errors.NewError(output)
}

output, err := output.CmdOutput(
output, err := output.CmdOutputSingular(
viper.GetString(cliflags.OutputFlag),
output.NewSingularOutputterFn(response),
response,
output.SingularPlaintextOutputFn,
)
if err != nil {
return err
return errors.NewError(err.Error())
}

fmt.Fprintf(cmd.OutOrStdout(), output+"\n")
Expand Down
2 changes: 1 addition & 1 deletion cmd/flags/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func TestCreate(t *testing.T) {
client := flags.MockClient{}
client.
On("Create", mockArgs...).
Return([]byte(`{}`), errors.NewError("An error"))
Return([]byte(`{}`), errors.NewError(`{"message": "An error"}`))
clients := cmd.APIClients{
FlagsClient: &client,
}
Expand Down
24 changes: 15 additions & 9 deletions cmd/flags/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"ldcli/cmd/cliflags"
"ldcli/cmd/validators"
"ldcli/internal/errors"
"ldcli/internal/flags"
"ldcli/internal/output"
)
Expand Down Expand Up @@ -57,11 +58,6 @@ func NewGetCmd(client flags.Client) (*cobra.Command, error) {

func runGet(client flags.Client) func(*cobra.Command, []string) error {
return func(cmd *cobra.Command, args []string) error {
// rebind flags used in other subcommands
_ = viper.BindPFlag(cliflags.FlagFlag, cmd.Flags().Lookup(cliflags.FlagFlag))
_ = viper.BindPFlag(cliflags.ProjectFlag, cmd.Flags().Lookup(cliflags.ProjectFlag))
_ = viper.BindPFlag(cliflags.EnvironmentFlag, cmd.Flags().Lookup(cliflags.EnvironmentFlag))

response, err := client.Get(
context.Background(),
viper.GetString(cliflags.AccessTokenFlag),
Expand All @@ -71,15 +67,25 @@ func runGet(client flags.Client) func(*cobra.Command, []string) error {
viper.GetString(cliflags.EnvironmentFlag),
)
if err != nil {
return err
output, err := output.CmdOutputSingular(
viper.GetString(cliflags.OutputFlag),
[]byte(err.Error()),
output.ErrorPlaintextOutputFn,
)
if err != nil {
return errors.NewError(err.Error())
}

return errors.NewError(output)
}

output, err := output.CmdOutput(
output, err := output.CmdOutputSingular(
viper.GetString(cliflags.OutputFlag),
output.NewSingularOutputterFn(response),
response,
output.SingularPlaintextOutputFn,
)
if err != nil {
return err
return errors.NewError(err.Error())
}

fmt.Fprintf(cmd.OutOrStdout(), output+"\n")
Expand Down
3 changes: 1 addition & 2 deletions cmd/flags/get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,14 @@ func TestGet(t *testing.T) {
client := flags.MockClient{}
client.
On("Get", mockArgs...).
Return([]byte(`{}`), errors.NewError("An error"))
Return([]byte(`{}`), errors.NewError(`{"message": "An error"}`))
clients := cmd.APIClients{
FlagsClient: &client,
}
args := []string{
"flags", "get",
"--access-token", "testAccessToken",
"--base-uri", "http://test.com",
"--output", "json",
"--flag", "test-key",
"--project", "test-proj-key",
"--environment", "test-env-key",
Expand Down
24 changes: 15 additions & 9 deletions cmd/flags/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"ldcli/cmd/cliflags"
"ldcli/cmd/validators"
"ldcli/internal/errors"
"ldcli/internal/flags"
"ldcli/internal/output"
)
Expand Down Expand Up @@ -116,11 +117,6 @@ func setToggleCommandFlags(cmd *cobra.Command) (*cobra.Command, error) {

func runUpdate(client flags.Client) func(*cobra.Command, []string) error {
return func(cmd *cobra.Command, args []string) error {
// rebind flags used in other subcommands
_ = viper.BindPFlag(cliflags.DataFlag, cmd.Flags().Lookup(cliflags.DataFlag))
_ = viper.BindPFlag(cliflags.ProjectFlag, cmd.Flags().Lookup(cliflags.ProjectFlag))
_ = viper.BindPFlag(cliflags.FlagFlag, cmd.Flags().Lookup(cliflags.FlagFlag))

var patch []flags.UpdateInput
if cmd.CalledAs() == "toggle-on" || cmd.CalledAs() == "toggle-off" {
_ = viper.BindPFlag(cliflags.EnvironmentFlag, cmd.Flags().Lookup(cliflags.EnvironmentFlag))
Expand All @@ -142,15 +138,25 @@ func runUpdate(client flags.Client) func(*cobra.Command, []string) error {
patch,
)
if err != nil {
return err
output, err := output.CmdOutputSingular(
viper.GetString(cliflags.OutputFlag),
[]byte(err.Error()),
output.ErrorPlaintextOutputFn,
)
if err != nil {
return errors.NewError(err.Error())
}

return errors.NewError(output)
}

output, err := output.CmdOutput(
output, err := output.CmdOutputSingular(
viper.GetString(cliflags.OutputFlag),
output.NewSingularOutputterFn(response),
response,
output.SingularPlaintextOutputFn,
)
if err != nil {
return err
return errors.NewError(err.Error())
}

fmt.Fprintf(cmd.OutOrStdout(), output+"\n")
Expand Down
4 changes: 2 additions & 2 deletions cmd/flags/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func TestUpdate(t *testing.T) {
client := flags.MockClient{}
client.
On("Update", mockArgs...).
Return([]byte(`{}`), errors.NewError("An error"))
Return([]byte(`{}`), errors.NewError(`{"message": "An error"}`))
clients := cmd.APIClients{
FlagsClient: &client,
}
Expand Down Expand Up @@ -207,7 +207,7 @@ func TestToggle(t *testing.T) {
client := flags.MockClient{}
client.
On("Update", mockArgs...).
Return([]byte(`{}`), errors.NewError("An error"))
Return([]byte(`{}`), errors.NewError(`{"message": "An error"}`))
clients := cmd.APIClients{
FlagsClient: &client,
}
Expand Down
21 changes: 16 additions & 5 deletions cmd/members/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"ldcli/cmd/cliflags"
"ldcli/cmd/validators"
"ldcli/internal/errors"
"ldcli/internal/members"
"ldcli/internal/output"
)
Expand Down Expand Up @@ -42,7 +43,7 @@ func runCreate(client members.Client) func(*cobra.Command, []string) error {
// TODO: why does viper.GetString(cliflags.DataFlag) not work?
err := json.Unmarshal([]byte(cmd.Flags().Lookup(cliflags.DataFlag).Value.String()), &data)
if err != nil {
return err
return errors.NewError(err.Error())
}

response, err := client.Create(
Expand All @@ -52,15 +53,25 @@ func runCreate(client members.Client) func(*cobra.Command, []string) error {
data,
)
if err != nil {
return err
output, err := output.CmdOutputSingular(
viper.GetString(cliflags.OutputFlag),
[]byte(err.Error()),
output.ErrorPlaintextOutputFn,
)
if err != nil {
return errors.NewError(err.Error())
}

return errors.NewError(output)
}

output, err := output.CmdOutput(
output, err := output.CmdOutputSingular(
viper.GetString(cliflags.OutputFlag),
output.NewSingularOutputterFn(response),
response,
output.SingularPlaintextOutputFn,
)
if err != nil {
return err
return errors.NewError(err.Error())
}

fmt.Fprintf(cmd.OutOrStdout(), output+"\n")
Expand Down
2 changes: 1 addition & 1 deletion cmd/members/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func TestCreate(t *testing.T) {
client := members.MockClient{}
client.
On("Create", mockArgs...).
Return([]byte(`{}`), errors.NewError("An error"))
Return([]byte(`{}`), errors.NewError(`{"message": "An error"}`))
clients := cmd.APIClients{
MembersClient: &client,
}
Expand Down
19 changes: 15 additions & 4 deletions cmd/members/invite.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"ldcli/cmd/cliflags"
"ldcli/cmd/validators"
"ldcli/internal/errors"
"ldcli/internal/members"
"ldcli/internal/output"
)
Expand Down Expand Up @@ -61,15 +62,25 @@ func runInvite(client members.Client) func(*cobra.Command, []string) error {
memberInputs,
)
if err != nil {
return err
output, err := output.CmdOutputSingular(
viper.GetString(cliflags.OutputFlag),
[]byte(err.Error()),
output.ErrorPlaintextOutputFn,
)
if err != nil {
return errors.NewError(err.Error())
}

return errors.NewError(output)
}

output, err := output.CmdOutput(
output, err := output.CmdOutputMultiple(
viper.GetString(cliflags.OutputFlag),
output.NewSingularOutputterFn(response),
response,
output.MultipleEmailPlaintextOutputFn,
)
if err != nil {
return err
return errors.NewError(err.Error())
}

fmt.Fprintf(cmd.OutOrStdout(), output+"\n")
Expand Down
3 changes: 1 addition & 2 deletions cmd/members/invite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func TestInvite(t *testing.T) {
client := members.MockClient{}
client.
On("Create", mockArgs...).
Return([]byte(`{}`), errors.NewError("An error"))
Return([]byte(`{}`), errors.NewError(`{"message": "An error"}`))
clients := cmd.APIClients{
MembersClient: &client,
}
Expand All @@ -86,7 +86,6 @@ func TestInvite(t *testing.T) {
"invite",
"--access-token", "testAccessToken",
"--base-uri", "http://test.com",
"--output", "json",
"-e", `testemail1@test.com,testemail2@test.com`,
}

Expand Down
Loading

0 comments on commit 5de73ec

Please sign in to comment.