Files
coder/enterprise/cli/prebuilds.go
T
Spike Curtis 606ae897b7 chore: refactor to directly create Client in Command Handlers (#19760)
Refactors the CLI to create the `*codersdk.Client` in the handlers. This is groundwork for changing the `rootCmd.InitClient()` to use the new `ClientOption`​s.

It also improves variable locality, scoping the Client to the handler. This makes misuse less likely and reduces the memory allocations to just the command being executed, rather than allocating a Client for every command regardless of whether it is executed.
2025-09-22 17:14:07 +04:00

93 lines
2.1 KiB
Go

package cli
import (
"fmt"
"golang.org/x/xerrors"
"github.com/coder/serpent"
"github.com/coder/coder/v2/cli"
"github.com/coder/coder/v2/codersdk"
)
func (r *RootCmd) prebuilds() *serpent.Command {
cmd := &serpent.Command{
Use: "prebuilds",
Short: "Manage Coder prebuilds",
Long: "Administrators can use these commands to manage prebuilt workspace settings.\n" + cli.FormatExamples(
cli.Example{
Description: "Pause Coder prebuilt workspace reconciliation.",
Command: "coder prebuilds pause",
},
cli.Example{
Description: "Resume Coder prebuilt workspace reconciliation if it has been paused.",
Command: "coder prebuilds resume",
},
),
Aliases: []string{"prebuild"},
Handler: func(inv *serpent.Invocation) error {
return inv.Command.HelpHandler(inv)
},
Children: []*serpent.Command{
r.pausePrebuilds(),
r.resumePrebuilds(),
},
}
return cmd
}
func (r *RootCmd) pausePrebuilds() *serpent.Command {
cmd := &serpent.Command{
Use: "pause",
Short: "Pause prebuilds",
Middleware: serpent.Chain(
serpent.RequireNArgs(0),
),
Handler: func(inv *serpent.Invocation) error {
client, err := r.InitClient(inv)
if err != nil {
return err
}
err = client.PutPrebuildsSettings(inv.Context(), codersdk.PrebuildsSettings{
ReconciliationPaused: true,
})
if err != nil {
return xerrors.Errorf("unable to pause prebuilds: %w", err)
}
_, _ = fmt.Fprintln(inv.Stderr, "Prebuilds are now paused.")
return nil
},
}
return cmd
}
func (r *RootCmd) resumePrebuilds() *serpent.Command {
cmd := &serpent.Command{
Use: "resume",
Short: "Resume prebuilds",
Middleware: serpent.Chain(
serpent.RequireNArgs(0),
),
Handler: func(inv *serpent.Invocation) error {
client, err := r.InitClient(inv)
if err != nil {
return err
}
err = client.PutPrebuildsSettings(inv.Context(), codersdk.PrebuildsSettings{
ReconciliationPaused: false,
})
if err != nil {
return xerrors.Errorf("unable to resume prebuilds: %w", err)
}
_, _ = fmt.Fprintln(inv.Stderr, "Prebuilds are now resumed.")
return nil
},
}
return cmd
}