MCPcopy Index your code
hub / github.com/coder/coder / rename

Method rename

cli/rename.go:14–65  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

12)
13
14func (r *RootCmd) rename() *serpent.Command {
15 cmd := &serpent.Command{
16 Annotations: workspaceCommand,
17 Use: "rename <workspace> <new name>",
18 Short: "Rename a workspace",
19 Middleware: serpent.Chain(
20 serpent.RequireNArgs(2),
21 ),
22 Handler: func(inv *serpent.Invocation) error {
23 client, err := r.InitClient(inv)
24 if err != nil {
25 return err
26 }
27 appearanceConfig := initAppearance(inv.Context(), client)
28
29 workspace, err := client.ResolveWorkspace(inv.Context(), inv.Args[0])
30 if err != nil {
31 return xerrors.Errorf("get workspace: %w", err)
32 }
33
34 _, _ = fmt.Fprintf(inv.Stdout, "%s\n\n",
35 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."),
36 )
37 _, _ = fmt.Fprintf(inv.Stdout, "See: %s%s\n\n", appearanceConfig.DocsURL, "/templates/resource-persistence#%EF%B8%8F-persistence-pitfalls")
38 _, err = cliui.Prompt(inv, cliui.PromptOptions{
39 Text: fmt.Sprintf("Type %q to confirm rename:", workspace.Name),
40 Validate: func(s string) error {
41 if s == workspace.Name {
42 return nil
43 }
44 return xerrors.Errorf("Input %q does not match %q", s, workspace.Name)
45 },
46 })
47 if err != nil {
48 return err
49 }
50
51 err = client.UpdateWorkspace(inv.Context(), workspace.ID, codersdk.UpdateWorkspaceRequest{
52 Name: inv.Args[1],
53 })
54 if err != nil {
55 return xerrors.Errorf("rename workspace: %w", err)
56 }
57 _, _ = fmt.Fprintf(inv.Stdout, "Workspace %q renamed to %q\n", workspace.Name, inv.Args[1])
58 return nil
59 },
60 }
61
62 cmd.Options = append(cmd.Options, cliui.SkipPromptOption())
63
64 return cmd
65}

Callers 1

CoreSubcommandsMethod · 0.95

Calls 8

InitClientMethod · 0.95
PromptFunction · 0.92
SkipPromptOptionFunction · 0.92
initAppearanceFunction · 0.85
ResolveWorkspaceMethod · 0.80
ContextMethod · 0.65
UpdateWorkspaceMethod · 0.65
ErrorfMethod · 0.45

Tested by

no test coverage detected