-
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 flags toggle-on/toggle-off commands
- Loading branch information
Showing
5 changed files
with
177 additions
and
3 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,104 @@ | ||
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 NewToggleOnCmd(client resources.Client) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Args: validators.Validate(), | ||
Long: "Turn a feature flag on", | ||
RunE: runE(client), | ||
Short: "Turn a feature flag on", | ||
Use: "toggle-on", | ||
} | ||
|
||
cmd.SetUsageTemplate(resourcescmd.OperationUsageTemplate()) | ||
initFlags(cmd) | ||
|
||
return cmd | ||
} | ||
|
||
func NewToggleOffCmd(client resources.Client) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Args: validators.Validate(), | ||
Long: "Turn a feature flag off", | ||
RunE: runE(client), | ||
Short: "Turn a feature flag off", | ||
Use: "toggle-off", | ||
} | ||
|
||
initFlags(cmd) | ||
|
||
return cmd | ||
} | ||
|
||
func runE(client resources.Client) func(*cobra.Command, []string) error { | ||
return func(cmd *cobra.Command, args []string) error { | ||
var toggleOn bool | ||
switch cmd.CalledAs() { | ||
case "toggle-on": | ||
toggleOn = true | ||
case "toggle-off": | ||
toggleOn = false | ||
} | ||
|
||
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(buildPatch(viper.GetString("environment"), toggleOn)), | ||
) | ||
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 initFlags(cmd *cobra.Command) { | ||
cmd.Flags().String(cliflags.EnvironmentFlag, "", "The environment key") | ||
_ = cmd.MarkFlagRequired(cliflags.EnvironmentFlag) | ||
_ = cmd.Flags().SetAnnotation(cliflags.EnvironmentFlag, "required", []string{"true"}) | ||
_ = viper.BindPFlag(cliflags.EnvironmentFlag, cmd.Flags().Lookup(cliflags.EnvironmentFlag)) | ||
|
||
cmd.Flags().String(cliflags.FlagFlag, "", "The feature flag key") | ||
_ = 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)) | ||
} | ||
|
||
func buildPatch(envKey string, toggleValue bool) string { | ||
return fmt.Sprintf(`[{"op": "replace", "path": "/environments/%s/on", "value": %t}]`, envKey, toggleValue) | ||
} |
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,40 @@ | ||
package flags_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
"ldcli/cmd" | ||
"ldcli/internal/analytics" | ||
"ldcli/internal/resources" | ||
) | ||
|
||
func TestToggleOn(t *testing.T) { | ||
mockClient := &resources.MockClient{ | ||
Response: []byte(`{ | ||
"key": "test-flag", | ||
"name": "test flag" | ||
}`), | ||
} | ||
args := []string{ | ||
"flags", "toggle-on", | ||
"--access-token", "abcd1234", | ||
"--environment", "test-env", | ||
"--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": "/environments/test-env/on", "value": true}]`, string(mockClient.Input)) | ||
assert.Equal(t, "Successfully updated test flag (test-flag)\n", string(output)) | ||
} |
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
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
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,20 @@ | ||
package resources | ||
|
||
import "net/url" | ||
|
||
type MockClient struct { | ||
Input []byte | ||
Response []byte | ||
} | ||
|
||
var _ Client = &MockClient{} | ||
|
||
func (c *MockClient) MakeRequest( | ||
accessToken, method, path, contentType string, | ||
query url.Values, | ||
data []byte, | ||
) ([]byte, error) { | ||
c.Input = data | ||
|
||
return c.Response, nil | ||
} |