Skip to content

Commit 4678def

Browse files
committed
Make createAgentClient use token file and errors
1 parent f696ff4 commit 4678def

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

cli/root.go

Lines changed: 21 additions & 5 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,11 +669,26 @@ 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 works just like CreateClient, but uses the agent token and URL instead.
672+
// createAgentClient returns a new client from the command context. It works
673+
// just like InitClient, but uses the agent token and URL instead.
673674
func (r *RootCmd) createAgentClient() (*agentsdk.Client, error) {
674-
client := agentsdk.New(r.agentURL)
675-
client.SetSessionToken(r.agentToken)
675+
agentURL := r.agentURL
676+
if agentURL == nil || agentURL.String() == "" {
677+
return nil, xerrors.Errorf("%s must be set", envAgentURL)
678+
}
679+
token := r.agentToken
680+
if token == "" {
681+
if r.agentTokenFile == "" {
682+
return nil, xerrors.Errorf("Either %s or %s must be set", envAgentToken, envAgentTokenFile)
683+
}
684+
tokenBytes, err := os.ReadFile(r.agentTokenFile)
685+
if err != nil {
686+
return nil, 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)
676692
return client, nil
677693
}
678694

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy