Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

chore: refactor to directly create Client in Command Handlers#19760

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
spikecurtis merged 1 commit intomainfromspike/init-client-directly
Sep 22, 2025
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletionscli/autoupdate.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -12,18 +12,21 @@ import (
)

func (r *RootCmd) autoupdate() *serpent.Command {
client := new(codersdk.Client)
cmd := &serpent.Command{
Annotations: workspaceCommand,
Use: "autoupdate <workspace> <always|never>",
Short: "Toggle auto-update policy for a workspace",
Middleware: serpent.Chain(
serpent.RequireNArgs(2),
r.InitClient(client),
),
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)
err = validateAutoUpdatePolicy(policy)
if err != nil {
return xerrors.Errorf("validate policy: %w", err)
}
Expand Down
8 changes: 5 additions & 3 deletionscli/configssh.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -236,7 +236,6 @@ func (r *RootCmd) configSSH() *serpent.Command {
dryRun bool
coderCliPath string
)
client := new(codersdk.Client)
cmd := &serpent.Command{
Annotations: workspaceCommand,
Use: "config-ssh",
Expand All@@ -253,9 +252,13 @@ func (r *RootCmd) configSSH() *serpent.Command {
),
Middleware: serpent.Chain(
serpent.RequireNArgs(0),
r.InitClient(client),
),
Handler: func(inv *serpent.Invocation) error {
client, err := r.InitClient(inv)
if err != nil {
return err
}

ctx := inv.Context()

if sshConfigOpts.waitEnum != "auto" && sshConfigOpts.skipProxyCommand {
Expand All@@ -280,7 +283,6 @@ func (r *RootCmd) configSSH() *serpent.Command {
out = inv.Stderr
}

var err error
coderBinary := coderCliPath
if coderBinary == "" {
coderBinary, err = currentBinPath(out)
Expand Down
8 changes: 5 additions & 3 deletionscli/create.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -50,7 +50,6 @@ func (r *RootCmd) Create(opts CreateOptions) *serpent.Command {
// shares the same name across multiple organizations.
orgContext = NewOrganizationContext()
)
client := new(codersdk.Client)
cmd := &serpent.Command{
Annotations: workspaceCommand,
Use: "create [workspace]",
Expand All@@ -61,9 +60,12 @@ func (r *RootCmd) Create(opts CreateOptions) *serpent.Command {
Command: "coder create <username>/<workspace_name>",
},
),
Middleware: serpent.Chain(r.InitClient(client)),
Handler: func(inv *serpent.Invocation) error {
var err error
client, err := r.InitClient(inv)
if err != nil {
return err
}

workspaceOwner := codersdk.Me
if len(inv.Args) >= 1 {
workspaceOwner, workspaceName, err = splitNamedWorkspace(inv.Args[0])
Expand Down
7 changes: 5 additions & 2 deletionscli/delete.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -16,7 +16,6 @@ func (r *RootCmd) deleteWorkspace() *serpent.Command {
orphan bool
prov buildFlags
)
client := new(codersdk.Client)
cmd := &serpent.Command{
Annotations: workspaceCommand,
Use: "delete <workspace>",
Expand All@@ -29,9 +28,13 @@ func (r *RootCmd) deleteWorkspace() *serpent.Command {
),
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
}

workspace, err := namedWorkspace(inv.Context(), client, inv.Args[0])
if err != nil {
return err
Expand Down
9 changes: 5 additions & 4 deletionscli/exp_mcp.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -399,7 +399,6 @@ type mcpServer struct {

func (r *RootCmd) mcpServer() *serpent.Command {
var (
client = new(codersdk.Client)
instructions string
allowedTools []string
appStatusSlug string
Expand All@@ -409,6 +408,11 @@ func (r *RootCmd) mcpServer() *serpent.Command {
cmd := &serpent.Command{
Use: "server",
Handler: func(inv *serpent.Invocation) error {
client, err := r.TryInitClient(inv)
if err != nil {
return err
}

var lastReport taskReport
// Create a queue that skips duplicates and preserves summaries.
queue := cliutil.NewQueue[taskReport](512).WithPredicate(func(report taskReport) (taskReport, bool) {
Expand DownExpand Up@@ -548,9 +552,6 @@ func (r *RootCmd) mcpServer() *serpent.Command {
return srv.startServer(ctx, inv, instructions, allowedTools)
},
Short: "Start the Coder MCP server.",
Middleware: serpent.Chain(
r.TryInitClient(client),
),
Options: []serpent.Option{
{
Name: "instructions",
Expand Down
10 changes: 5 additions & 5 deletionscli/exp_rpty.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -22,24 +22,24 @@ import (
)

func (r *RootCmd) rptyCommand() *serpent.Command {
var (
client = new(codersdk.Client)
args handleRPTYArgs
)
var args handleRPTYArgs

cmd := &serpent.Command{
Handler: func(inv *serpent.Invocation) error {
if r.disableDirect {
return xerrors.New("direct connections are disabled, but you can try websocat ;-)")
}
client, err := r.InitClient(inv)
if err != nil {
return err
}
args.NamedWorkspace = inv.Args[0]
args.Command = inv.Args[1:]
return handleRPTY(inv, client, args)
},
Long: "Establish an RPTY session with a workspace/agent. This uses the same mechanism as the Web Terminal.",
Middleware: serpent.Chain(
serpent.RequireRangeArgs(1, -1),
r.InitClient(client),
),
Options: []serpent.Option{
{
Expand Down
54 changes: 28 additions & 26 deletionscli/exp_scaletest.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -395,18 +395,17 @@ func (r *userCleanupRunner) Run(ctx context.Context, _ string, _ io.Writer) erro

func (r *RootCmd) scaletestCleanup() *serpent.Command {
var template string

cleanupStrategy := &scaletestStrategyFlags{cleanup: true}
client := new(codersdk.Client)

cmd := &serpent.Command{
Use: "cleanup",
Short: "Cleanup scaletest workspaces, then cleanup scaletest users.",
Long: "The strategy flags will apply to each stage of the cleanup process.",
Middleware: serpent.Chain(
r.InitClient(client),
),
Handler: func(inv *serpent.Invocation) error {
client, err := r.InitClient(inv)
if err != nil {
return err
}

ctx := inv.Context()

me, err := requireAdmin(ctx, client)
Expand DownExpand Up@@ -551,14 +550,16 @@ func (r *RootCmd) scaletestCreateWorkspaces() *serpent.Command {
output = &scaletestOutputFlags{}
)

client := new(codersdk.Client)

cmd := &serpent.Command{
Use: "create-workspaces",
Short: "Creates many users, then creates a workspace for each user and waits for them finish building and fully come online. Optionally runs a command inside each workspace, and connects to the workspace over WireGuard.",
Long: `It is recommended that all rate limits are disabled on the server before running this scaletest. This test generates many login events which will be rate limited against the (most likely single) IP.`,
Middleware: r.InitClient(client),
Use: "create-workspaces",
Short: "Creates many users, then creates a workspace for each user and waits for them finish building and fully come online. Optionally runs a command inside each workspace, and connects to the workspace over WireGuard.",
Long: `It is recommended that all rate limits are disabled on the server before running this scaletest. This test generates many login events which will be rate limited against the (most likely single) IP.`,
Handler: func(inv *serpent.Invocation) error {
client, err := r.InitClient(inv)
if err != nil {
return err
}

ctx := inv.Context()

me, err := requireAdmin(ctx, client)
Expand DownExpand Up@@ -861,7 +862,6 @@ func (r *RootCmd) scaletestWorkspaceTraffic() *serpent.Command {
targetWorkspaces string
workspaceProxyURL string

client = &codersdk.Client{}
tracingFlags = &scaletestTracingFlags{}
strategy = &scaletestStrategyFlags{}
cleanupStrategy = &scaletestStrategyFlags{cleanup: true}
Expand All@@ -872,10 +872,12 @@ func (r *RootCmd) scaletestWorkspaceTraffic() *serpent.Command {
cmd := &serpent.Command{
Use: "workspace-traffic",
Short: "Generate traffic to scaletest workspaces through coderd",
Middleware: serpent.Chain(
r.InitClient(client),
),
Handler: func(inv *serpent.Invocation) (err error) {
client, err := r.InitClient(inv)
if err != nil {
return err
}

ctx := inv.Context()

notifyCtx, stop := signal.NotifyContext(ctx, StopSignals...) // Checked later.
Expand DownExpand Up@@ -1150,13 +1152,11 @@ func (r *RootCmd) scaletestWorkspaceTraffic() *serpent.Command {

func (r *RootCmd) scaletestDashboard() *serpent.Command {
var (
interval time.Duration
jitter time.Duration
headless bool
randSeed int64
targetUsers string

client = &codersdk.Client{}
interval time.Duration
jitter time.Duration
headless bool
randSeed int64
targetUsers string
tracingFlags = &scaletestTracingFlags{}
strategy = &scaletestStrategyFlags{}
cleanupStrategy = &scaletestStrategyFlags{cleanup: true}
Expand All@@ -1167,10 +1167,12 @@ func (r *RootCmd) scaletestDashboard() *serpent.Command {
cmd := &serpent.Command{
Use: "dashboard",
Short: "Generate traffic to the HTTP API to simulate use of the dashboard.",
Middleware: serpent.Chain(
r.InitClient(client),
),
Handler: func(inv *serpent.Invocation) error {
client, err := r.InitClient(inv)
if err != nil {
return err
}

if !(interval > 0) {
return xerrors.Errorf("--interval must be greater than zero")
}
Expand Down
7 changes: 5 additions & 2 deletionscli/exp_task_create.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -16,7 +16,6 @@ import (
func (r *RootCmd) taskCreate() *serpent.Command {
var (
orgContext = NewOrganizationContext()
client = new(codersdk.Client)

templateName string
templateVersionName string
Expand All@@ -30,7 +29,6 @@ func (r *RootCmd) taskCreate() *serpent.Command {
Short: "Create an experimental task",
Middleware: serpent.Chain(
serpent.RequireRangeArgs(0, 1),
r.InitClient(client),
),
Options: serpent.OptionSet{
{
Expand DownExpand Up@@ -67,6 +65,11 @@ func (r *RootCmd) taskCreate() *serpent.Command {
},
},
Handler: func(inv *serpent.Invocation) error {
client, err := r.InitClient(inv)
if err != nil {
return err
}

var (
ctx = inv.Context()
expClient = codersdk.NewExperimentalClient(client)
Expand Down
9 changes: 5 additions & 4 deletionscli/exp_task_delete.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -16,20 +16,21 @@ import (
)

func (r *RootCmd) taskDelete() *serpent.Command {
client := new(codersdk.Client)

cmd := &serpent.Command{
Use: "delete <task> [<task> ...]",
Short: "Delete experimental tasks",
Middleware: serpent.Chain(
serpent.RequireRangeArgs(1, -1),
r.InitClient(client),
),
Options: serpent.OptionSet{
cliui.SkipPromptOption(),
},
Handler: func(inv *serpent.Invocation) error {
ctx := inv.Context()
client, err := r.InitClient(inv)
if err != nil {
return err
}
exp := codersdk.NewExperimentalClient(client)

type toDelete struct {
Expand DownExpand Up@@ -70,7 +71,7 @@ func (r *RootCmd) taskDelete() *serpent.Command {
for _, it := range items {
displayList = append(displayList, it.Display)
}
_, err:= cliui.Prompt(inv, cliui.PromptOptions{
_, err = cliui.Prompt(inv, cliui.PromptOptions{
Text: fmt.Sprintf("Delete these tasks: %s?", pretty.Sprint(cliui.DefaultStyles.Code, strings.Join(displayList, ", "))),
IsConfirm: true,
Default: cliui.ConfirmNo,
Expand Down
7 changes: 5 additions & 2 deletionscli/exp_task_list.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -38,7 +38,6 @@ func (r *RootCmd) taskList() *serpent.Command {
user string
quiet bool

client = new(codersdk.Client)
formatter = cliui.NewOutputFormatter(
cliui.TableFormat(
[]taskListRow{},
Expand DownExpand Up@@ -73,7 +72,6 @@ func (r *RootCmd) taskList() *serpent.Command {
Aliases: []string{"ls"},
Middleware: serpent.Chain(
serpent.RequireNArgs(0),
r.InitClient(client),
),
Options: serpent.OptionSet{
{
Expand DownExpand Up@@ -108,6 +106,11 @@ func (r *RootCmd) taskList() *serpent.Command {
},
},
Handler: func(inv *serpent.Invocation) error {
client, err := r.InitClient(inv)
if err != nil {
return err
}

ctx := inv.Context()
exp := codersdk.NewExperimentalClient(client)

Expand Down
7 changes: 5 additions & 2 deletionscli/exp_task_status.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -15,7 +15,6 @@ import (

func (r *RootCmd) taskStatus() *serpent.Command {
var (
client = new(codersdk.Client)
formatter = cliui.NewOutputFormatter(
cliui.TableFormat(
[]taskStatusRow{},
Expand DownExpand Up@@ -66,9 +65,13 @@ func (r *RootCmd) taskStatus() *serpent.Command {
},
Middleware: serpent.Chain(
serpent.RequireNArgs(1),
r.InitClient(client),
),
Handler: func(i *serpent.Invocation) error {
client, err := r.InitClient(i)
if err != nil {
return err
}

ctx := i.Context()
ec := codersdk.NewExperimentalClient(client)
identifier := i.Args[0]
Expand Down
Loading
Loading

[8]ページ先頭

©2009-2025 Movatter.jp