mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
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:
+4
-3
@@ -33,8 +33,6 @@ func (r *RootCmd) createUserStatusCommand(sdkStatus codersdk.UserStatus) *serpen
|
||||
panic(fmt.Sprintf("%s is not supported", sdkStatus))
|
||||
}
|
||||
|
||||
client := new(codersdk.Client)
|
||||
|
||||
var columns []string
|
||||
allColumns := []string{"username", "email", "created at", "status"}
|
||||
cmd := &serpent.Command{
|
||||
@@ -48,9 +46,12 @@ func (r *RootCmd) createUserStatusCommand(sdkStatus codersdk.UserStatus) *serpen
|
||||
),
|
||||
Middleware: serpent.Chain(
|
||||
serpent.RequireNArgs(1),
|
||||
r.InitClient(client),
|
||||
),
|
||||
Handler: func(inv *serpent.Invocation) error {
|
||||
client, err := r.InitClient(inv)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
identifier := inv.Args[0]
|
||||
if identifier == "" {
|
||||
return xerrors.Errorf("user identifier cannot be an empty string")
|
||||
|
||||
Reference in New Issue
Block a user