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.
This commit is contained in:
Spike Curtis
2025-09-22 17:14:07 +04:00
committed by GitHub
parent 12496830d6
commit 606ae897b7
67 changed files with 605 additions and 431 deletions
+4 -2
View File
@@ -23,13 +23,11 @@ func (r *RootCmd) templatePull() *serpent.Command {
orgContext = NewOrganizationContext()
)
client := new(codersdk.Client)
cmd := &serpent.Command{
Use: "pull <name> [destination]",
Short: "Download the active, latest, or specified version of a template to a path.",
Middleware: serpent.Chain(
serpent.RequireRangeArgs(1, 2),
r.InitClient(client),
),
Handler: func(inv *serpent.Invocation) error {
var (
@@ -37,6 +35,10 @@ func (r *RootCmd) templatePull() *serpent.Command {
templateName = inv.Args[0]
dest string
)
client, err := r.InitClient(inv)
if err != nil {
return err
}
if len(inv.Args) > 1 {
dest = inv.Args[1]