Skip to content

feat: Print an undo command after a flag change - #123

Open
0xvikram wants to merge 1 commit into
Flagsmith:mainfrom
0xvikram:feat/undo-hints
Open

0xvikram wants to merge 1 commit into
Flagsmith:mainfrom
0xvikram:feat/undo-hints

Conversation

@0xvikram

Copy link
Copy Markdown

Part of #65: hint an undo command after a successful mutation.

After flag update, flag enable or flag disable succeeds, the CLI prints the command that puts the flag back, on stderr under the confirmation:

✓ Set undo-hint-check to 30 in environment Production (dtALjtDbbZmK8BVDXk5a8f)
  Undo: flagsmith flag update undo-hint-check --value 25
✓ Enabled undo-hint-check in environment Production (dtALjtDbbZmK8BVDXk5a8f)
  Undo: flagsmith flag update undo-hint-check --disable
  • The undo is worked out from the state the command already reads before the write, so it costs no extra request.
  • Segment overrides: an edit restores the previous enabled state, value or priority with --segment <id>; a newly created override is undone with flagsmith flag delete <feature> --segment <id>.
  • An explicit --project / --environment is repeated, so the command works from any directory. Values are shell-quoted, and a string that would be inferred as another type gets --type string.
  • No hint when the previous state can't be restated as a command (an unset value, re-weighted variants) or when nothing changed. No hint is better than a wrong one.

Not in this PR: the first half of #65 (showing a diff when confirming). That needs a decision on the format, so I left it for a follow-up.

Verified

  • TestFlagUpdateUndoHint (9 cases, including a failed update and the no-hint cases) and TestUndoValueArgs. The hint cases fail without the change.
  • go vet ./... and go test -race ./... pass.
  • On Flagsmith Cloud with a throwaway feature: set a value, enabled it, ran the printed undo, and the flag was back to its previous state.

This contribution was prepared with AI coding assistance.

After flag update, flag enable or flag disable succeeds, print the command
that puts the flag back, on stderr under the confirmation:

  ✓ Set max_items to 30 in environment Production (…)
    Undo: flagsmith flag update max_items --value 25

It is worked out from the state read before the write. A new segment override
is undone with flag delete --segment. An explicit --project or --environment
is repeated so the command works from anywhere. No hint is printed when the
previous state can't be restated as a command (an unset value, re-weighted
variants) or when nothing changed.

Part of Flagsmith#65.
@0xvikram
0xvikram requested a review from a team as a code owner November 27, 2026 09:02
@0xvikram
0xvikram requested review from a team and emyller and a balanced review from Copilot and removed request for a team November 27, 2026 09:02

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

5 issues found across 3 files

Confidence score: 3/5

  • Copied undo commands in undo.go can target the default host or fail to resolve a project when the original command used explicit context flags. Preserve --api-url and --organisation in the undo command.
  • The --identifier update path in flag_update.go returns before printing an undo command, so successful identity changes cannot be reversed from the hint. Add undo generation to that path, including cleanup for newly created identities.
  • flag_update.go can print an undo hint for a no-op update, and that hint repeats the current value. Suppress it when the requested value matches target.Value.
  • The new test in cmd_test.go can pass without confirming the hint is on stdout, and it is missing the repository's Given/When/Then sections. Capture stdout and stderr separately and add those sections.
Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="internal/cmd/cmd_test.go">

<violation number="1" location="internal/cmd/cmd_test.go:8861">
P3: This assertion accepts the hint on stdout or before the confirmation because `run` combines both streams and `undoLine` searches the combined buffer. Capture stdout and stderr separately, then assert the hint is on stderr after the confirmation.</violation>

<violation number="2" location="internal/cmd/cmd_test.go:8879">
P3: Add the required Given/When/Then sections to this new test so it follows the repository's test structure.</violation>
</file>

<file name="internal/cmd/flag_update.go">

<violation number="1" location="internal/cmd/flag_update.go:298">
P2: Setting a value to its current value still prints an undo hint that repeats the current value, despite nothing changing. Suppress the hint when the requested value matches `target.Value`.</violation>

<violation number="2" location="internal/cmd/flag_update.go:298">
P2: Successful `--identifier` updates return before reaching this hook, so they still print no undo command. Add equivalent undo generation and printing to the identity update path, including deleting newly created identity overrides.</violation>
</file>

<file name="internal/cmd/undo.go">

<violation number="1" location="internal/cmd/undo.go:57">
P2: `scopeArgs` drops explicit `--api-url` and `--organisation`, so a copied undo can hit the default host or fail to resolve a named project across multiple organisations. Repeat those explicit context flags too.</violation>
</file>

Reply to a comment to ask cubic a question or push back. It learns from your replies.

Re-trigger cubic

}

