4
4
"context"
5
5
"encoding/json"
6
6
"fmt"
7
- "net/http"
8
7
"os"
9
8
"regexp"
10
9
"strconv"
@@ -49,15 +48,6 @@ func urlCmd() *cobra.Command {
49
48
return cmd
50
49
}
51
50
52
- // DevURL is the parsed json response record for a devURL from cemanager.
53
- type DevURL struct {
54
- ID string `json:"id" table:"-"`
55
- URL string `json:"url" table:"URL"`
56
- Port int `json:"port" table:"Port"`
57
- Name string `json:"name" table:"-"`
58
- Access string `json:"access" table:"Access"`
59
- }
60
-
61
51
var urlAccessLevel = map [string ]string {
62
52
// Remote API endpoint requires these in uppercase.
63
53
"PRIVATE" : "Only you can access" ,
@@ -130,9 +120,10 @@ func createDevURLCmd() *cobra.Command {
130
120
var (
131
121
access string
132
122
urlname string
123
+ scheme string
133
124
)
134
125
cmd := & cobra.Command {
135
- Use : "create [env_name] [port] [--access <level>] [--name <name>] " ,
126
+ Use : "create [env_name] [port]" ,
136
127
Short : "Create a new devurl for an environment" ,
137
128
Aliases : []string {"edit" },
138
129
Args : cobra .ExactArgs (2 ),
@@ -174,36 +165,37 @@ func createDevURLCmd() *cobra.Command {
174
165
175
166
urlID , found := devURLID (portNum , urls )
176
167
if found {
177
- clog .LogInfo (fmt .Sprintf ("updating devurl for port %v" , port ))
178
168
err := client .PutDevURL (ctx , env .ID , urlID , coder.PutDevURLReq {
179
169
Port : portNum ,
180
170
Name : urlname ,
181
171
Access : access ,
182
172
EnvID : env .ID ,
183
- Scheme : "http" ,
173
+ Scheme : scheme ,
184
174
})
185
175
if err != nil {
186
176
return xerrors .Errorf ("update DevURL: %w" , err )
187
177
}
178
+ clog .LogSuccess (fmt .Sprintf ("patched devurl for port %s" , port ))
188
179
} else {
189
- clog .LogInfo (fmt .Sprintf ("Adding devurl for port %v" , port ))
190
180
err := client .CreateDevURL (ctx , env .ID , coder.CreateDevURLReq {
191
181
Port : portNum ,
192
182
Name : urlname ,
193
183
Access : access ,
194
184
EnvID : env .ID ,
195
- Scheme : "http" ,
185
+ Scheme : scheme ,
196
186
})
197
187
if err != nil {
198
188
return xerrors .Errorf ("insert DevURL: %w" , err )
199
189
}
190
+ clog .LogSuccess (fmt .Sprintf ("created devurl for port %s" , port ))
200
191
}
201
192
return nil
202
193
},
203
194
}
204
195
205
196
cmd .Flags ().StringVar (& access , "access" , "private" , "Set DevURL access to [private | org | authed | public]" )
206
197
cmd .Flags ().StringVar (& urlname , "name" , "" , "DevURL name" )
198
+ cmd .Flags ().StringVar (& scheme , "scheme" , "http" , "Server scheme (http|https)" )
207
199
_ = cmd .MarkFlagRequired ("name" )
208
200
209
201
return cmd
@@ -217,7 +209,7 @@ var devURLNameValidRx = regexp.MustCompile("^[a-zA-Z][a-zA-Z0-9]{0,63}$")
217
209
// devURLID returns the ID of a devURL, given the env name and port
218
210
// from a list of DevURL records.
219
211
// ("", false) is returned if no match is found.
220
- func devURLID (port int , urls []DevURL ) (string , bool ) {
212
+ func devURLID (port int , urls []coder. DevURL ) (string , bool ) {
221
213
for _ , url := range urls {
222
214
if url .Port == port {
223
215
return url .ID , true
@@ -267,36 +259,10 @@ func removeDevURL(cmd *cobra.Command, args []string) error {
267
259
}
268
260
269
261
// urlList returns the list of active devURLs from the cemanager.
270
- func urlList (ctx context.Context , client * coder.Client , envName string ) ([]DevURL , error ) {
262
+ func urlList (ctx context.Context , client * coder.Client , envName string ) ([]coder. DevURL , error ) {
271
263
env , err := findEnv (ctx , client , envName , coder .Me )
272
264
if err != nil {
273
265
return nil , err
274
266
}
275
-
276
- reqString := "%s/api/environments/%s/devurls?session_token=%s"
277
- reqURL := fmt .Sprintf (reqString , client .BaseURL , env .ID , client .Token )
278
-
279
- req , err := http .NewRequestWithContext (ctx , http .MethodGet , reqURL , nil )
280
- if err != nil {
281
- return nil , err
282
- }
283
-
284
- resp , err := http .DefaultClient .Do (req )
285
- if err != nil {
286
- return nil , err
287
- }
288
- defer func () { _ = resp .Body .Close () }() // Best effort.
289
-
290
- if resp .StatusCode != http .StatusOK {
291
- return nil , xerrors .Errorf ("non-success status code: %d" , resp .StatusCode )
292
- }
293
-
294
- dec := json .NewDecoder (resp .Body )
295
-
296
- var devURLs []DevURL
297
- if err := dec .Decode (& devURLs ); err != nil {
298
- return nil , err
299
- }
300
-
301
- return devURLs , nil
267
+ return client .DevURLs (ctx , env .ID )
302
268
}
0 commit comments