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

Commit54f6878

Browse files
committed
refactor agent auth options into it's own type
1 parent80aa609 commit54f6878

30 files changed

+176
-141
lines changed

‎agent/agenttest/agent.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func New(t testing.TB, coderURL *url.URL, agentToken string, opts ...func(*agent
3030
}
3131

3232
ifo.Client==nil {
33-
agentClient:=agentsdk.New(coderURL,agentsdk.UsingFixedToken(agentToken))
33+
agentClient:=agentsdk.New(coderURL,agentsdk.WithFixedToken(agentToken))
3434
agentClient.SDK.SetLogger(log)
3535
o.Client=agentClient
3636
}

‎cli/agent.go‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ import (
3737
"github.com/coder/coder/v2/codersdk/agentsdk"
3838
)
3939

40-
func(r*RootCmd)workspaceAgent()*serpent.Command {
40+
funcworkspaceAgent()*serpent.Command {
4141
var (
4242
logDirstring
4343
scriptDataDirstring
@@ -57,6 +57,7 @@ func (r *RootCmd) workspaceAgent() *serpent.Command {
5757
devcontainerProjectDiscoverybool
5858
devcontainerDiscoveryAutostartbool
5959
)
60+
agentAuth:=NewAgentAuth()
6061
cmd:=&serpent.Command{
6162
Use:"agent",
6263
Short:`Starts the Coder workspace agent.`,
@@ -174,11 +175,11 @@ func (r *RootCmd) workspaceAgent() *serpent.Command {
174175

175176
version:=buildinfo.Version()
176177
logger.Info(ctx,"agent is starting now",
177-
slog.F("url",r.agentURL),
178-
slog.F("auth",r.agentAuth),
178+
slog.F("url",agentAuth.agentURL),
179+
slog.F("auth",agentAuth.agentAuth),
179180
slog.F("version",version),
180181
)
181-
client,err:=r.createAgentClient(ctx)
182+
client,err:=agentAuth.CreateClient(ctx)
182183
iferr!=nil {
183184
returnxerrors.Errorf("create agent client: %w",err)
184185
}
@@ -190,7 +191,7 @@ func (r *RootCmd) workspaceAgent() *serpent.Command {
190191
client.SDK.HTTPClient.Timeout=30*time.Second
191192
// Attach header transport so we process --agent-header and
192193
// --agent-header-command flags
193-
headerTransport,err:=headerTransport(ctx,r.agentURL,agentHeader,agentHeaderCommand)
194+
headerTransport,err:=headerTransport(ctx,agentAuth.agentURL,agentHeader,agentHeaderCommand)
194195
iferr!=nil {
195196
returnxerrors.Errorf("configure header transport: %w",err)
196197
}
@@ -292,7 +293,7 @@ func (r *RootCmd) workspaceAgent() *serpent.Command {
292293
Execer:execer,
293294
Devcontainers:devcontainers,
294295
DevcontainerAPIOptions: []agentcontainers.Option{
295-
agentcontainers.WithSubAgentURL(r.agentURL.String()),
296+
agentcontainers.WithSubAgentURL(agentAuth.agentURL.String()),
296297
agentcontainers.WithProjectDiscovery(devcontainerProjectDiscovery),
297298
agentcontainers.WithDiscoveryAutostart(devcontainerDiscoveryAutostart),
298299
},
@@ -449,7 +450,7 @@ func (r *RootCmd) workspaceAgent() *serpent.Command {
449450
Value:serpent.BoolOf(&devcontainerDiscoveryAutostart),
450451
},
451452
}
452-
453+
agentAuth.AttachOptions(cmd,false)
453454
returncmd
454455
}
455456

‎cli/exp_mcp.go‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func (r *RootCmd) mcpConfigure() *serpent.Command {
5656
},
5757
Children: []*serpent.Command{
5858
r.mcpConfigureClaudeDesktop(),
59-
r.mcpConfigureClaudeCode(),
59+
mcpConfigureClaudeCode(),
6060
r.mcpConfigureCursor(),
6161
},
6262
}
@@ -117,7 +117,7 @@ func (*RootCmd) mcpConfigureClaudeDesktop() *serpent.Command {
117117
returncmd
118118
}
119119

120-
func(r*RootCmd)mcpConfigureClaudeCode()*serpent.Command {
120+
funcmcpConfigureClaudeCode()*serpent.Command {
121121
var (
122122
claudeAPIKeystring
123123
claudeConfigPathstring
@@ -131,6 +131,7 @@ func (r *RootCmd) mcpConfigureClaudeCode() *serpent.Command {
131131

132132
deprecatedCoderMCPClaudeAPIKeystring
133133
)
134+
agentAuth:=NewAgentAuth()
134135
cmd:=&serpent.Command{
135136
Use:"claude-code <project-directory>",
136137
Short:"Configure the Claude Code server. You will need to run this command for each project you want to use. Specify the project directory as the first argument.",
@@ -148,7 +149,7 @@ func (r *RootCmd) mcpConfigureClaudeCode() *serpent.Command {
148149
binPath=testBinaryName
149150
}
150151
configureClaudeEnv:=map[string]string{}
151-
agentClient,err:=r.createAgentClient(inv.Context())
152+
agentClient,err:=agentAuth.CreateClient(inv.Context())
152153
iferr!=nil {
153154
cliui.Warnf(inv.Stderr,"failed to create agent client: %s",err)
154155
}else {
@@ -292,6 +293,7 @@ func (r *RootCmd) mcpConfigureClaudeCode() *serpent.Command {
292293
},
293294
},
294295
}
296+
agentAuth.AttachOptions(cmd,false)
295297
returncmd
296298
}
297299

@@ -403,7 +405,8 @@ func (r *RootCmd) mcpServer() *serpent.Command {
403405
appStatusSlugstring
404406
aiAgentAPIURL url.URL
405407
)
406-
return&serpent.Command{
408+
agentAuth:=NewAgentAuth()
409+
cmd:=&serpent.Command{
407410
Use:"server",
408411
Handler:func(inv*serpent.Invocation)error {
409412
varlastReporttaskReport
@@ -494,7 +497,7 @@ func (r *RootCmd) mcpServer() *serpent.Command {
494497
}
495498

496499
// Try to create an agent client for status reporting. Not validated.
497-
agentClient,err:=r.createAgentClient(inv.Context())
500+
agentClient,err:=agentAuth.CreateClient(inv.Context())
498501
iferr==nil {
499502
cliui.Infof(inv.Stderr,"Agent URL : %s",agentClient.SDK.URL.String())
500503
srv.agentClient=agentClient
@@ -579,6 +582,8 @@ func (r *RootCmd) mcpServer() *serpent.Command {
579582
},
580583
},
581584
}
585+
agentAuth.AttachOptions(cmd,false)
586+
returncmd
582587
}
583588

584589
func (s*mcpServer)startReporter(ctx context.Context,inv*serpent.Invocation) {

‎cli/externalauth.go‎

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,16 @@ package cli
22

33
import (
44
"encoding/json"
5-
"fmt"
6-
7-
"golang.org/x/xerrors"
85

96
"github.com/tidwall/gjson"
7+
"golang.org/x/xerrors"
108

119
"github.com/coder/coder/v2/cli/cliui"
1210
"github.com/coder/coder/v2/codersdk/agentsdk"
13-
"github.com/coder/pretty"
1411
"github.com/coder/serpent"
1512
)
1613

17-
func(r*RootCmd)externalAuth()*serpent.Command {
14+
funcexternalAuth()*serpent.Command {
1815
return&serpent.Command{
1916
Use:"external-auth",
2017
Short:"Manage external authentication",
@@ -23,14 +20,15 @@ func (r *RootCmd) externalAuth() *serpent.Command {
2320
returni.Command.HelpHandler(i)
2421
},
2522
Children: []*serpent.Command{
26-
r.externalAuthAccessToken(),
23+
externalAuthAccessToken(),
2724
},
2825
}
2926
}
3027

31-
func(r*RootCmd)externalAuthAccessToken()*serpent.Command {
28+
funcexternalAuthAccessToken()*serpent.Command {
3229
varextrastring
33-
return&serpent.Command{
30+
agentAuth:=NewAgentAuth()
31+
cmd:=&serpent.Command{
3432
Use:"access-token <provider>",
3533
Short:"Print auth for an external provider",
3634
Long:"Print an access-token for an external auth provider. "+
@@ -70,12 +68,7 @@ fi
7068
ctx,stop:=inv.SignalNotifyContext(ctx,StopSignals...)
7169
deferstop()
7270

73-
ifr.agentToken=="" {
74-
_,_=fmt.Fprint(inv.Stderr,pretty.Sprintf(headLineStyle(),"No agent token found, this command must be run from inside a running workspace.\n"))
75-
returnxerrors.Errorf("agent token not found")
76-
}
77-
78-
client,err:=r.createAgentClient(ctx)
71+
client,err:=agentAuth.CreateClient(ctx)
7972
iferr!=nil {
8073
returnxerrors.Errorf("create agent client: %w",err)
8174
}
@@ -115,4 +108,6 @@ fi
115108
returnnil
116109
},
117110
}
111+
agentAuth.AttachOptions(cmd,false)
112+
returncmd
118113
}

‎cli/gitaskpass.go‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ import (
1818

1919
// gitAskpass is used by the Coder agent to automatically authenticate
2020
// with Git providers based on a hostname.
21-
func(r*RootCmd)gitAskpass()*serpent.Command {
22-
return&serpent.Command{
21+
funcgitAskpass(agentAuth*AgentAuth)*serpent.Command {
22+
cmd:=&serpent.Command{
2323
Use:"gitaskpass",
2424
Hidden:true,
2525
Handler:func(inv*serpent.Invocation)error {
@@ -33,7 +33,7 @@ func (r *RootCmd) gitAskpass() *serpent.Command {
3333
returnxerrors.Errorf("parse host: %w",err)
3434
}
3535

36-
client,err:=r.createAgentClient(ctx)
36+
client,err:=agentAuth.CreateClient(ctx)
3737
iferr!=nil {
3838
returnxerrors.Errorf("create agent client: %w",err)
3939
}
@@ -90,4 +90,6 @@ func (r *RootCmd) gitAskpass() *serpent.Command {
9090
returnnil
9191
},
9292
}
93+
agentAuth.AttachOptions(cmd,false)
94+
returncmd
9395
}

‎cli/gitssh.go‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ import (
1818
"github.com/coder/serpent"
1919
)
2020

21-
func (r*RootCmd)gitssh()*serpent.Command {
21+
funcgitssh()*serpent.Command {
22+
agentAuth:=NewAgentAuth()
2223
cmd:=&serpent.Command{
2324
Use:"gitssh",
2425
Hidden:true,
@@ -38,7 +39,7 @@ func (r *RootCmd) gitssh() *serpent.Command {
3839
returnerr
3940
}
4041

41-
client,err:=r.createAgentClient(ctx)
42+
client,err:=agentAuth.CreateClient(ctx)
4243
iferr!=nil {
4344
returnxerrors.Errorf("create agent client: %w",err)
4445
}
@@ -108,7 +109,7 @@ func (r *RootCmd) gitssh() *serpent.Command {
108109
returnnil
109110
},
110111
}
111-
112+
agentAuth.AttachOptions(cmd,false)
112113
returncmd
113114
}
114115

‎cli/gitssh_test.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func prepareTestGitSSH(ctx context.Context, t *testing.T) (*agentsdk.Client, str
5454
}).WithAgent().Do()
5555

5656
// start workspace agent
57-
agentClient:=agentsdk.New(client.URL,agentsdk.UsingFixedToken(r.AgentToken))
57+
agentClient:=agentsdk.New(client.URL,agentsdk.WithFixedToken(r.AgentToken))
5858
_=agenttest.New(t,client.URL,r.AgentToken,func(o*agent.Options) {
5959
o.Client=agentClient
6060
})

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp