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

Commit 97d1ec4

Browse files
committed
added devurls capability
1 parent 209ec24 commit 97d1ec4

File tree

4 files changed

+72
-0
lines changed

4 files changed

+72
-0
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 & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,14 @@ $ coder sync ~/Projects/cdr/enterprise/. my-env:~/enterprise
3030
You can access your environment's terminal with `coder sh <env>`. You can also
3131
execute a command in your environment with `coder sh <env> [command] [args]`.
3232

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

3543
- 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+
"io/ioutil"
7+
"net/http"
8+
9+
"github.com/spf13/pflag"
10+
"go.coder.com/cli"
11+
"go.coder.com/flog"
12+
)
13+
14+
type urlCmd struct {
15+
}
16+
17+
type DevURL struct {
18+
Url string `json:"url"`
19+
}
20+
21+
func (cmd urlCmd) Spec() cli.CommandSpec {
22+
return cli.CommandSpec{
23+
Name: "url",
24+
Usage: "[env name] [port]",
25+
Desc: "get a development url for external access",
26+
}
27+
}
28+
29+
func (cmd urlCmd) Run(fl *pflag.FlagSet) {
30+
var (
31+
envName = fl.Arg(0)
32+
port = fl.Arg(1)
33+
)
34+
if envName == "" || port == "" {
35+
exitUsage(fl)
36+
}
37+
38+
entClient := requireAuth()
39+
40+
env := findEnv(entClient, envName)
41+
42+
reqString := "%s/api/environments/%s/devurl?port=%s&session_token=%s"
43+
reqUrl := fmt.Sprintf(reqString, entClient.BaseURL, env.ID, port, entClient.Token)
44+
45+
resp, err := http.Get(reqUrl)
46+
if err != nil {
47+
flog.Fatal("%v", err)
48+
}
49+
50+
body, err := ioutil.ReadAll(resp.Body)
51+
if err != nil {
52+
flog.Fatal("%v", err)
53+
}
54+
55+
var devUrl DevURL
56+
err = json.Unmarshal(body, &devUrl)
57+
if err != nil {
58+
flog.Fatal("%v")
59+
}
60+
61+
flog.Success(devUrl.Url)
62+
}

0 commit comments

Comments
 (0)