// Worked out from the state before the write, printed once it succeeds.
undo, undoOK := undoFlagMutation(cmd, name, segmentID, target, meta.priority, m)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: Setting a value to its current value still prints an undo hint that repeats the current value, despite nothing changing. Suppress the hint when the requested value matches target.Value.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At internal/cmd/flag_update.go, line 298:

<comment>Setting a value to its current value still prints an undo hint that repeats the current value, despite nothing changing. Suppress the hint when the requested value matches `target.Value`.</comment>

<file context>
@@ -294,6 +294,9 @@ func applyFlagMutation(cmd *cobra.Command, name string, m flagMutation) error {
 	}
 
+	// Worked out from the state before the write, printed once it succeeds.
+	undo, undoOK := undoFlagMutation(cmd, name, segmentID, target, meta.priority, m)
+
 	resp, err := cred.client().UpdateFlag(cmd.Context(), env.APIKey, feature.ID, req)
</file context>

}

// Worked out from the state before the write, printed once it succeeds.
undo, undoOK := undoFlagMutation(cmd, name, segmentID, target, meta.priority, m)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: Successful --identifier updates return before reaching this hook, so they still print no undo command. Add equivalent undo generation and printing to the identity update path, including deleting newly created identity overrides.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At internal/cmd/flag_update.go, line 298:

<comment>Successful `--identifier` updates return before reaching this hook, so they still print no undo command. Add equivalent undo generation and printing to the identity update path, including deleting newly created identity overrides.</comment>

<file context>
@@ -294,6 +294,9 @@ func applyFlagMutation(cmd *cobra.Command, name string, m flagMutation) error {
 	}
 
+	// Worked out from the state before the write, printed once it succeeds.
+	undo, undoOK := undoFlagMutation(cmd, name, segmentID, target, meta.priority, m)
+
 	resp, err := cred.client().UpdateFlag(cmd.Context(), env.APIKey, feature.ID, req)
</file context>

Comment thread internal/cmd/undo.go
// hits the same project, environment and override from any directory.
func scopeArgs(cmd *cobra.Command, segmentID int) []string {
var args []string
for _, name := range []string{"project", "environment"} {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: scopeArgs drops explicit --api-url and --organisation, so a copied undo can hit the default host or fail to resolve a named project across multiple organisations. Repeat those explicit context flags too.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At internal/cmd/undo.go, line 57:

<comment>`scopeArgs` drops explicit `--api-url` and `--organisation`, so a copied undo can hit the default host or fail to resolve a named project across multiple organisations. Repeat those explicit context flags too.</comment>

<file context>
@@ -0,0 +1,107 @@
+// hits the same project, environment and override from any directory.
+func scopeArgs(cmd *cobra.Command, segmentID int) []string {
+	var args []string
+	for _, name := range []string{"project", "environment"} {
+		if f := cmd.Flags().Lookup(name); f != nil && f.Changed {
+			args = append(args, "--"+name, shellQuote(f.Value.String()))
</file context>
Suggested change
for _, name := range []string{"project", "environment"} {
for _, name := range []string{"api-url", "organisation", "project", "environment"} {

Comment thread internal/cmd/cmd_test.go
})
}

func TestUndoValueArgs(t *testing.T) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P3: Add the required Given/When/Then sections to this new test so it follows the repository's test structure.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At internal/cmd/cmd_test.go, line 8879:

<comment>Add the required Given/When/Then sections to this new test so it follows the repository's test structure.</comment>

<file context>
@@ -8770,3 +8770,132 @@ func TestRefreshPersistsToKeychain(t *testing.T) {
+	})
+}
+
+func TestUndoValueArgs(t *testing.T) {
+	cases := []struct {
+		in   any
</file context>

Comment thread internal/cmd/cmd_test.go
if err != nil {
t.Fatalf("%v: %v\noutput: %s", tc.args, err, out)
}
if got := undoLine(out); got != tc.want {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P3: This assertion accepts the hint on stdout or before the confirmation because run combines both streams and undoLine searches the combined buffer. Capture stdout and stderr separately, then assert the hint is on stderr after the confirmation.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At internal/cmd/cmd_test.go, line 8861:

<comment>This assertion accepts the hint on stdout or before the confirmation because `run` combines both streams and `undoLine` searches the combined buffer. Capture stdout and stderr separately, then assert the hint is on stderr after the confirmation.</comment>

<file context>
@@ -8770,3 +8770,132 @@ func TestRefreshPersistsToKeychain(t *testing.T) {
+			if err != nil {
+				t.Fatalf("%v: %v\noutput: %s", tc.args, err, out)
+			}
+			if got := undoLine(out); got != tc.want {
+				t.Errorf("undo = %q, want %q\noutput: %s", got, tc.want, out)
+			}
</file context>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants