Skip to content

Commit

Permalink
feat: output flag all commands (#201)
Browse files Browse the repository at this point in the history
Show plaintext response for all commands with `--output plaintext`
  • Loading branch information
dbolson authored Apr 24, 2024
1 parent b8dc52a commit 1670cae
Show file tree
Hide file tree
Showing 26 changed files with 514 additions and 341 deletions.
5 changes: 4 additions & 1 deletion cmd/cmdtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ import (
"ldcli/internal/analytics"
)

var ValidResponse = `{"valid": true}`
var StubbedSuccessResponse = `{
"key": "test-key",
"name": "test-name"
}`

func CallCmd(
t *testing.T,
Expand Down
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
21 changes: 16 additions & 5 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,18 +64,28 @@ 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(), string(output)+"\n")
fmt.Fprintf(cmd.OutOrStdout(), output+"\n")

return nil
}
Expand Down
5 changes: 2 additions & 3 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 Expand Up @@ -178,7 +177,7 @@ func TestGet(t *testing.T) {
client := environments.MockClient{}
client.
On("Get", mockArgs...).
Return([]byte(cmd.ValidResponse), nil)
Return([]byte(cmd.StubbedSuccessResponse), nil)
clients := cmd.APIClients{
EnvironmentsClient: &client,
}
Expand Down
28 changes: 22 additions & 6 deletions cmd/flags/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import (

"ldcli/cmd/cliflags"
"ldcli/cmd/validators"
"ldcli/internal/errors"
"ldcli/internal/flags"
"ldcli/internal/output"
)

func NewCreateCmd(client flags.Client) (*cobra.Command, error) {
Expand Down Expand Up @@ -52,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 @@ -71,10 +69,28 @@ 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.CmdOutputSingular(
viper.GetString(cliflags.OutputFlag),
response,
output.SingularPlaintextOutputFn,
)
if err != nil {
return errors.NewError(err.Error())
}

fmt.Fprintf(cmd.OutOrStdout(), string(response)+"\n")
fmt.Fprintf(cmd.OutOrStdout(), output+"\n")

return nil
}
Expand Down
14 changes: 8 additions & 6 deletions cmd/flags/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,23 @@ func TestCreate(t *testing.T) {
client := flags.MockClient{}
client.
On("Create", mockArgs...).
Return([]byte(cmd.ValidResponse), nil)
Return([]byte(cmd.StubbedSuccessResponse), nil)
clients := cmd.APIClients{
FlagsClient: &client,
}
args := []string{
"flags", "create",
"--access-token", "testAccessToken",
"--base-uri", "http://test.com",
"--output", "json",
"-d", `{"key": "test-key", "name": "test-name"}`,
"--project", "test-proj-key",
}

output, err := cmd.CallCmd(t, clients, &analytics.NoopClient{}, args)

require.NoError(t, err)
assert.JSONEq(t, `{"valid": true}`, string(output))
assert.JSONEq(t, cmd.StubbedSuccessResponse, string(output))
})

t.Run("with valid flags from environment variables calls API", func(t *testing.T) {
Expand All @@ -50,27 +51,28 @@ func TestCreate(t *testing.T) {
client := flags.MockClient{}
client.
On("Create", mockArgs...).
Return([]byte(cmd.ValidResponse), nil)
Return([]byte(cmd.StubbedSuccessResponse), nil)
clients := cmd.APIClients{
FlagsClient: &client,
}
args := []string{
"flags", "create",
"--output", "json",
"-d", `{"key": "test-key", "name": "test-name"}`,
"--project", "test-proj-key",
}

output, err := cmd.CallCmd(t, clients, &analytics.NoopClient{}, args)

require.NoError(t, err)
assert.JSONEq(t, `{"valid": true}`, string(output))
assert.JSONEq(t, cmd.StubbedSuccessResponse, string(output))
})

t.Run("with an error response is an error", func(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 Expand Up @@ -159,7 +161,7 @@ func TestCreate(t *testing.T) {
client := flags.MockClient{}
client.
On("Create", mockArgs...).
Return([]byte(cmd.ValidResponse), nil)
Return([]byte(cmd.StubbedSuccessResponse), nil)
clients := cmd.APIClients{
FlagsClient: &client,
}
Expand Down
29 changes: 22 additions & 7 deletions cmd/flags/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import (

"ldcli/cmd/cliflags"
"ldcli/cmd/validators"
"ldcli/internal/errors"
"ldcli/internal/flags"
"ldcli/internal/output"
)

func NewGetCmd(client flags.Client) (*cobra.Command, error) {
Expand Down Expand Up @@ -56,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 @@ -70,10 +67,28 @@ 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.CmdOutputSingular(
viper.GetString(cliflags.OutputFlag),
response,
output.SingularPlaintextOutputFn,
)
if err != nil {
return errors.NewError(err.Error())
}

fmt.Fprintf(cmd.OutOrStdout(), string(response)+"\n")
fmt.Fprintf(cmd.OutOrStdout(), output+"\n")

return nil
}
Expand Down
16 changes: 10 additions & 6 deletions cmd/flags/get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,15 @@ func TestGet(t *testing.T) {
client := flags.MockClient{}
client.
On("Get", mockArgs...).
Return([]byte(cmd.ValidResponse), nil)
Return([]byte(cmd.StubbedSuccessResponse), nil)
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 All @@ -41,7 +42,7 @@ func TestGet(t *testing.T) {
output, err := cmd.CallCmd(t, clients, &analytics.NoopClient{}, args)

require.NoError(t, err)
assert.JSONEq(t, `{"valid": true}`, string(output))
assert.JSONEq(t, cmd.StubbedSuccessResponse, string(output))
})

t.Run("with valid flags from environment variables calls API", func(t *testing.T) {
Expand All @@ -50,28 +51,29 @@ func TestGet(t *testing.T) {
client := flags.MockClient{}
client.
On("Get", mockArgs...).
Return([]byte(cmd.ValidResponse), nil)
Return([]byte(cmd.StubbedSuccessResponse), nil)
clients := cmd.APIClients{
FlagsClient: &client,
}
args := []string{
"flags", "get",
"--flag", "test-key",
"--output", "json",
"--project", "test-proj-key",
"--environment", "test-env-key",
}

output, err := cmd.CallCmd(t, clients, &analytics.NoopClient{}, args)

require.NoError(t, err)
assert.JSONEq(t, `{"valid": true}`, string(output))
assert.JSONEq(t, cmd.StubbedSuccessResponse, string(output))
})

t.Run("with an error response is an error", func(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,
}
Expand Down Expand Up @@ -129,13 +131,14 @@ func TestGet(t *testing.T) {
"base-uri",
"environment",
"flag",
"output",
"project",
})

client := flags.MockClient{}
client.
On("Get", mockArgs...).
Return([]byte(cmd.ValidResponse), nil)
Return([]byte(cmd.StubbedSuccessResponse), nil)
clients := cmd.APIClients{
FlagsClient: &client,
}
Expand All @@ -144,6 +147,7 @@ func TestGet(t *testing.T) {
"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
Loading

0 comments on commit 1670cae

Please sign in to comment.