@@ -19,7 +19,6 @@ import (
19
19
"golang.org/x/xerrors"
20
20
"nhooyr.io/websocket"
21
21
22
- "cdr.dev/coder-cli/internal/x/xcobra"
23
22
"cdr.dev/coder-cli/internal/x/xwebrtc"
24
23
"cdr.dev/coder-cli/pkg/proto"
25
24
)
@@ -40,37 +39,57 @@ func agentCmd() *cobra.Command {
40
39
41
40
func startCmd () * cobra.Command {
42
41
var (
43
- token string
42
+ token string
43
+ coderURL string
44
44
)
45
45
cmd := & cobra.Command {
46
- Use : "start [coderURL] --token=[token]" ,
47
- Args : xcobra .ExactArgs (1 ),
46
+ Use : "start --coder-url=[coder_url] --token=[token]" ,
48
47
Short : "starts the coder agent" ,
49
48
Long : "starts the coder agent" ,
50
- Example : `# start the agent and connect with a Coder agent token
49
+ Example : `# start the agent and use CODER_URL and CODER_AGENT_TOKEN env vars
51
50
52
- coder agent start https://my-coder.com --token xxxx-xxxx
51
+ coder agent start
53
52
54
- # start the agent and use CODER_AGENT_TOKEN env var for auth token
53
+ # start the agent and connect with a specified url and agent token
55
54
56
- coder agent start https://my-coder.com
55
+ coder agent start --coder-url https://my-coder.com --token xxxx-xxxx
57
56
` ,
58
57
RunE : func (cmd * cobra.Command , args []string ) error {
59
58
ctx := cmd .Context ()
60
59
log := slog .Make (sloghuman .Sink (cmd .OutOrStdout ()))
61
60
62
- // Pull the URL from the args and do some sanity check.
63
- rawURL := args [0 ]
64
- if rawURL == "" || ! strings .HasPrefix (rawURL , "http" ) {
61
+ if coderURL == "" {
62
+ var ok bool
63
+ token , ok = os .LookupEnv ("CODER_URL" )
64
+ if ! ok {
65
+ client , err := newClient (ctx )
66
+ if err != nil {
67
+ return xerrors .New ("must login, pass --coder-url flag, or set the CODER_URL env variable" )
68
+ }
69
+ burl := client .BaseURL ()
70
+ coderURL = burl .String ()
71
+ }
72
+ }
73
+
74
+ if ! strings .HasPrefix (coderURL , "http" ) {
65
75
return xerrors .Errorf ("invalid URL" )
66
76
}
67
- u , err := url .Parse (rawURL )
77
+ u , err := url .Parse (coderURL )
68
78
if err != nil {
69
79
return xerrors .Errorf ("parse url: %w" , err )
70
80
}
71
81
// Remove the trailing '/' if any.
72
82
u .Path = "/api/private/envagent/listen"
73
83
84
+
85
+ if token == "" {
86
+ var ok bool
87
+ token , ok = os .LookupEnv ("CODER_AGENT_TOKEN" )
88
+ if ! ok {
89
+ return xerrors .New ("must pass --token or set the CODER_AGENT_TOKEN env variable" )
90
+ }
91
+ }
92
+
74
93
if token == "" {
75
94
var ok bool
76
95
token , ok = os .LookupEnv ("CODER_AGENT_TOKEN" )
@@ -112,6 +131,8 @@ coder agent start https://my-coder.com
112
131
}
113
132
114
133
cmd .Flags ().StringVar (& token , "token" , "" , "coder agent token" )
134
+ cmd .Flags ().StringVar (& coderURL , "coder-url" , "" , "coder access url" )
135
+
115
136
return cmd
116
137
}
117
138
0 commit comments