5
5
"fmt"
6
6
"net/http"
7
7
"os"
8
+ "strings"
8
9
"text/tabwriter"
9
10
10
11
"github.com/spf13/pflag"
@@ -14,36 +15,140 @@ import (
14
15
)
15
16
16
17
type urlsCmd struct {}
18
+ type createSubCmd struct {}
19
+ type delSubCmd struct {}
17
20
21
+ // DevURL is the parsed json response record for a devURL from cemanager
18
22
type DevURL struct {
23
+ ID string `json:"id"`
19
24
URL string `json:"url"`
20
25
Port string `json:"port"`
21
26
Access string `json:"access"`
22
27
}
23
28
24
- func (cmd urlsCmd ) Spec () cli.CommandSpec {
29
+ var urlAccessLevel = 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
+ func isAccessLevelValid (level string ) bool {
37
+ _ , ok := urlAccessLevel [level ]
38
+ return ok
39
+ }
40
+
41
+ func (sub createSubCmd ) Spec () cli.CommandSpec {
25
42
return cli.CommandSpec {
26
- Name : "urls" ,
27
- Usage : "<env name>" ,
28
- Desc : "get all development urls for external access" ,
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]` ,
29
47
}
30
48
}
31
49
32
- func (cmd urlsCmd ) Run (fl * pflag.FlagSet ) {
33
- var envName = fl .Arg (0 )
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 (sub createSubCmd ) Run (fl * pflag.FlagSet ) {
54
+ envName := fl .Arg (0 )
55
+ port := fl .Arg (1 )
56
+ access := fl .Arg (2 )
34
57
35
58
if envName == "" {
36
59
exitUsage (fl )
37
60
}
38
61
62
+ if access == "" {
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
+
39
73
entClient := requireAuth ()
40
74
41
75
env := findEnv (entClient , envName )
42
76
77
+ _ , found := devURLID (port , urlList (envName ))
78
+ if found {
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
+ if err != nil {
86
+ flog .Error ("upsert devurl: %s" , err .Error ())
87
+ }
88
+ }
89
+
90
+ func (sub delSubCmd ) Spec () cli.CommandSpec {
91
+ return cli.CommandSpec {
92
+ Name : "del" ,
93
+ Usage : "<env name> <port>" ,
94
+ Desc : "delete a devurl" ,
95
+ }
96
+ }
97
+
98
+ // devURLID returns the ID of a devURL, given the env name and port.
99
+ // ("", false) is returned if no match is found.
100
+ func devURLID (port string , urls []DevURL ) (string , bool ) {
101
+ for _ , url := range urls {
102
+ if url .Port == port {
103
+ return url .ID , true
104
+ }
105
+ }
106
+ return "" , false
107
+ }
108
+
109
+ // Run deletes a devURL, specified by env ID and port, from the cemanager.
110
+ func (sub delSubCmd ) Run (fl * pflag.FlagSet ) {
111
+ envName := fl .Arg (0 )
112
+ port := fl .Arg (1 )
113
+
114
+ if envName == "" {
115
+ exitUsage (fl )
116
+ }
117
+
118
+ entClient := requireAuth ()
119
+
120
+ env := findEnv (entClient , envName )
121
+
122
+ urlID , found := devURLID (port , urlList (envName ))
123
+ if found {
124
+ fmt .Printf ("Deleting devurl for port %v\n " , port )
125
+ } else {
126
+ flog .Fatal ("No devurl found for port %v" , port )
127
+ }
128
+
129
+ err := entClient .DelDevURL (env .ID , urlID )
130
+ if err != nil {
131
+ flog .Error ("delete devurl: %s" , err .Error ())
132
+ }
133
+ }
134
+
135
+ func (cmd urlsCmd ) Spec () cli.CommandSpec {
136
+ return cli.CommandSpec {
137
+ Name : "urls" ,
138
+ Usage : "<env name>" ,
139
+ Desc : "get all development urls for external access" ,
140
+ }
141
+ }
142
+
143
+ // urlList returns the list of active devURLs from the cemanager server.
144
+ func urlList (envName string ) []DevURL {
145
+ entClient := requireAuth ()
146
+ env := findEnv (entClient , envName )
147
+
43
148
reqString := "%s/api/environments/%s/devurls?session_token=%s"
44
- reqUrl := fmt .Sprintf (reqString , entClient .BaseURL , env .ID , entClient .Token )
149
+ reqURL := fmt .Sprintf (reqString , entClient .BaseURL , env .ID , entClient .Token )
45
150
46
- resp , err := http .Get (reqUrl )
151
+ resp , err := http .Get (reqURL )
47
152
if err != nil {
48
153
flog .Fatal ("%v" , err )
49
154
}
@@ -55,7 +160,7 @@ func (cmd urlsCmd) Run(fl *pflag.FlagSet) {
55
160
56
161
dec := json .NewDecoder (resp .Body )
57
162
58
- var devURLs = make ([]DevURL , 0 )
163
+ devURLs : = make ([]DevURL , 0 )
59
164
err = dec .Decode (& devURLs )
60
165
if err != nil {
61
166
flog .Fatal ("%v" , err )
@@ -65,9 +170,25 @@ func (cmd urlsCmd) Run(fl *pflag.FlagSet) {
65
170
fmt .Printf ("no dev urls were found for environment: %s\n " , envName )
66
171
}
67
172
173
+ return devURLs
174
+ }
175
+
176
+ // Run gets the list of active devURLs from the cemanager for the
177
+ // specified environment and outputs info to stdout.
178
+ func (cmd urlsCmd ) Run (fl * pflag.FlagSet ) {
179
+ envName := fl .Arg (0 )
180
+ devURLs := urlList (envName )
181
+
68
182
w := tabwriter .NewWriter (os .Stdout , 0 , 0 , 1 , ' ' , tabwriter .TabIndent )
69
183
for _ , devURL := range devURLs {
70
184
fmt .Fprintf (w , "%s\t %s\t %s\n " , devURL .URL , devURL .Port , devURL .Access )
71
185
}
72
186
w .Flush ()
73
187
}
188
+
189
+ func (cmd * urlsCmd ) Subcommands () []cli.Command {
190
+ return []cli.Command {
191
+ & createSubCmd {},
192
+ & delSubCmd {},
193
+ }
194
+ }
0 commit comments