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

Commit24c98cd

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

File tree

7 files changed

+197
-15
lines changed

7 files changed

+197
-15
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: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package main
22

33
import (
4-
"errors"
5-
"fmt"
64
"os"
75
"path/filepath"
86
"strings"
@@ -31,10 +29,6 @@ func (cmd *syncCmd) RegisterFlags(fl *pflag.FlagSet) {
3129
fl.BoolVarP(&cmd.init,"init","i",false,"do initial transfer and exit")
3230
}
3331

34-
// 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")
37-
3832
func (cmd*syncCmd)Run(fl*pflag.FlagSet) {
3933
var (
4034
local=fl.Arg(0)
@@ -81,11 +75,7 @@ func (cmd *syncCmd) Run(fl *pflag.FlagSet) {
8175
err=s.Run()
8276
}
8377

84-
iffmt.Sprintf("%v",err)==fmt.Sprintf("%v",IncompatRsync) {
85-
flog.Fatal("no compatible rsync present on remote machine")
86-
}elseiffmt.Sprintf("%v",err)==fmt.Sprintf("%v",StreamErrRsync) {
87-
flog.Fatal("error in rsync protocol datastream (no installed remote rsync?)")
88-
}else {
78+
iferr!=nil {
8979
flog.Fatal("sync: %v",err)
9080
}
9181
}

‎cmd/coder/urls.go

Lines changed: 125 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"net/http"
77
"os"
8+
"strings"
89
"text/tabwriter"
910

1011
"github.com/spf13/pflag"
@@ -14,13 +15,124 @@ import (
1415
)
1516

1617
typeurlsCmdstruct{}
18+
typecreateURLCmdstruct{}
19+
typedelURLCmdstruct{}
1720

21+
// DevURL is the parsed json response record for a devURL from cemanager
1822
typeDevURLstruct {
23+
IDstring`json:"id"`
1924
URLstring`json:"url"`
2025
Portstring`json:"port"`
2126
Accessstring`json:"access"`
2227
}
2328

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

32-
func (cmdurlsCmd)Run(fl*pflag.FlagSet) {
144+
// urlList() returns the list of active devURLs from the cemanager server.
145+
146+
funcurlList(fl*pflag.FlagSet) []DevURL {
33147
varenvName=fl.Arg(0)
34148

35149
ifenvName=="" {
@@ -41,9 +155,9 @@ func (cmd urlsCmd) Run(fl *pflag.FlagSet) {
41155
env:=findEnv(entClient,envName)
42156

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

46-
resp,err:=http.Get(reqUrl)
160+
resp,err:=http.Get(reqURL)
47161
iferr!=nil {
48162
flog.Fatal("%v",err)
49163
}
@@ -65,6 +179,14 @@ func (cmd urlsCmd) Run(fl *pflag.FlagSet) {
65179
fmt.Printf("no dev urls were found for environment: %s\n",envName)
66180
}
67181

182+
returndevURLs
183+
}
184+
185+
// Run() gets the list of active devURLs from the cemanager for the
186+
// specified environment and outputs info to stdout.
187+
func (cmdurlsCmd)Run(fl*pflag.FlagSet) {
188+
devURLs:=urlList(fl)
189+
68190
w:=tabwriter.NewWriter(os.Stdout,0,0,1,' ',tabwriter.TabIndent)
69191
for_,devURL:=rangedevURLs {
70192
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+
}

‎internal/sync/sync.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ type Sync struct {
4141
Client*entclient.Client
4242
}
4343

44+
// See https://lxadm.com/Rsync_exit_codes#List_of_standard_rsync_exit_codes.
45+
const (
46+
ERR_RSYNC_INCOMPAT=2
47+
ERR_RSYNC_STREAM=12
48+
)
49+
4450
func (sSync)syncPaths(deletebool,local,remotestring)error {
4551
self:=os.Args[0]
4652

@@ -66,6 +72,15 @@ func (s Sync) syncPaths(delete bool, local, remote string) error {
6672
cmd.Stdin=os.Stdin
6773
err:=cmd.Run()
6874
iferr!=nil {
75+
ifexitError,ok:=err.(*exec.ExitError);ok {
76+
ifexitError.ExitCode()==ERR_RSYNC_INCOMPAT {
77+
flog.Error("no compatible rsync present on remote machine")
78+
}elseifexitError.ExitCode()==ERR_RSYNC_STREAM {
79+
flog.Error("error in rsync protocol datastream (no installed remote rsync?)")
80+
}else {
81+
flog.Error("sync: %v",err)
82+
}
83+
}
6984
returnxerrors.Errorf("rsync: %w",err)
7085
}
7186
returnnil

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp