@@ -81,6 +81,7 @@ const (
81
81
envAgentToken = "CODER_AGENT_TOKEN"
82
82
//nolint:gosec
83
83
envAgentTokenFile = "CODER_AGENT_TOKEN_FILE"
84
+ envAgentURL = "CODER_AGENT_URL"
84
85
envURL = "CODER_URL"
85
86
)
86
87
@@ -398,7 +399,7 @@ func (r *RootCmd) Command(subcommands []*serpent.Command) (*serpent.Command, err
398
399
},
399
400
{
400
401
Flag : varAgentURL ,
401
- Env : "CODER_AGENT_URL" ,
402
+ Env : envAgentURL ,
402
403
Description : "URL for an agent to access your deployment." ,
403
404
Value : serpent .URLOf (r .agentURL ),
404
405
Hidden : true ,
@@ -668,11 +669,26 @@ func (r *RootCmd) createUnauthenticatedClient(ctx context.Context, serverURL *ur
668
669
return & client , err
669
670
}
670
671
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.
673
674
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 )
676
692
return client , nil
677
693
}
678
694
0 commit comments