Repository navigation
feat: Print an undo command after a flag change #123
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
0xvikram
wants to merge
2
commits into
Flagsmith:main
Choose a base branch
from
0xvikram:feat/undo-hints
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,152 @@ | ||
| package cmd | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "math" | ||
| "regexp" | ||
| "strconv" | ||
| "strings" | ||
|
|
||
| "github.com/spf13/cobra" | ||
|
|
||
| "github.com/Flagsmith/flagsmith-cli/v2/internal/api" | ||
| ) | ||
|
|
||
| // undoFlagMutation returns the command that reverts m, given the state it | ||
| // changed (before is nil when m creates a segment override). It returns false | ||
| // when the previous state can't be restated as a command — an unset value, or | ||
| // re-weighted variants — so that no hint is better than a wrong one. | ||
| func undoFlagMutation(cmd *cobra.Command, name string, segmentID int, before *api.FeatureState, prevPriority int, m flagMutation) (string, bool) { | ||
| var target []string | ||
| if segmentID != 0 { | ||
| target = []string{"--segment", strconv.Itoa(segmentID)} | ||
| } | ||
| scope := scopeArgs(cmd, target...) | ||
| if segmentID != 0 && before == nil { | ||
| return deleteOverride(name, scope), true | ||
| } | ||
| if before == nil || len(m.weights) > 0 { | ||
| return "", false | ||
| } | ||
| var priority *int | ||
| if m.setPriority && prevPriority != m.priority { | ||
| priority = &prevPriority | ||
| } | ||
| return revertArgs(name, scope, before.Enabled, before.Value, m, priority) | ||
| } | ||
|
|
||
| // undoIdentityMutation is undoFlagMutation for an identity override: before is | ||
| // nil when the update creates the override, which is then undone by deleting | ||
| // it. (A core identity created along the way stays; only its override goes.) | ||
| func undoIdentityMutation(cmd *cobra.Command, name, identifier string, before *api.IdentityFeatureState, m flagMutation) (string, bool) { | ||
| scope := scopeArgs(cmd, "--identifier", shellQuote(identifier)) | ||
| if before == nil { | ||
| return deleteOverride(name, scope), true | ||
| } | ||
| return revertArgs(name, scope, before.Enabled, before.Value, m, nil) | ||
| } | ||
|
|
||
| func deleteOverride(name string, scope []string) string { | ||
| return strings.Join(append([]string{"flagsmith", "flag", "delete", shellQuote(name)}, scope...), " ") | ||
| } | ||
|
|
||
| // revertArgs builds the `flag update` that puts back what m changed: the | ||
| // enabled state, the value and the priority (nil when unchanged). Properties m | ||
| // sets to what they already were are left out; if nothing is left, there is | ||
| // nothing to undo. | ||
| func revertArgs(name string, scope []string, enabled bool, value any, m flagMutation, priority *int) (string, bool) { | ||
| args := []string{"flagsmith", "flag", "update", shellQuote(name)} | ||
| args = append(args, scope...) | ||
| if (m.enable || m.disable) && enabled != m.enable { | ||
| if enabled { | ||
| args = append(args, "--enable") | ||
| } else { | ||
| args = append(args, "--disable") | ||
| } | ||
| } | ||
| if m.setValue && !sameAsRequested(value) { | ||
| restore, ok := valueArgs(value) | ||
| if !ok { | ||
| return "", false | ||
| } | ||
| args = append(args, restore...) | ||
| } | ||
| if priority != nil { | ||
| args = append(args, "--priority", strconv.Itoa(*priority)) | ||
| } | ||
| if len(args) == 4+len(scope) { | ||
| return "", false // nothing changed that needs reverting | ||
| } | ||
| return strings.Join(args, " "), true | ||
| } | ||
|
|
||
| // sameAsRequested reports whether the stored value v already equals the | ||
| // --value (and --type) being written, compared as the arguments that would | ||
| // restore it. | ||
| func sameAsRequested(v any) bool { | ||
| fv, err := inferFeatureValue(flagValueFlag, flagTypeFlag) | ||
| if err != nil { | ||
| return false | ||
| } | ||
| requested, err := nativeScalar(fv) | ||
| if err != nil { | ||
| return false | ||
| } | ||
| have, okHave := valueArgs(v) | ||
| want, okWant := valueArgs(requested) | ||
| return okHave && okWant && strings.Join(have, " ") == strings.Join(want, " ") | ||
| } | ||
|
|
||
| // scopeArgs repeats the context the user gave explicitly — API host, | ||
| // organisation, project, environment — followed by target (the override), so | ||
| // the undo command hits the same flag from any directory. | ||
| func scopeArgs(cmd *cobra.Command, target ...string) []string { | ||
| var args []string | ||
| for _, name := range []string{"api-url", "organisation", "project", "environment"} { | ||
| if f := cmd.Flags().Lookup(name); f != nil && f.Changed { | ||
| args = append(args, "--"+name, shellQuote(f.Value.String())) | ||
| } | ||
| } | ||
| return append(args, target...) | ||
| } | ||
|
|
||
| // valueArgs restates a stored flag value as --value (and --type when inference | ||
| // would read it differently). A flag with no value can't be restored this way. | ||
| func valueArgs(v any) ([]string, bool) { | ||
| switch v := v.(type) { | ||
| case bool: | ||
| return []string{"--value", strconv.FormatBool(v)}, true | ||
| case float64: | ||
| if v != math.Trunc(v) || math.Abs(v) > 1<<53 { | ||
| return nil, false | ||
| } | ||
| return []string{"--value", strconv.FormatInt(int64(v), 10)}, true | ||
| case int: | ||
| return []string{"--value", strconv.Itoa(v)}, true | ||
| case string: | ||
| args := []string{"--value", shellQuote(v)} | ||
| if inferred, _ := inferFeatureValue(v, ""); inferred.Type != "string" { | ||
| args = append(args, "--type", "string") | ||
| } | ||
| return args, true | ||
| default: | ||
| return nil, false | ||
| } | ||
| } | ||
|
|
||
| var shellSafe = regexp.MustCompile(`^[A-Za-z0-9_./:@%+=,-]+$`) | ||
|
|
||
| // shellQuote returns s ready to paste into a POSIX shell. | ||
| func shellQuote(s string) string { | ||
| if shellSafe.MatchString(s) { | ||
| return s | ||
| } | ||
| return "'" + strings.ReplaceAll(s, "'", `'\''`) + "'" | ||
| } | ||
|
|
||
| // printUndo writes the undo hint after a successful mutation. | ||
| func printUndo(cmd *cobra.Command, undo string, ok bool) { | ||
| if ok { | ||
| fmt.Fprintf(cmd.ErrOrStderr(), " Undo: %s\n", undo) | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.