From 7b6c6f1e160598353ab0ed0e93cbc1bd3dd04360 Mon Sep 17 00:00:00 2001 From: Lucy Wonsower Date: Tue, 2 Apr 2024 13:51:52 -0700 Subject: [PATCH] feat: add toggle on and off aliases to update flag (#82) * feat: add toggle on and off aliases to update flag * Refactor * Fix flag references --- cmd/flags/flags.go | 11 +++++++ cmd/flags/update.go | 77 +++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 85 insertions(+), 3 deletions(-) diff --git a/cmd/flags/flags.go b/cmd/flags/flags.go index a32772ac..7e1b6cd7 100644 --- a/cmd/flags/flags.go +++ b/cmd/flags/flags.go @@ -22,8 +22,19 @@ func NewFlagsCmd(client flags.Client) (*cobra.Command, error) { return nil, err } + toggleOnUpdateCmd, err := NewToggleOnUpdateCmd(client) + if err != nil { + return nil, err + } + toggleOffUpdateCmd, err := NewToggleOffUpdateCmd(client) + if err != nil { + return nil, err + } + cmd.AddCommand(createCmd) cmd.AddCommand(updateCmd) + cmd.AddCommand(toggleOnUpdateCmd) + cmd.AddCommand(toggleOffUpdateCmd) return cmd, nil } diff --git a/cmd/flags/update.go b/cmd/flags/update.go index 9d90b2c6..e789ccdf 100644 --- a/cmd/flags/update.go +++ b/cmd/flags/update.go @@ -55,16 +55,83 @@ func NewUpdateCmd(client flags.Client) (*cobra.Command, error) { return cmd, nil } +func NewToggleOnUpdateCmd(client flags.Client) (*cobra.Command, error) { + cmd := &cobra.Command{ + Args: validators.Validate(), + Long: "Turn a flag on", + RunE: runUpdate(client), + Short: "Turn a flag on", + Use: "toggle-on", + } + + return setToggleCommandFlags(cmd) +} + +func NewToggleOffUpdateCmd(client flags.Client) (*cobra.Command, error) { + cmd := &cobra.Command{ + Args: validators.Validate(), + Long: "Turn a flag off", + RunE: runUpdate(client), + Short: "Turn a flag off", + Use: "toggle-off", + } + + return setToggleCommandFlags(cmd) +} + +func setToggleCommandFlags(cmd *cobra.Command) (*cobra.Command, error) { + cmd.Flags().StringP(cliflags.EnvironmentFlag, "e", "", "Environment key") + err := cmd.MarkFlagRequired(cliflags.EnvironmentFlag) + if err != nil { + return nil, err + } + err = viper.BindPFlag(cliflags.EnvironmentFlag, cmd.Flags().Lookup(cliflags.EnvironmentFlag)) + if err != nil { + return nil, err + } + + cmd.Flags().String(cliflags.FlagFlag, "", "Flag key") + err = cmd.MarkFlagRequired(cliflags.FlagFlag) + if err != nil { + return nil, err + } + err = viper.BindPFlag(cliflags.FlagFlag, cmd.Flags().Lookup(cliflags.FlagFlag)) + if err != nil { + return nil, err + } + + cmd.Flags().String(cliflags.ProjectFlag, "", "Project key") + err = cmd.MarkFlagRequired(cliflags.ProjectFlag) + if err != nil { + return nil, err + } + err = viper.BindPFlag(cliflags.ProjectFlag, cmd.Flags().Lookup(cliflags.ProjectFlag)) + if err != nil { + return nil, err + } + + return cmd, nil +} + 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 - err := json.Unmarshal([]byte(viper.GetString(cliflags.DataFlag)), &patch) - if err != nil { - return err + if cmd.CalledAs() == "toggle-on" || cmd.CalledAs() == "toggle-off" { + _ = viper.BindPFlag(cliflags.EnvironmentFlag, cmd.Flags().Lookup(cliflags.EnvironmentFlag)) + err := json.Unmarshal([]byte(buildPatch(viper.GetString(cliflags.EnvironmentFlag), cmd.CalledAs() == "toggle-on")), &patch) + if err != nil { + return err + } + } else { + err := json.Unmarshal([]byte(viper.GetString(cliflags.DataFlag)), &patch) + if err != nil { + return err + } } response, err := client.Update( @@ -84,3 +151,7 @@ func runUpdate(client flags.Client) func(*cobra.Command, []string) error { return nil } } + +func buildPatch(envKey string, toggleValue bool) string { + return fmt.Sprintf(`[{"op": "replace", "path": "/environments/%s/on", "value": %t}]`, envKey, toggleValue) +}