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

Commitb76e49d

Browse files
author
Russtopia
committed
Add subcommands 'create' and 'del' to manage devurls
1 parent6450687 commitb76e49d

File tree

6 files changed

+178
-8
lines changed

6 files changed

+178
-8
lines changed

‎cmd/coder/main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ func (r *rootCmd) Subcommands() []cli.Command {
3737
&shellCmd{},
3838
&syncCmd{},
3939
&urlsCmd{},
40+
&createURLCmd{},
41+
&delURLCmd{},
4042
&versionCmd{},
4143
&configSSHCmd{},
4244
}

‎cmd/coder/sync.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ func (cmd *syncCmd) RegisterFlags(fl *pflag.FlagSet) {
3232
}
3333

3434
// See https://lxadm.com/Rsync_exit_codes#List_of_standard_rsync_exit_codes.
35-
varIncompatRsync=errors.New("rsync: exit status 2")
36-
varStreamErrRsync=errors.New("rsync: exit status 12")
35+
varerrIncompatRsync=errors.New("rsync: exit status 2")
36+
varerrStreamErrRsync=errors.New("rsync: exit status 12")
3737

3838
func (cmd*syncCmd)Run(fl*pflag.FlagSet) {
3939
var (
@@ -81,9 +81,9 @@ func (cmd *syncCmd) Run(fl *pflag.FlagSet) {
8181
err=s.Run()
8282
}
8383

84-
iffmt.Sprintf("%v",err)==fmt.Sprintf("%v",IncompatRsync) {
84+
iffmt.Sprintf("%v",err)==fmt.Sprintf("%v",errIncompatRsync) {
8585
flog.Fatal("no compatible rsync present on remote machine")
86-
}elseiffmt.Sprintf("%v",err)==fmt.Sprintf("%v",StreamErrRsync) {
86+
}elseiffmt.Sprintf("%v",err)==fmt.Sprintf("%v",errStreamErrRsync) {
8787
flog.Fatal("error in rsync protocol datastream (no installed remote rsync?)")
8888
}else {
8989
flog.Fatal("sync: %v",err)

‎cmd/coder/urls.go

Lines changed: 118 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,118 @@ import (
1414
)
1515

1616
typeurlsCmdstruct{}
17+
typecreateURLCmdstruct{}
18+
typedelURLCmdstruct{}
1719

20+
// DevURL is the parsed json response record for a devURL from cemanager
1821
typeDevURLstruct {
22+
IDstring`json:"id"`
1923
URLstring`json:"url"`
2024
Portstring`json:"port"`
2125
Accessstring`json:"access"`
2226
}
2327

28+
varurlAccessLevel=map[string]string{
29+
"PRIVATE":"Only you can access",
30+
"ORG":"All members of your organization can access",
31+
"AUTHED":"Authenticated users can access",
32+
"PUBLIC":"Anyone on the internet can access this link",
33+
}
34+
35+
funcisAccessLevelValid(levelstring)bool {
36+
_,ok:=urlAccessLevel[level]
37+
returnok
38+
}
39+
40+
func (cmdcreateURLCmd)Spec() cli.CommandSpec {
41+
return cli.CommandSpec{
42+
Name:"create",
43+
Usage:"<env name> <port> <access>",
44+
Desc:`create/update a devurl for external access
45+
<access> is one of [PRIVATE | ORG | AUTHED | PUBLIC]`,
46+
}
47+
}
48+
49+
// Run() creates or updates a devURL, specified by env ID and port
50+
// (fl.Arg(0) and fl.Arg(1)), with access level (fl.Arg(2)) on
51+
// the cemanager.
52+
func (cmdcreateURLCmd)Run(fl*pflag.FlagSet) {
53+
varenvName=fl.Arg(0)
54+
varport=fl.Arg(1)
55+
varaccess=fl.Arg(2)
56+
57+
ifenvName=="" {
58+
exitUsage(fl)
59+
}
60+
61+
if!isAccessLevelValid(access) {
62+
fmt.Printf("Invalid access level '%v'\n",access)
63+
exitUsage(fl)
64+
}
65+
66+
entClient:=requireAuth()
67+
68+
env:=findEnv(entClient,envName)
69+
70+
urlID,found:=devURLID(port,urlList(fl))
71+
iffound {
72+
fmt.Printf("Updating devurl %v\n",urlID)
73+
}else {
74+
fmt.Printf("Adding devurl for port %v\n",port)
75+
}
76+
77+
err:=entClient.UpsertDevURL(env.ID,port,access)
78+
iferr!=nil {
79+
flog.Error("upsert devurl: %s",err.Error())
80+
}
81+
}
82+
83+
func (cmddelURLCmd)Spec() cli.CommandSpec {
84+
return cli.CommandSpec{
85+
Name:"del",
86+
Usage:"<env name> <port>",
87+
Desc:"delete a devurl",
88+
}
89+
}
90+
91+
// Return the ID of a devURL, given the env name and port.
92+
// ("", false) is returned if no match is found.
93+
94+
funcdevURLID(portstring,urls []DevURL) (string,bool) {
95+
for_,url:=rangeurls {
96+
ifurl.Port==port {
97+
returnurl.ID,true
98+
}
99+
}
100+
return"",false
101+
}
102+
103+
// Run() deletes a devURL, specified by env ID and port, from the cemanager.
104+
func (cmddelURLCmd)Run(fl*pflag.FlagSet) {
105+
varenvName=fl.Arg(0)
106+
varport=fl.Arg(1)
107+
108+
ifenvName=="" {
109+
exitUsage(fl)
110+
}
111+
112+
entClient:=requireAuth()
113+
114+
env:=findEnv(entClient,envName)
115+
116+
urlID,found:=devURLID(port,urlList(fl))
117+
iffound {
118+
fmt.Printf("Found devurl id:%v\n",urlID)
119+
}else {
120+
flog.Fatal("Not found: %v",port)
121+
}
122+
123+
err:=entClient.DelDevURL(env.ID,urlID)
124+
iferr!=nil {
125+
flog.Error("delete devurl: %s",err.Error())
126+
}
127+
}
128+
24129
func (cmdurlsCmd)Spec() cli.CommandSpec {
25130
return cli.CommandSpec{
26131
Name:"urls",
@@ -29,7 +134,9 @@ func (cmd urlsCmd) Spec() cli.CommandSpec {
29134
}
30135
}
31136

32-
func (cmdurlsCmd)Run(fl*pflag.FlagSet) {
137+
// urlList() returns the list of active devURLs from the cemanager server.
138+
139+
funcurlList(fl*pflag.FlagSet) []DevURL {
33140
varenvName=fl.Arg(0)
34141

35142
ifenvName=="" {
@@ -41,9 +148,9 @@ func (cmd urlsCmd) Run(fl *pflag.FlagSet) {
41148
env:=findEnv(entClient,envName)
42149

43150
reqString:="%s/api/environments/%s/devurls?session_token=%s"
44-
reqUrl:=fmt.Sprintf(reqString,entClient.BaseURL,env.ID,entClient.Token)
151+
reqURL:=fmt.Sprintf(reqString,entClient.BaseURL,env.ID,entClient.Token)
45152

46-
resp,err:=http.Get(reqUrl)
153+
resp,err:=http.Get(reqURL)
47154
iferr!=nil {
48155
flog.Fatal("%v",err)
49156
}
@@ -65,6 +172,14 @@ func (cmd urlsCmd) Run(fl *pflag.FlagSet) {
65172
fmt.Printf("no dev urls were found for environment: %s\n",envName)
66173
}
67174

175+
returndevURLs
176+
}
177+
178+
// Run() gets the list of active devURLs from the cemanager for the
179+
// specified environment and outputs info to stdout.
180+
func (cmdurlsCmd)Run(fl*pflag.FlagSet) {
181+
devURLs:=urlList(fl)
182+
68183
w:=tabwriter.NewWriter(os.Stdout,0,0,1,' ',tabwriter.TabIndent)
69184
for_,devURL:=rangedevURLs {
70185
fmt.Fprintf(w,"%s\t%s\t%s\n",devURL.URL,devURL.Port,devURL.Access)

‎internal/entclient/activity.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package entclient
22

3-
import"net/http"
3+
import (
4+
"net/http"
5+
)
46

57
func (cClient)PushActivity(sourcestring,envIDstring)error {
68
res,err:=c.request("POST","/api/metrics/usage/push",map[string]string{

‎internal/entclient/request.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ import (
88
"golang.org/x/xerrors"
99
)
1010

11+
// request() makes an HTTP request of the specified method, returning the
12+
// *http.Response and any error.
13+
1114
func (cClient)request(
1215
methodstring,pathstring,
1316
requestinterface{},
@@ -30,6 +33,9 @@ func (c Client) request(
3033
returnclient.Do(req)
3134
}
3235

36+
// requestBody() makes an HTTP request of the specified method, storing any
37+
// returned body in the supplied response.
38+
3339
func (cClient)requestBody(
3440
methodstring,pathstring,requestinterface{},responseinterface{},
3541
)error {

‎internal/entclient/url.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package entclient
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
)
7+
8+
func (cClient)DelDevURL(envID,urlIDstring)error {
9+
reqString:="/api/environments/%s/devurls/%s"
10+
reqUrl:=fmt.Sprintf(reqString,envID,urlID)
11+
12+
res,err:=c.request("DELETE",reqUrl,map[string]string{
13+
"environment_id":envID,
14+
"url_id":urlID,
15+
})
16+
iferr!=nil {
17+
returnerr
18+
}
19+
20+
ifres.StatusCode!=http.StatusOK {
21+
returnbodyError(res)
22+
}
23+
24+
returnnil
25+
}
26+
27+
func (cClient)UpsertDevURL(envID,port,accessstring)error {
28+
reqString:="/api/environments/%s/devurls"
29+
reqUrl:=fmt.Sprintf(reqString,envID)
30+
31+
res,err:=c.request("POST",reqUrl,map[string]string{
32+
"environment_id":envID,
33+
"port":port,
34+
"access":access,
35+
})
36+
iferr!=nil {
37+
returnerr
38+
}
39+
40+
ifres.StatusCode!=http.StatusOK {
41+
returnbodyError(res)
42+
}
43+
44+
returnnil
45+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp