mirror of
https://github.com/coder/coder.git
synced 2026-10-16 14:48:56 +08:00
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:
+20
-11
@@ -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
@@ -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
@@ -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.
|
||||
|
||||
Generated
+8
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user