mirror of
https://github.com/coder/coder.git
synced 2026-06-03 13:08:25 +00:00
606ae897b7
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.
70 lines
1.7 KiB
Go
70 lines
1.7 KiB
Go
package cli
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"golang.org/x/xerrors"
|
|
|
|
"github.com/coder/serpent"
|
|
)
|
|
|
|
func (r *RootCmd) favorite() *serpent.Command {
|
|
cmd := &serpent.Command{
|
|
Aliases: []string{"fav", "favou" + "rite"},
|
|
Annotations: workspaceCommand,
|
|
Use: "favorite <workspace>",
|
|
Short: "Add a workspace to your favorites",
|
|
Middleware: serpent.Chain(
|
|
serpent.RequireNArgs(1),
|
|
),
|
|
Handler: func(inv *serpent.Invocation) error {
|
|
client, err := r.InitClient(inv)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
ws, err := namedWorkspace(inv.Context(), client, inv.Args[0])
|
|
if err != nil {
|
|
return xerrors.Errorf("get workspace: %w", err)
|
|
}
|
|
|
|
if err := client.FavoriteWorkspace(inv.Context(), ws.ID); err != nil {
|
|
return xerrors.Errorf("favorite workspace: %w", err)
|
|
}
|
|
_, _ = fmt.Fprintf(inv.Stdout, "Workspace %q added to favorites.\n", ws.Name)
|
|
return nil
|
|
},
|
|
}
|
|
return cmd
|
|
}
|
|
|
|
func (r *RootCmd) unfavorite() *serpent.Command {
|
|
cmd := &serpent.Command{
|
|
Aliases: []string{"unfav", "unfavou" + "rite"},
|
|
Annotations: workspaceCommand,
|
|
Use: "unfavorite <workspace>",
|
|
Short: "Remove a workspace from your favorites",
|
|
Middleware: serpent.Chain(
|
|
serpent.RequireNArgs(1),
|
|
),
|
|
Handler: func(inv *serpent.Invocation) error {
|
|
client, err := r.InitClient(inv)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
ws, err := namedWorkspace(inv.Context(), client, inv.Args[0])
|
|
if err != nil {
|
|
return xerrors.Errorf("get workspace: %w", err)
|
|
}
|
|
|
|
if err := client.UnfavoriteWorkspace(inv.Context(), ws.ID); err != nil {
|
|
return xerrors.Errorf("unfavorite workspace: %w", err)
|
|
}
|
|
_, _ = fmt.Fprintf(inv.Stdout, "Workspace %q removed from favorites.\n", ws.Name)
|
|
return nil
|
|
},
|
|
}
|
|
return cmd
|
|
}
|