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

Commit19ea167

Browse files
committed
chore: refactor to directly create Client in Command Handlers
1 parent5c2b9a5 commit19ea167

File tree

67 files changed

+603
-429
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+603
-429
lines changed

‎cli/autoupdate.go‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,21 @@ import (
1212
)
1313

1414
func (r*RootCmd)autoupdate()*serpent.Command {
15-
client:=new(codersdk.Client)
1615
cmd:=&serpent.Command{
1716
Annotations:workspaceCommand,
1817
Use:"autoupdate <workspace> <always|never>",
1918
Short:"Toggle auto-update policy for a workspace",
2019
Middleware:serpent.Chain(
2120
serpent.RequireNArgs(2),
22-
r.InitClient(client),
2321
),
2422
Handler:func(inv*serpent.Invocation)error {
23+
client,err:=r.InitClient(inv)
24+
iferr!=nil {
25+
returnerr
26+
}
27+
2528
policy:=strings.ToLower(inv.Args[1])
26-
err:=validateAutoUpdatePolicy(policy)
29+
err=validateAutoUpdatePolicy(policy)
2730
iferr!=nil {
2831
returnxerrors.Errorf("validate policy: %w",err)
2932
}

‎cli/configssh.go‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,6 @@ func (r *RootCmd) configSSH() *serpent.Command {
236236
dryRunbool
237237
coderCliPathstring
238238
)
239-
client:=new(codersdk.Client)
240239
cmd:=&serpent.Command{
241240
Annotations:workspaceCommand,
242241
Use:"config-ssh",
@@ -253,9 +252,13 @@ func (r *RootCmd) configSSH() *serpent.Command {
253252
),
254253
Middleware:serpent.Chain(
255254
serpent.RequireNArgs(0),
256-
r.InitClient(client),
257255
),
258256
Handler:func(inv*serpent.Invocation)error {
257+
client,err:=r.InitClient(inv)
258+
iferr!=nil {
259+
returnerr
260+
}
261+
259262
ctx:=inv.Context()
260263

261264
ifsshConfigOpts.waitEnum!="auto"&&sshConfigOpts.skipProxyCommand {
@@ -280,7 +283,6 @@ func (r *RootCmd) configSSH() *serpent.Command {
280283
out=inv.Stderr
281284
}
282285

283-
varerrerror
284286
coderBinary:=coderCliPath
285287
ifcoderBinary=="" {
286288
coderBinary,err=currentBinPath(out)

‎cli/create.go‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ func (r *RootCmd) Create(opts CreateOptions) *serpent.Command {
5050
// shares the same name across multiple organizations.
5151
orgContext=NewOrganizationContext()
5252
)
53-
client:=new(codersdk.Client)
5453
cmd:=&serpent.Command{
5554
Annotations:workspaceCommand,
5655
Use:"create [workspace]",
@@ -61,9 +60,12 @@ func (r *RootCmd) Create(opts CreateOptions) *serpent.Command {
6160
Command:"coder create <username>/<workspace_name>",
6261
},
6362
),
64-
Middleware:serpent.Chain(r.InitClient(client)),
6563
Handler:func(inv*serpent.Invocation)error {
66-
varerrerror
64+
client,err:=r.InitClient(inv)
65+
iferr!=nil {
66+
returnerr
67+
}
68+
6769
workspaceOwner:=codersdk.Me
6870
iflen(inv.Args)>=1 {
6971
workspaceOwner,workspaceName,err=splitNamedWorkspace(inv.Args[0])

‎cli/delete.go‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ func (r *RootCmd) deleteWorkspace() *serpent.Command {
1616
orphanbool
1717
provbuildFlags
1818
)
19-
client:=new(codersdk.Client)
2019
cmd:=&serpent.Command{
2120
Annotations:workspaceCommand,
2221
Use:"delete <workspace>",
@@ -29,9 +28,13 @@ func (r *RootCmd) deleteWorkspace() *serpent.Command {
2928
),
3029
Middleware:serpent.Chain(
3130
serpent.RequireNArgs(1),
32-
r.InitClient(client),
3331
),
3432
Handler:func(inv*serpent.Invocation)error {
33+
client,err:=r.InitClient(inv)
34+
iferr!=nil {
35+
returnerr
36+
}
37+
3538
workspace,err:=namedWorkspace(inv.Context(),client,inv.Args[0])
3639
iferr!=nil {
3740
returnerr

‎cli/exp_mcp.go‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,6 @@ type mcpServer struct {
399399

400400
func (r*RootCmd)mcpServer()*serpent.Command {
401401
var (
402-
client=new(codersdk.Client)
403402
instructionsstring
404403
allowedTools []string
405404
appStatusSlugstring
@@ -409,6 +408,11 @@ func (r *RootCmd) mcpServer() *serpent.Command {
409408
cmd:=&serpent.Command{
410409
Use:"server",
411410
Handler:func(inv*serpent.Invocation)error {
411+
client,err:=r.TryInitClient(inv)
412+
iferr!=nil {
413+
returnerr
414+
}
415+
412416
varlastReporttaskReport
413417
// Create a queue that skips duplicates and preserves summaries.
414418
queue:= cliutil.NewQueue[taskReport](512).WithPredicate(func(reporttaskReport) (taskReport,bool) {
@@ -548,9 +552,6 @@ func (r *RootCmd) mcpServer() *serpent.Command {
548552
returnsrv.startServer(ctx,inv,instructions,allowedTools)
549553
},
550554
Short:"Start the Coder MCP server.",
551-
Middleware:serpent.Chain(
552-
r.TryInitClient(client),
553-
),
554555
Options: []serpent.Option{
555556
{
556557
Name:"instructions",

‎cli/exp_rpty.go‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,24 +22,24 @@ import (
2222
)
2323

2424
func (r*RootCmd)rptyCommand()*serpent.Command {
25-
var (
26-
client=new(codersdk.Client)
27-
argshandleRPTYArgs
28-
)
25+
varargshandleRPTYArgs
2926

3027
cmd:=&serpent.Command{
3128
Handler:func(inv*serpent.Invocation)error {
3229
ifr.disableDirect {
3330
returnxerrors.New("direct connections are disabled, but you can try websocat ;-)")
3431
}
32+
client,err:=r.InitClient(inv)
33+
iferr!=nil {
34+
returnerr
35+
}
3536
args.NamedWorkspace=inv.Args[0]
3637
args.Command=inv.Args[1:]
3738
returnhandleRPTY(inv,client,args)
3839
},
3940
Long:"Establish an RPTY session with a workspace/agent. This uses the same mechanism as the Web Terminal.",
4041
Middleware:serpent.Chain(
4142
serpent.RequireRangeArgs(1,-1),
42-
r.InitClient(client),
4343
),
4444
Options: []serpent.Option{
4545
{

‎cli/exp_scaletest.go‎

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -395,18 +395,17 @@ func (r *userCleanupRunner) Run(ctx context.Context, _ string, _ io.Writer) erro
395395

396396
func (r*RootCmd)scaletestCleanup()*serpent.Command {
397397
vartemplatestring
398-
399398
cleanupStrategy:=&scaletestStrategyFlags{cleanup:true}
400-
client:=new(codersdk.Client)
401-
402399
cmd:=&serpent.Command{
403400
Use:"cleanup",
404401
Short:"Cleanup scaletest workspaces, then cleanup scaletest users.",
405402
Long:"The strategy flags will apply to each stage of the cleanup process.",
406-
Middleware:serpent.Chain(
407-
r.InitClient(client),
408-
),
409403
Handler:func(inv*serpent.Invocation)error {
404+
client,err:=r.InitClient(inv)
405+
iferr!=nil {
406+
returnerr
407+
}
408+
410409
ctx:=inv.Context()
411410

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

554-
client:=new(codersdk.Client)
555-
556553
cmd:=&serpent.Command{
557-
Use:"create-workspaces",
558-
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.",
559-
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.`,
560-
Middleware:r.InitClient(client),
554+
Use:"create-workspaces",
555+
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.",
556+
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.`,
561557
Handler:func(inv*serpent.Invocation)error {
558+
client,err:=r.InitClient(inv)
559+
iferr!=nil {
560+
returnerr
561+
}
562+
562563
ctx:=inv.Context()
563564

564565
me,err:=requireAdmin(ctx,client)
@@ -861,7 +862,6 @@ func (r *RootCmd) scaletestWorkspaceTraffic() *serpent.Command {
861862
targetWorkspacesstring
862863
workspaceProxyURLstring
863864

864-
client=&codersdk.Client{}
865865
tracingFlags=&scaletestTracingFlags{}
866866
strategy=&scaletestStrategyFlags{}
867867
cleanupStrategy=&scaletestStrategyFlags{cleanup:true}
@@ -872,10 +872,12 @@ func (r *RootCmd) scaletestWorkspaceTraffic() *serpent.Command {
872872
cmd:=&serpent.Command{
873873
Use:"workspace-traffic",
874874
Short:"Generate traffic to scaletest workspaces through coderd",
875-
Middleware:serpent.Chain(
876-
r.InitClient(client),
877-
),
878875
Handler:func(inv*serpent.Invocation) (errerror) {
876+
client,err:=r.InitClient(inv)
877+
iferr!=nil {
878+
returnerr
879+
}
880+
879881
ctx:=inv.Context()
880882

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

11511153
func (r*RootCmd)scaletestDashboard()*serpent.Command {
11521154
var (
1153-
interval time.Duration
1154-
jitter time.Duration
1155-
headlessbool
1156-
randSeedint64
1157-
targetUsersstring
1158-
1159-
client=&codersdk.Client{}
1155+
interval time.Duration
1156+
jitter time.Duration
1157+
headlessbool
1158+
randSeedint64
1159+
targetUsersstring
11601160
tracingFlags=&scaletestTracingFlags{}
11611161
strategy=&scaletestStrategyFlags{}
11621162
cleanupStrategy=&scaletestStrategyFlags{cleanup:true}
@@ -1167,10 +1167,12 @@ func (r *RootCmd) scaletestDashboard() *serpent.Command {
11671167
cmd:=&serpent.Command{
11681168
Use:"dashboard",
11691169
Short:"Generate traffic to the HTTP API to simulate use of the dashboard.",
1170-
Middleware:serpent.Chain(
1171-
r.InitClient(client),
1172-
),
11731170
Handler:func(inv*serpent.Invocation)error {
1171+
client,err:=r.InitClient(inv)
1172+
iferr!=nil {
1173+
returnerr
1174+
}
1175+
11741176
if!(interval>0) {
11751177
returnxerrors.Errorf("--interval must be greater than zero")
11761178
}

‎cli/exp_task_create.go‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import (
1616
func (r*RootCmd)taskCreate()*serpent.Command {
1717
var (
1818
orgContext=NewOrganizationContext()
19-
client=new(codersdk.Client)
2019

2120
templateNamestring
2221
templateVersionNamestring
@@ -30,7 +29,6 @@ func (r *RootCmd) taskCreate() *serpent.Command {
3029
Short:"Create an experimental task",
3130
Middleware:serpent.Chain(
3231
serpent.RequireRangeArgs(0,1),
33-
r.InitClient(client),
3432
),
3533
Options: serpent.OptionSet{
3634
{
@@ -67,6 +65,11 @@ func (r *RootCmd) taskCreate() *serpent.Command {
6765
},
6866
},
6967
Handler:func(inv*serpent.Invocation)error {
68+
client,err:=r.InitClient(inv)
69+
iferr!=nil {
70+
returnerr
71+
}
72+
7073
var (
7174
ctx=inv.Context()
7275
expClient=codersdk.NewExperimentalClient(client)

‎cli/exp_task_delete.go‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,21 @@ import (
1616
)
1717

1818
func (r*RootCmd)taskDelete()*serpent.Command {
19-
client:=new(codersdk.Client)
20-
2119
cmd:=&serpent.Command{
2220
Use:"delete <task> [<task> ...]",
2321
Short:"Delete experimental tasks",
2422
Middleware:serpent.Chain(
2523
serpent.RequireRangeArgs(1,-1),
26-
r.InitClient(client),
2724
),
2825
Options: serpent.OptionSet{
2926
cliui.SkipPromptOption(),
3027
},
3128
Handler:func(inv*serpent.Invocation)error {
3229
ctx:=inv.Context()
30+
client,err:=r.InitClient(inv)
31+
iferr!=nil {
32+
returnerr
33+
}
3334
exp:=codersdk.NewExperimentalClient(client)
3435

3536
typetoDeletestruct {
@@ -70,7 +71,7 @@ func (r *RootCmd) taskDelete() *serpent.Command {
7071
for_,it:=rangeitems {
7172
displayList=append(displayList,it.Display)
7273
}
73-
_,err:=cliui.Prompt(inv, cliui.PromptOptions{
74+
_,err=cliui.Prompt(inv, cliui.PromptOptions{
7475
Text:fmt.Sprintf("Delete these tasks: %s?",pretty.Sprint(cliui.DefaultStyles.Code,strings.Join(displayList,", "))),
7576
IsConfirm:true,
7677
Default:cliui.ConfirmNo,

‎cli/exp_task_list.go‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ func (r *RootCmd) taskList() *serpent.Command {
3838
userstring
3939
quietbool
4040

41-
client=new(codersdk.Client)
4241
formatter=cliui.NewOutputFormatter(
4342
cliui.TableFormat(
4443
[]taskListRow{},
@@ -73,7 +72,6 @@ func (r *RootCmd) taskList() *serpent.Command {
7372
Aliases: []string{"ls"},
7473
Middleware:serpent.Chain(
7574
serpent.RequireNArgs(0),
76-
r.InitClient(client),
7775
),
7876
Options: serpent.OptionSet{
7977
{
@@ -108,6 +106,11 @@ func (r *RootCmd) taskList() *serpent.Command {
108106
},
109107
},
110108
Handler:func(inv*serpent.Invocation)error {
109+
client,err:=r.InitClient(inv)
110+
iferr!=nil {
111+
returnerr
112+
}
113+
111114
ctx:=inv.Context()
112115
exp:=codersdk.NewExperimentalClient(client)
113116

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp