Stacked on #29913. Now that `ResolveWorkspace` forwards `WorkspaceOptions`, narrows the related data loaded by three CLI commands that only use the workspace's own `ID` and `Name`. Each passes an empty `wsrelated.Config{}` so the server skips loading the template and latest build. - `cli/favorite.go`: `favorite` and `unfavorite`. - `cli/autoupdate.go`: `autoupdate`. - `cli/rename.go`: `rename`. These were selected as the safest first candidates: they read only core workspace fields, so no output or behavior changes, only the amount of data fetched. _This PR was created by Coder Agents on behalf of @spikecurtis._
77 lines
2.3 KiB
Go
77 lines
2.3 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/coder/v2/codersdk/wsrelated"
|
|
"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)
|
|
|
|
// Only the workspace ID and name are used.
|
|
workspace, err := client.ResolveWorkspace(inv.Context(), inv.Args[0], codersdk.WorkspaceOptions{IncludeRelated: &wsrelated.Config{}})
|
|
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
|
|
}
|