diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..485dee64 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea diff --git a/README.md b/README.md index 1f30ec25..7fb1254f 100644 --- a/README.md +++ b/README.md @@ -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. `` @@ -30,6 +29,14 @@ $ coder sync ~/Projects/cdr/enterprise/. my-env:~/enterprise You can access your environment's terminal with `coder sh `. You can also execute a command in your environment with `coder sh [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 diff --git a/cmd/coder/main.go b/cmd/coder/main.go index 3d29e88f..3553eef4 100644 --- a/cmd/coder/main.go +++ b/cmd/coder/main.go @@ -30,6 +30,7 @@ func (r *rootCmd) Subcommands() []cli.Command { &logoutCmd{}, &shellCmd{}, &syncCmd{}, + &urlCmd{}, } } diff --git a/cmd/coder/url.go b/cmd/coder/url.go new file mode 100644 index 00000000..59f61809 --- /dev/null +++ b/cmd/coder/url.go @@ -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: " ", + 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) + + 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) +}