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

Commit d32a738

Browse files
committed
Added url command to get a devurl
1 parent 209ec24 commit d32a738

File tree

4 files changed

+72
-1
lines changed

4 files changed

+72
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.idea

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ $ coder login https://my-coder-enterprise.com
1818
`coder sync` is useful in cases where you want to use an unsupported IDE with your Coder
1919
Environment.
2020

21-
2221
Ensure that `rsync` is installed locally and in your environment.
2322

2423
``
@@ -30,6 +29,14 @@ $ coder sync ~/Projects/cdr/enterprise/. my-env:~/enterprise
3029
You can access your environment's terminal with `coder sh <env>`. You can also
3130
execute a command in your environment with `coder sh <env> [command] [args]`.
3231

32+
## Development URLs
33+
34+
You can retrieve the devurl of an environment.
35+
36+
``
37+
$ coder url my-env 8080
38+
``
39+
3340
## Caveats
3441

3542
- The `coder login` flow will not work when the CLI is ran from a different network

cmd/coder/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ func (r *rootCmd) Subcommands() []cli.Command {
3030
&logoutCmd{},
3131
&shellCmd{},
3232
&syncCmd{},
33+
&urlCmd{},
3334
}
3435
}
3536

cmd/coder/url.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"net/http"
7+
8+
"github.com/spf13/pflag"
9+
"go.coder.com/cli"
10+
"go.coder.com/flog"
11+
)
12+
13+
type urlCmd struct {
14+
}
15+
16+
type DevURL struct {
17+
Url string `json:"url"`
18+
}
19+
20+
func (cmd urlCmd) Spec() cli.CommandSpec {
21+
return cli.CommandSpec{
22+
Name: "url",
23+
Usage: "[env name] [port]",
24+
Desc: "get a development url for external access",
25+
}
26+
}
27+
28+
func (cmd urlCmd) Run(fl *pflag.FlagSet) {
29+
var (
30+
envName = fl.Arg(0)
31+
port = fl.Arg(1)
32+
)
33+
if envName == "" || port == "" {
34+
exitUsage(fl)
35+
}
36+
37+
entClient := requireAuth()
38+
39+
env := findEnv(entClient, envName)
40+
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+
44+
resp, err := http.Get(reqUrl)
45+
if err != nil {
46+
flog.Fatal("%v", err)
47+
}
48+
defer resp.Body.Close()
49+
50+
dec := json.NewDecoder(resp.Body)
51+
52+
var m map[string] interface{}
53+
54+
err = dec.Decode(&m)
55+
if err != nil {
56+
flog.Fatal("%v", err)
57+
}
58+
59+
for _, v := range m {
60+
fmt.Println(v)
61+
}
62+
}

0 commit comments

Comments
 (0)