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

Commit20b60a3

Browse files
committed
Make createAgentClient use token file and errors
For now, the old function has been renamed to tryCreateAgentClientbecause it is not clear to me if it is safe to error in these cases orif it was intentional to create a client even if there was no valid URLor token.
1 parentf696ff4 commit20b60a3

File tree

4 files changed

+33
-6
lines changed

4 files changed

+33
-6
lines changed

‎cli/externalauth.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ fi
7575
returnxerrors.Errorf("agent token not found")
7676
}
7777

78-
client,err:=r.createAgentClient()
78+
client,err:=r.tryCreateAgentClient()
7979
iferr!=nil {
8080
returnxerrors.Errorf("create agent client: %w",err)
8181
}

‎cli/gitaskpass.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func (r *RootCmd) gitAskpass() *serpent.Command {
3333
returnxerrors.Errorf("parse host: %w",err)
3434
}
3535

36-
client,err:=r.createAgentClient()
36+
client,err:=r.tryCreateAgentClient()
3737
iferr!=nil {
3838
returnxerrors.Errorf("create agent client: %w",err)
3939
}

‎cli/gitssh.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func (r *RootCmd) gitssh() *serpent.Command {
3838
returnerr
3939
}
4040

41-
client,err:=r.createAgentClient()
41+
client,err:=r.tryCreateAgentClient()
4242
iferr!=nil {
4343
returnxerrors.Errorf("create agent client: %w",err)
4444
}

‎cli/root.go‎

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ const (
8181
envAgentToken="CODER_AGENT_TOKEN"
8282
//nolint:gosec
8383
envAgentTokenFile="CODER_AGENT_TOKEN_FILE"
84+
envAgentURL="CODER_AGENT_URL"
8485
envURL="CODER_URL"
8586
)
8687

@@ -398,7 +399,7 @@ func (r *RootCmd) Command(subcommands []*serpent.Command) (*serpent.Command, err
398399
},
399400
{
400401
Flag:varAgentURL,
401-
Env:"CODER_AGENT_URL",
402+
Env:envAgentURL,
402403
Description:"URL for an agent to access your deployment.",
403404
Value:serpent.URLOf(r.agentURL),
404405
Hidden:true,
@@ -668,9 +669,35 @@ func (r *RootCmd) createUnauthenticatedClient(ctx context.Context, serverURL *ur
668669
return&client,err
669670
}
670671

671-
// createAgentClient returns a new client from the command context.
672-
//It worksjust likeCreateClient, but uses the agent token and URL instead.
672+
// createAgentClient returns a new client from the command context. It works
673+
// just likeInitClient, but uses the agent token and URL instead.
673674
func (r*RootCmd)createAgentClient() (*agentsdk.Client,error) {
675+
agentURL:=r.agentURL
676+
ifagentURL==nil||agentURL.String()=="" {
677+
returnnil,xerrors.Errorf("%s must be set",envAgentURL)
678+
}
679+
token:=r.agentToken
680+
iftoken=="" {
681+
ifr.agentTokenFile=="" {
682+
returnnil,xerrors.Errorf("Either %s or %s must be set",envAgentToken,envAgentTokenFile)
683+
}
684+
tokenBytes,err:=os.ReadFile(r.agentTokenFile)
685+
iferr!=nil {
686+
returnnil,xerrors.Errorf("read token file %q: %w",r.agentTokenFile,err)
687+
}
688+
token=strings.TrimSpace(string(tokenBytes))
689+
}
690+
client:=agentsdk.New(agentURL)
691+
client.SetSessionToken(token)
692+
returnclient,nil
693+
}
694+
695+
// tryCreateAgentClient returns a new client from the command context. It works
696+
// just like tryCreateAgentClient, but does not error.
697+
func (r*RootCmd)tryCreateAgentClient() (*agentsdk.Client,error) {
698+
// TODO: Why does this not actually return any errors despite the function
699+
// signature? Could we just use createAgentClient instead, or is it expected
700+
// that we return a client in some cases even without a valid URL or token?
674701
client:=agentsdk.New(r.agentURL)
675702
client.SetSessionToken(r.agentToken)
676703
returnclient,nil

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp