fix!: make --yes skip the rename confirmation prompt (#30151)

`coder rename` registers `-y/--yes`, but its confirmation asks you to
type the workspace name, and `cliui.Prompt` only honors `-y` for yes/no
prompts.
So `coder rename ws new -y` still blocked on input.

With `-y`, the type-the-name prompt is now skipped.
The data-loss warning is still printed.

**Behavior change:** With `-y`, the rename happens without reading
stdin.
Without `-y`, the prompt is unchanged.

Closes [CLI-35](https://linear.app/codercom/issue/CLI-35)

_This PR was generated by Coder Agents on behalf of @nickvigilante._
This commit is contained in:
Nick Vigilante
2026-10-06 10:08:14 -04:00
committed by GitHub
parent 0e81c4b954
commit 237616a87d
4 changed files with 63 additions and 12 deletions
+20 -11
View File
@@ -16,6 +16,12 @@ func (r *RootCmd) rename() *serpent.Command {
Annotations: workspaceCommand,
Use: "rename <workspace> <new name>",
Short: "Rename a workspace",
Long: FormatExamples(
Example{
Description: "Rename a workspace without a confirmation prompt",
Command: "coder rename <workspace> <new name> --yes",
},
),
Middleware: serpent.Chain(
serpent.RequireNArgs(2),
),
@@ -35,17 +41,20 @@ func (r *RootCmd) rename() *serpent.Command {
pretty.Sprint(cliui.DefaultStyles.Wrap, "WARNING: A rename can result in data loss if a resource references the workspace name in the template (e.g volumes). Please backup any data before proceeding."),
)
_, _ = fmt.Fprintf(inv.Stdout, "See: %s%s\n\n", appearanceConfig.DocsURL, "/templates/resource-persistence#%EF%B8%8F-persistence-pitfalls")
_, err = cliui.Prompt(inv, cliui.PromptOptions{
Text: fmt.Sprintf("Type %q to confirm rename:", workspace.Name),
Validate: func(s string) error {
if s == workspace.Name {
return nil
}
return xerrors.Errorf("Input %q does not match %q", s, workspace.Name)
},
})
if err != nil {
return err
skip, _ := inv.ParsedFlags().GetBool("yes")
if !skip {
_, err = cliui.Prompt(inv, cliui.PromptOptions{
Text: fmt.Sprintf("Type %q to confirm rename:", workspace.Name),
Validate: func(s string) error {
if s == workspace.Name {
return nil
}
return xerrors.Errorf("Input %q does not match %q", s, workspace.Name)
},
})
if err != nil {
return err
}
}
err = client.UpdateWorkspace(inv.Context(), workspace.ID, codersdk.UpdateWorkspaceRequest{
+31 -1
View File
@@ -29,7 +29,7 @@ func TestRename(t *testing.T) {
defer cancel()
want := coderdtest.RandomUsername(t)
inv, root := clitest.New(t, "rename", workspace.Name, want, "--yes")
inv, root := clitest.New(t, "rename", workspace.Name, want)
clitest.SetupConfig(t, member, root)
stdout := expecter.NewAttachedToInvocation(t, inv)
stdin := testutil.NewWriterAttachedToInvocation(t, logger.Named("stdin"), inv)
@@ -45,3 +45,33 @@ func TestRename(t *testing.T) {
got := ws.Name
assert.Equal(t, want, got, "workspace name did not change")
}
func TestRename_SkipsPromptWithYesFlag(t *testing.T) {
t.Parallel()
client := coderdtest.New(t, &coderdtest.Options{IncludeProvisionerDaemon: true, AllowWorkspaceRenames: true})
owner := coderdtest.CreateFirstUser(t, client)
member, _ := coderdtest.CreateAnotherUser(t, client, owner.OrganizationID)
version := coderdtest.CreateTemplateVersion(t, client, owner.OrganizationID, nil)
coderdtest.AwaitTemplateVersionJobCompleted(t, client, version.ID)
template := coderdtest.CreateTemplate(t, client, owner.OrganizationID, version.ID)
workspace := coderdtest.CreateWorkspace(t, member, template.ID)
coderdtest.AwaitWorkspaceBuildJobCompleted(t, client, workspace.LatestBuild.ID)
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
defer cancel()
want := coderdtest.RandomUsername(t)
inv, root := clitest.New(t, "rename", workspace.Name, want, "--yes")
clitest.SetupConfig(t, member, root)
stdout := expecter.NewAttachedToInvocation(t, inv)
clitest.Start(t, inv)
stdout.ExpectMatch(ctx, "renamed to")
ws, err := client.Workspace(ctx, workspace.ID)
assert.NoError(t, err)
got := ws.Name
assert.Equal(t, want, got, "workspace name did not change")
}
+4
View File
@@ -5,6 +5,10 @@ USAGE:
Rename a workspace
- Rename a workspace without a confirmation prompt:
$ coder rename <workspace> <new name> --yes
OPTIONS:
-y, --yes bool
Bypass confirmation prompts.
+8
View File
@@ -14,6 +14,14 @@ Rename a workspace
coder rename [flags] <workspace> <new name>
```
## Description
```console
- Rename a workspace without a confirmation prompt:
$ coder rename <workspace> <new name> --yes
```
## Options
### -y, --yes