`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._
75 lines
2.1 KiB
Go
75 lines
2.1 KiB
Go
package cli
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"golang.org/x/xerrors"
|
|
|
|
"github.com/coder/coder/v2/cli/cliui"
|
|
"github.com/coder/coder/v2/codersdk"
|
|
"github.com/coder/pretty"
|
|
"github.com/coder/serpent"
|
|
)
|
|
|
|
func (r *RootCmd) rename() *serpent.Command {
|
|
cmd := &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),
|
|
),
|
|
Handler: func(inv *serpent.Invocation) error {
|
|
client, err := r.InitClient(inv)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
appearanceConfig := initAppearance(inv.Context(), client)
|
|
|
|
workspace, err := client.ResolveWorkspace(inv.Context(), inv.Args[0])
|
|
if err != nil {
|
|
return xerrors.Errorf("get workspace: %w", err)
|
|
}
|
|
|
|
_, _ = fmt.Fprintf(inv.Stdout, "%s\n\n",
|
|
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")
|
|
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{
|
|
Name: inv.Args[1],
|
|
})
|
|
if err != nil {
|
|
return xerrors.Errorf("rename workspace: %w", err)
|
|
}
|
|
_, _ = fmt.Fprintf(inv.Stdout, "Workspace %q renamed to %q\n", workspace.Name, inv.Args[1])
|
|
return nil
|
|
},
|
|
}
|
|
|
|
cmd.Options = append(cmd.Options, cliui.SkipPromptOption())
|
|
|
|
return cmd
|
|
}
|