Skip to content
This repository was archived by the owner on Aug 30, 2024. It is now read-only.

Commit 1f16b2b

Browse files
authored
Add --scheme flag to devurls (#196)
1 parent 44f4a04 commit 1f16b2b

File tree

3 files changed

+23
-47
lines changed

3 files changed

+23
-47
lines changed

coder-sdk/devurl.go

+11-2
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ import (
88

99
// DevURL is the parsed json response record for a devURL from cemanager.
1010
type DevURL struct {
11-
ID string `json:"id" table:"ID"`
11+
ID string `json:"id" table:"-"`
1212
URL string `json:"url" table:"URL"`
1313
Port int `json:"port" table:"Port"`
1414
Access string `json:"access" table:"Access"`
1515
Name string `json:"name" table:"Name"`
16-
Scheme string `json:"scheme" table:"-"`
16+
Scheme string `json:"scheme" table:"Scheme"`
1717
}
1818

1919
type delDevURLRequest struct {
@@ -45,6 +45,15 @@ func (c Client) CreateDevURL(ctx context.Context, envID string, req CreateDevURL
4545
return c.requestBody(ctx, http.MethodPost, "/api/private/environments/"+envID+"/devurls", req, nil)
4646
}
4747

48+
// DevURLs fetches the Dev URLs for a given environment.
49+
func (c Client) DevURLs(ctx context.Context, envID string) ([]DevURL, error) {
50+
var devurls []DevURL
51+
if err := c.requestBody(ctx, http.MethodGet, "/api/private/environments/"+envID+"/devurls", nil, &devurls); err != nil {
52+
return nil, err
53+
}
54+
return devurls, nil
55+
}
56+
4857
// PutDevURLReq defines the request parameters for overwriting a DevURL.
4958
type PutDevURLReq CreateDevURLReq
5059

docs/coder_urls_create.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
Create a new devurl for an environment
44

55
```
6-
coder urls create [env_name] [port] [--access <level>] [--name <name>] [flags]
6+
coder urls create [env_name] [port] [flags]
77
```
88

99
### Options
@@ -12,6 +12,7 @@ coder urls create [env_name] [port] [--access <level>] [--name <name>] [flags]
1212
--access string Set DevURL access to [private | org | authed | public] (default "private")
1313
-h, --help help for create
1414
--name string DevURL name
15+
--scheme string Server scheme (http|https) (default "http")
1516
```
1617

1718
### Options inherited from parent commands

internal/cmd/urls.go

+10-44
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
"encoding/json"
66
"fmt"
7-
"net/http"
87
"os"
98
"regexp"
109
"strconv"
@@ -49,15 +48,6 @@ func urlCmd() *cobra.Command {
4948
return cmd
5049
}
5150

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-
6151
var urlAccessLevel = map[string]string{
6252
// Remote API endpoint requires these in uppercase.
6353
"PRIVATE": "Only you can access",
@@ -130,9 +120,10 @@ func createDevURLCmd() *cobra.Command {
130120
var (
131121
access string
132122
urlname string
123+
scheme string
133124
)
134125
cmd := &cobra.Command{
135-
Use: "create [env_name] [port] [--access <level>] [--name <name>]",
126+
Use: "create [env_name] [port]",
136127
Short: "Create a new devurl for an environment",
137128
Aliases: []string{"edit"},
138129
Args: cobra.ExactArgs(2),
@@ -174,36 +165,37 @@ func createDevURLCmd() *cobra.Command {
174165

175166
urlID, found := devURLID(portNum, urls)
176167
if found {
177-
clog.LogInfo(fmt.Sprintf("updating devurl for port %v", port))
178168
err := client.PutDevURL(ctx, env.ID, urlID, coder.PutDevURLReq{
179169
Port: portNum,
180170
Name: urlname,
181171
Access: access,
182172
EnvID: env.ID,
183-
Scheme: "http",
173+
Scheme: scheme,
184174
})
185175
if err != nil {
186176
return xerrors.Errorf("update DevURL: %w", err)
187177
}
178+
clog.LogSuccess(fmt.Sprintf("patched devurl for port %s", port))
188179
} else {
189-
clog.LogInfo(fmt.Sprintf("Adding devurl for port %v", port))
190180
err := client.CreateDevURL(ctx, env.ID, coder.CreateDevURLReq{
191181
Port: portNum,
192182
Name: urlname,
193183
Access: access,
194184
EnvID: env.ID,
195-
Scheme: "http",
185+
Scheme: scheme,
196186
})
197187
if err != nil {
198188
return xerrors.Errorf("insert DevURL: %w", err)
199189
}
190+
clog.LogSuccess(fmt.Sprintf("created devurl for port %s", port))
200191
}
201192
return nil
202193
},
203194
}
204195

205196
cmd.Flags().StringVar(&access, "access", "private", "Set DevURL access to [private | org | authed | public]")
206197
cmd.Flags().StringVar(&urlname, "name", "", "DevURL name")
198+
cmd.Flags().StringVar(&scheme, "scheme", "http", "Server scheme (http|https)")
207199
_ = cmd.MarkFlagRequired("name")
208200

209201
return cmd
@@ -217,7 +209,7 @@ var devURLNameValidRx = regexp.MustCompile("^[a-zA-Z][a-zA-Z0-9]{0,63}$")
217209
// devURLID returns the ID of a devURL, given the env name and port
218210
// from a list of DevURL records.
219211
// ("", 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) {
221213
for _, url := range urls {
222214
if url.Port == port {
223215
return url.ID, true
@@ -267,36 +259,10 @@ func removeDevURL(cmd *cobra.Command, args []string) error {
267259
}
268260

269261
// 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) {
271263
env, err := findEnv(ctx, client, envName, coder.Me)
272264
if err != nil {
273265
return nil, err
274266
}
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)
302268
}

0 commit comments

Comments
 (0)