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

Commite7d80a4

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

File tree

6 files changed

+180
-8
lines changed

6 files changed

+180
-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: 120 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,120 @@ 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+
// TODO: check for missing args above
58+
59+
ifenvName=="" {
60+
exitUsage(fl)
61+
}
62+
63+
if!isAccessLevelValid(access) {
64+
fmt.Printf("Invalid access level '%v'\n",access)
65+
exitUsage(fl)
66+
}
67+
68+
entClient:=requireAuth()
69+
70+
env:=findEnv(entClient,envName)
71+
72+
urlID,found:=devURLID(port,urlList(fl))
73+
iffound {
74+
fmt.Printf("Updating devurl %v\n",urlID)
75+
}else {
76+
fmt.Printf("Adding devurl for port %v\n",port)
77+
}
78+
79+
err:=entClient.UpsertDevURL(env.ID,port,access)
80+
iferr!=nil {
81+
flog.Error("upsert devurl: %s",err.Error())
82+
}
83+
}
84+
85+
func (cmddelURLCmd)Spec() cli.CommandSpec {
86+
return cli.CommandSpec{
87+
Name:"del",
88+
Usage:"<env name> <port>",
89+
Desc:"delete a devurl",
90+
}
91+
}
92+
93+
// Return the ID of a devURL, given the env name and port.
94+
// ("", false) is returned if no match is found.
95+
96+
funcdevURLID(portstring,urls []DevURL) (string,bool) {
97+
for_,url:=rangeurls {
98+
ifurl.Port==port {
99+
returnurl.ID,true
100+
}
101+
}
102+
return"",false
103+
}
104+
105+
// Run() deletes a devURL, specified by env ID and port, from the cemanager.
106+
func (cmddelURLCmd)Run(fl*pflag.FlagSet) {
107+
varenvName=fl.Arg(0)
108+
varport=fl.Arg(1)
109+
110+
ifenvName=="" {
111+
exitUsage(fl)
112+
}
113+
114+
entClient:=requireAuth()
115+
116+
env:=findEnv(entClient,envName)
117+
118+
urlID,found:=devURLID(port,urlList(fl))
119+
iffound {
120+
fmt.Printf("Found devurl id:%v\n",urlID)
121+
}else {
122+
flog.Fatal("Not found: %v",port)
123+
}
124+
125+
err:=entClient.DelDevURL(env.ID,urlID)
126+
iferr!=nil {
127+
flog.Error("delete devurl: %s",err.Error())
128+
}
129+
}
130+
24131
func (cmdurlsCmd)Spec() cli.CommandSpec {
25132
return cli.CommandSpec{
26133
Name:"urls",
@@ -29,7 +136,9 @@ func (cmd urlsCmd) Spec() cli.CommandSpec {
29136
}
30137
}
31138

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

35144
ifenvName=="" {
@@ -41,9 +150,9 @@ func (cmd urlsCmd) Run(fl *pflag.FlagSet) {
41150
env:=findEnv(entClient,envName)
42151

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

46-
resp,err:=http.Get(reqUrl)
155+
resp,err:=http.Get(reqURL)
47156
iferr!=nil {
48157
flog.Fatal("%v",err)
49158
}
@@ -65,6 +174,14 @@ func (cmd urlsCmd) Run(fl *pflag.FlagSet) {
65174
fmt.Printf("no dev urls were found for environment: %s\n",envName)
66175
}
67176

177+
returndevURLs
178+
}
179+
180+
// Run() gets the list of active devURLs from the cemanager for the
181+
// specified environment and outputs info to stdout.
182+
func (cmdurlsCmd)Run(fl*pflag.FlagSet) {
183+
devURLs:=urlList(fl)
184+
68185
w:=tabwriter.NewWriter(os.Stdout,0,0,1,' ',tabwriter.TabIndent)
69186
for_,devURL:=rangedevURLs {
70187
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