Skip to content
This repository was archived by the owner on Aug 30, 2024. It is now read-only.

Commit c71c3c9

Browse files
author
Russtopia
committed
Add urls ls subcmd; add -o [human | json] support matching users cmd
1 parent 362ddc7 commit c71c3c9

File tree

1 file changed

+62
-28
lines changed

1 file changed

+62
-28
lines changed

cmd/coder/urls.go

Lines changed: 62 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,26 @@ import (
1818

1919
type urlsCmd struct{}
2020

21+
func (cmd *urlsCmd) Subcommands() []cli.Command {
22+
return []cli.Command{
23+
&listSubCmd{},
24+
&createSubCmd{},
25+
&delSubCmd{},
26+
}
27+
}
28+
29+
func (cmd urlsCmd) Spec() cli.CommandSpec {
30+
return cli.CommandSpec{
31+
Name: "urls",
32+
Usage: "[subcommand] <flags>",
33+
Desc: "interact with environment devurls",
34+
}
35+
}
36+
37+
func (cmd urlsCmd) Run(fl *pflag.FlagSet) {
38+
exitUsage(fl)
39+
}
40+
2141
// DevURL is the parsed json response record for a devURL from cemanager
2242
type DevURL struct {
2343
ID string `json:"id"`
@@ -57,6 +77,48 @@ func accessLevelIsValid(level string) bool {
5777
return ok
5878
}
5979

80+
type listSubCmd struct {
81+
outputFmt string
82+
}
83+
84+
// Run gets the list of active devURLs from the cemanager for the
85+
// specified environment and outputs info to stdout.
86+
func (sub listSubCmd) Run(fl *pflag.FlagSet) {
87+
envName := fl.Arg(0)
88+
devURLs := urlList(envName)
89+
90+
switch sub.outputFmt {
91+
case "human":
92+
w := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', tabwriter.TabIndent)
93+
for _, devURL := range devURLs {
94+
fmt.Fprintf(w, "%s\t%d\t%s\n", devURL.URL, devURL.Port, devURL.Access)
95+
}
96+
err := w.Flush()
97+
if err != nil {
98+
flog.Fatal("failed to flush writer: %v", err)
99+
}
100+
case "json":
101+
err := json.NewEncoder(os.Stdout).Encode(devURLs)
102+
if err != nil {
103+
flog.Fatal("failed to encode devurls to json: %v", err)
104+
}
105+
default:
106+
exitUsage(fl)
107+
}
108+
}
109+
110+
func (sub *listSubCmd) RegisterFlags(fl *pflag.FlagSet) {
111+
fl.StringVarP(&sub.outputFmt, "output", "o", "human", "output format (human | json)")
112+
}
113+
114+
func (sub *listSubCmd) Spec() cli.CommandSpec {
115+
return cli.CommandSpec{
116+
Name: "ls",
117+
Usage: "<env> <flags>",
118+
Desc: "list all devurls",
119+
}
120+
}
121+
60122
type createSubCmd struct {
61123
access string
62124
urlname string
@@ -181,14 +243,6 @@ func (sub delSubCmd) Run(fl *pflag.FlagSet) {
181243
}
182244
}
183245

184-
func (cmd urlsCmd) Spec() cli.CommandSpec {
185-
return cli.CommandSpec{
186-
Name: "urls",
187-
Usage: "<env name>",
188-
Desc: "get all development urls for external access",
189-
}
190-
}
191-
192246
// urlList returns the list of active devURLs from the cemanager.
193247
func urlList(envName string) []DevURL {
194248
entClient := requireAuth()
@@ -217,23 +271,3 @@ func urlList(envName string) []DevURL {
217271

218272
return devURLs
219273
}
220-
221-
// Run gets the list of active devURLs from the cemanager for the
222-
// specified environment and outputs info to stdout.
223-
func (cmd urlsCmd) Run(fl *pflag.FlagSet) {
224-
envName := fl.Arg(0)
225-
devURLs := urlList(envName)
226-
227-
w := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', tabwriter.TabIndent)
228-
for _, devURL := range devURLs {
229-
fmt.Fprintf(w, "%s\t%d\t%s\n", devURL.URL, devURL.Port, devURL.Access)
230-
}
231-
w.Flush()
232-
}
233-
234-
func (cmd *urlsCmd) Subcommands() []cli.Command {
235-
return []cli.Command{
236-
&createSubCmd{},
237-
&delSubCmd{},
238-
}
239-
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy