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

added devurls capability #10

Merged
merged 1 commit into from
May 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ $ coder login https://my-coder-enterprise.com
`coder sync` is useful in cases where you want to use an unsupported IDE with your Coder
Environment.


Ensure that `rsync` is installed locally and in your environment.

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

## Development URLs

You can retrieve the devurl of an environment.

``
$ coder url my-env 8080
``

## Caveats

- The `coder login` flow will not work when the CLI is ran from a different network
Expand Down
1 change: 1 addition & 0 deletions cmd/coder/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func (r *rootCmd) Subcommands() []cli.Command {
&logoutCmd{},
&shellCmd{},
&syncCmd{},
&urlCmd{},
}
}

Expand Down
59 changes: 59 additions & 0 deletions cmd/coder/url.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package main

import (
"encoding/json"
"fmt"
"net/http"

"github.com/spf13/pflag"
"go.coder.com/cli"
"go.coder.com/flog"
)

type urlCmd struct {
}

type DevURL struct {
Url string `json:"url"`
}

func (cmd urlCmd) Spec() cli.CommandSpec {
return cli.CommandSpec{
Name: "url",
Usage: "<env name> <port>",
Desc: "get a development url for external access",
}
}

func (cmd urlCmd) Run(fl *pflag.FlagSet) {
var (
envName = fl.Arg(0)
port = fl.Arg(1)
)
if envName == "" || port == "" {
exitUsage(fl)
}

entClient := requireAuth()

env := findEnv(entClient, envName)

reqString := "%s/api/environments/%s/devurl?port=%s&session_token=%s"
reqUrl := fmt.Sprintf(reqString, entClient.BaseURL, env.ID, port, entClient.Token)
Comment on lines +41 to +42
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For re-usability I think it would make sense to have route constructors in their own function.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought about that, but it's basically just Sprintf.


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

dec := json.NewDecoder(resp.Body)

var devUrl DevURL
err = dec.Decode(&devUrl)
if err != nil {
flog.Fatal("%v", err)
}

fmt.Println(devUrl.Url)
}