Skip to content

Commit

Permalink
feat: add archive flag cmd (#287)
Browse files Browse the repository at this point in the history
* add archive flag cmd

* update descriptions
  • Loading branch information
k3llymariee authored May 15, 2024
1 parent a91de6b commit 1c6f55b
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 0 deletions.
74 changes: 74 additions & 0 deletions cmd/flags/archive.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package flags

import (
"fmt"

"github.com/spf13/cobra"
"github.com/spf13/viper"

"ldcli/cmd/cliflags"
resourcescmd "ldcli/cmd/resources"
"ldcli/cmd/validators"
"ldcli/internal/errors"
"ldcli/internal/output"
"ldcli/internal/resources"
)

func NewArchiveCmd(client resources.Client) *cobra.Command {
cmd := &cobra.Command{
Args: validators.Validate(),
Long: "Archive a flag across all environments. It will only appear in your list when filtered for.",
RunE: makeArchiveRequest(client),
Short: "Archive a feature flag",
Use: "archive",
}

cmd.SetUsageTemplate(resourcescmd.SubcommandUsageTemplate())
initArchiveFlags(cmd)

return cmd
}

func makeArchiveRequest(client resources.Client) func(*cobra.Command, []string) error {
return func(cmd *cobra.Command, args []string) error {

path := fmt.Sprintf(
"%s/api/v2/flags/%s/%s",
viper.GetString(cliflags.BaseURIFlag),
viper.GetString(cliflags.ProjectFlag),
viper.GetString(cliflags.FlagFlag),
)
res, err := client.MakeRequest(
viper.GetString(cliflags.AccessTokenFlag),
"PATCH",
path,
"application/json",
nil,
[]byte(`[{"op": "replace", "path": "/archived", "value": true}]`),
)
if err != nil {
return errors.NewError(output.CmdOutputError(viper.GetString(cliflags.OutputFlag), err))
}

output, err := output.CmdOutput("update", viper.GetString(cliflags.OutputFlag), res)
if err != nil {
return errors.NewError(err.Error())
}

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

return nil
}
}

func initArchiveFlags(cmd *cobra.Command) {
cmd.Flags().String(cliflags.FlagFlag, "", "The feature flag key. The key identifies the flag in your code.")
_ = cmd.MarkFlagRequired(cliflags.FlagFlag)
_ = cmd.Flags().SetAnnotation(cliflags.FlagFlag, "required", []string{"true"})
_ = viper.BindPFlag(cliflags.FlagFlag, cmd.Flags().Lookup(cliflags.FlagFlag))

cmd.Flags().String(cliflags.ProjectFlag, "", "The project key")
_ = cmd.MarkFlagRequired(cliflags.ProjectFlag)
_ = cmd.Flags().SetAnnotation(cliflags.ProjectFlag, "required", []string{"true"})
_ = viper.BindPFlag(cliflags.ProjectFlag, cmd.Flags().Lookup(cliflags.ProjectFlag))
}
60 changes: 60 additions & 0 deletions cmd/flags/archive_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package flags_test

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"ldcli/cmd"
"ldcli/internal/analytics"
"ldcli/internal/resources"
)

func TestArchive(t *testing.T) {
mockClient := &resources.MockClient{
Response: []byte(`{
"key": "test-flag",
"name": "test flag"
}`),
}

t.Run("succeeds with valid inputs", func(t *testing.T) {
args := []string{
"flags", "archive",
"--access-token", "abcd1234",
"--flag", "test-flag",
"--project", "test-proj",
}
output, err := cmd.CallCmd(
t,
cmd.APIClients{
ResourcesClient: mockClient,
},
analytics.NoopClientFn{}.Tracker(),
args,
)

require.NoError(t, err)
assert.Equal(t, `[{"op": "replace", "path": "/archived", "value": true}]`, string(mockClient.Input))
assert.Equal(t, "Successfully updated test flag (test-flag)\n", string(output))
})
t.Run("returns error with missing flags", func(t *testing.T) {
args := []string{
"flags", "archive",
"--access-token", "abcd1234",
"--flag", "test-flag",
}
_, err := cmd.CallCmd(
t,
cmd.APIClients{
ResourcesClient: mockClient,
},
analytics.NoopClientFn{}.Tracker(),
args,
)

assert.Error(t, err)
assert.Equal(t, "required flag(s) \"project\" not set. See `ldcli flags archive --help` for supported flags and usage.", err.Error())
})
}
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ func NewRootCommand(
if c.Name() == "flags" {
c.AddCommand(flagscmd.NewToggleOnCmd(clients.ResourcesClient))
c.AddCommand(flagscmd.NewToggleOffCmd(clients.ResourcesClient))
c.AddCommand(flagscmd.NewArchiveCmd(clients.ResourcesClient))
}
if c.Name() == "members" {
c.AddCommand(memberscmd.NewMembersInviteCmd(clients.ResourcesClient))
Expand Down

0 comments on commit 1c6f55b

Please sign in to comment.