Skip to content

Commit

Permalink
feat: add toggle on and off aliases to update flag (#82)
Browse files Browse the repository at this point in the history
* feat: add toggle on and off aliases to update flag

* Refactor

* Fix flag references
  • Loading branch information
lwonsower authored Apr 2, 2024
1 parent 606520b commit 7b6c6f1
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 3 deletions.
11 changes: 11 additions & 0 deletions cmd/flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
77 changes: 74 additions & 3 deletions cmd/flags/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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)
}

0 comments on commit 7b6c6f1

Please sign in to comment.