Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Aug 30, 2024. It is now read-only.
/coder-v1-cliPublic archive

Commit1f16b2b

Browse files
authored
Add --scheme flag to devurls (#196)
1 parent44f4a04 commit1f16b2b

File tree

3 files changed

+23
-47
lines changed

3 files changed

+23
-47
lines changed

‎coder-sdk/devurl.go

Lines changed: 11 additions & 2 deletions
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
typeDevURLstruct {
11-
IDstring`json:"id" table:"ID"`
11+
IDstring`json:"id" table:"-"`
1212
URLstring`json:"url" table:"URL"`
1313
Portint`json:"port" table:"Port"`
1414
Accessstring`json:"access" table:"Access"`
1515
Namestring`json:"name" table:"Name"`
16-
Schemestring`json:"scheme" table:"-"`
16+
Schemestring`json:"scheme" table:"Scheme"`
1717
}
1818

1919
typedelDevURLRequeststruct {
@@ -45,6 +45,15 @@ func (c Client) CreateDevURL(ctx context.Context, envID string, req CreateDevURL
4545
returnc.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 (cClient)DevURLs(ctx context.Context,envIDstring) ([]DevURL,error) {
50+
vardevurls []DevURL
51+
iferr:=c.requestBody(ctx,http.MethodGet,"/api/private/environments/"+envID+"/devurls",nil,&devurls);err!=nil {
52+
returnnil,err
53+
}
54+
returndevurls,nil
55+
}
56+
4857
// PutDevURLReq defines the request parameters for overwriting a DevURL.
4958
typePutDevURLReqCreateDevURLReq
5059

‎docs/coder_urls_create.md

Lines changed: 2 additions & 1 deletion
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

Lines changed: 10 additions & 44 deletions
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
returncmd
5049
}
5150

52-
// DevURL is the parsed json response record for a devURL from cemanager.
53-
typeDevURLstruct {
54-
IDstring`json:"id" table:"-"`
55-
URLstring`json:"url" table:"URL"`
56-
Portint`json:"port" table:"Port"`
57-
Namestring`json:"name" table:"-"`
58-
Accessstring`json:"access" table:"Access"`
59-
}
60-
6151
varurlAccessLevel=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
accessstring
132122
urlnamestring
123+
schemestring
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
iffound {
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
iferr!=nil {
186176
returnxerrors.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
iferr!=nil {
198188
returnxerrors.Errorf("insert DevURL: %w",err)
199189
}
190+
clog.LogSuccess(fmt.Sprintf("created devurl for port %s",port))
200191
}
201192
returnnil
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
returncmd
@@ -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-
funcdevURLID(portint,urls []DevURL) (string,bool) {
212+
funcdevURLID(portint,urls []coder.DevURL) (string,bool) {
221213
for_,url:=rangeurls {
222214
ifurl.Port==port {
223215
returnurl.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-
funcurlList(ctx context.Context,client*coder.Client,envNamestring) ([]DevURL,error) {
262+
funcurlList(ctx context.Context,client*coder.Client,envNamestring) ([]coder.DevURL,error) {
271263
env,err:=findEnv(ctx,client,envName,coder.Me)
272264
iferr!=nil {
273265
returnnil,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-
iferr!=nil {
281-
returnnil,err
282-
}
283-
284-
resp,err:=http.DefaultClient.Do(req)
285-
iferr!=nil {
286-
returnnil,err
287-
}
288-
deferfunc() {_=resp.Body.Close() }()// Best effort.
289-
290-
ifresp.StatusCode!=http.StatusOK {
291-
returnnil,xerrors.Errorf("non-success status code: %d",resp.StatusCode)
292-
}
293-
294-
dec:=json.NewDecoder(resp.Body)
295-
296-
vardevURLs []DevURL
297-
iferr:=dec.Decode(&devURLs);err!=nil {
298-
returnnil,err
299-
}
300-
301-
returndevURLs,nil
267+
returnclient.DevURLs(ctx,env.ID)
302268
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp