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

Commit 96cc93b

Browse files
committed
Update API request, print mult. envs in tabs, handle non 200 response
1 parent c69c791 commit 96cc93b

File tree

1 file changed

+25
-11
lines changed

1 file changed

+25
-11
lines changed

cmd/coder/url.go

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"encoding/json"
55
"fmt"
66
"net/http"
7+
"os"
8+
"text/tabwriter"
79

810
"github.com/spf13/pflag"
911

@@ -14,7 +16,9 @@ import (
1416
type urlCmd struct{}
1517

1618
type DevURL struct {
17-
Url string `json:"url"`
19+
URL string `json:"url"`
20+
Port string `json:"port"`
21+
Access string `json:"access"`
1822
}
1923

2024
func (cmd urlCmd) Spec() cli.CommandSpec {
@@ -26,34 +30,44 @@ func (cmd urlCmd) Spec() cli.CommandSpec {
2630
}
2731

2832
func (cmd urlCmd) Run(fl *pflag.FlagSet) {
29-
var (
30-
envName = fl.Arg(0)
31-
port = fl.Arg(1)
32-
)
33-
if envName == "" || port == "" {
33+
var envName = fl.Arg(0)
34+
35+
if envName == "" {
3436
exitUsage(fl)
3537
}
3638

3739
entClient := requireAuth()
3840

3941
env := findEnv(entClient, envName)
4042

41-
reqString := "%s/api/environments/%s/devurl?port=%s&session_token=%s"
42-
reqUrl := fmt.Sprintf(reqString, entClient.BaseURL, env.ID, port, entClient.Token)
43+
reqString := "%s/api/environments/%s/devurls?session_token=%s"
44+
reqUrl := fmt.Sprintf(reqString, entClient.BaseURL, env.ID, entClient.Token)
4345

4446
resp, err := http.Get(reqUrl)
4547
if err != nil {
4648
flog.Fatal("%v", err)
4749
}
4850
defer resp.Body.Close()
4951

52+
if resp.StatusCode != 200 {
53+
flog.Fatal("non-success status code: %d", resp.StatusCode)
54+
}
55+
5056
dec := json.NewDecoder(resp.Body)
5157

52-
var devUrl DevURL
53-
err = dec.Decode(&devUrl)
58+
var devURLs = make([]DevURL, 0)
59+
err = dec.Decode(&devURLs)
5460
if err != nil {
5561
flog.Fatal("%v", err)
5662
}
5763

58-
fmt.Println(devUrl.Url)
64+
if len(devURLs) == 0 {
65+
fmt.Printf("no dev urls were found for environment: %s\n", envName)
66+
}
67+
68+
w := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', tabwriter.TabIndent)
69+
for _, devURL := range devURLs {
70+
fmt.Fprintf(w, "%s\t%s\t%s\n", devURL.URL, devURL.Port, devURL.Access)
71+
}
72+
w.Flush()
5973
}

0 commit comments

Comments
 (0)