-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add archive flag cmd * update descriptions
- Loading branch information
1 parent
a91de6b
commit 1c6f55b
Showing
3 changed files
with
135 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters