Files
coder/cli/autoupdate.go
Spike Curtis 5b1eac6d1d feat(cli): narrow ResolveWorkspace related data in favorite, autoupdate, rename (#29914)
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._
2026-10-29 11:07:10 +01:00

62 lines
1.7 KiB
Go

package cli
import (
"fmt"
"strings"
"golang.org/x/xerrors"
"github.com/coder/coder/v2/codersdk"
"github.com/coder/coder/v2/codersdk/wsrelated"
"github.com/coder/serpent"
)
func (r *RootCmd) autoupdate() *serpent.Command {
cmd := &serpent.Command{
Annotations: workspaceCommand,
Use: "autoupdate <workspace> <always|never>",
Short: "Toggle auto-update policy for a workspace",
Middleware: serpent.Chain(
serpent.RequireNArgs(2),
),
Handler: func(inv *serpent.Invocation) error {
client, err := r.InitClient(inv)
if err != nil {
return err
}
policy := strings.ToLower(inv.Args[1])
err = validateAutoUpdatePolicy(policy)
if err != nil {
return xerrors.Errorf("validate policy: %w", err)
}
// 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)
}
err = client.UpdateWorkspaceAutomaticUpdates(inv.Context(), workspace.ID, codersdk.UpdateWorkspaceAutomaticUpdatesRequest{
AutomaticUpdates: codersdk.AutomaticUpdates(policy),
})
if err != nil {
return xerrors.Errorf("update workspace automatic updates policy: %w", err)
}
_, _ = fmt.Fprintf(inv.Stdout, "Updated workspace %q auto-update policy to %q\n", workspace.Name, policy)
return nil
},
}
return cmd
}
func validateAutoUpdatePolicy(arg string) error {
switch codersdk.AutomaticUpdates(arg) {
case codersdk.AutomaticUpdatesAlways, codersdk.AutomaticUpdatesNever:
return nil
default:
return xerrors.Errorf("invalid option %q must be either of %q or %q", arg, codersdk.AutomaticUpdatesAlways, codersdk.AutomaticUpdatesNever)
}
}