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

Commit a02fdfe

Browse files
author
Anthony Shull
authored
Merge pull request #15 from cdr/envs
Added ability to get all active environments
2 parents a1fb1d3 + c5548c0 commit a02fdfe

File tree

4 files changed

+56
-6
lines changed

4 files changed

+56
-6
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@ $ coder sync ~/Projects/cdr/enterprise/. my-env:~/enterprise
2929
You can access your environment's terminal with `coder sh <env>`. You can also
3030
execute a command in your environment with `coder sh <env> [command] [args]`.
3131

32+
## Environments
33+
34+
List all active environments for the user.
35+
36+
``
37+
$ coder envs
38+
``
39+
3240
## Development URLs
3341

3442
You can retrieve the devurl of an environment.

cmd/coder/ceapi.go

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ outer:
2424
return uo
2525
}
2626

27-
func findEnv(client *entclient.Client, name string) entclient.Environment {
27+
// getEnvs returns all environments for the user.
28+
func getEnvs(client *entclient.Client) []entclient.Environment {
2829
me, err := client.Me()
2930
if err != nil {
3031
flog.Fatal("get self: %+v", err)
@@ -37,21 +38,34 @@ func findEnv(client *entclient.Client, name string) entclient.Environment {
3738

3839
orgs = userOrgs(me, orgs)
3940

40-
var found []string
41+
var allEnvs []entclient.Environment
4142

4243
for _, org := range orgs {
4344
envs, err := client.Envs(me, org)
4445
if err != nil {
4546
flog.Fatal("get envs for %v: %+v", org.Name, err)
4647
}
4748
for _, env := range envs {
48-
found = append(found, env.Name)
49-
if env.Name != name {
50-
continue
51-
}
49+
allEnvs = append(allEnvs, env)
50+
}
51+
}
52+
53+
return allEnvs
54+
}
55+
56+
// findEnv returns a single environment by name (if it exists.)
57+
func findEnv(client *entclient.Client, name string) entclient.Environment {
58+
envs := getEnvs(client)
59+
60+
var found []string
61+
62+
for _, env := range envs {
63+
found = append(found, env.Name)
64+
if env.Name == name {
5265
return env
5366
}
5467
}
68+
5569
flog.Info("found %q", found)
5670
flog.Fatal("environment %q not found", name)
5771
panic("unreachable")

cmd/coder/envs.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"github.com/spf13/pflag"
6+
"go.coder.com/cli"
7+
)
8+
9+
type envsCmd struct {
10+
}
11+
12+
func (cmd envsCmd) Spec() cli.CommandSpec {
13+
return cli.CommandSpec{
14+
Name: "envs",
15+
Desc: "get a list of active environment",
16+
}
17+
}
18+
19+
func (cmd envsCmd) Run(fl *pflag.FlagSet) {
20+
entClient := requireAuth()
21+
22+
envs := getEnvs(entClient)
23+
24+
for _, env := range envs {
25+
fmt.Println(env.Name)
26+
}
27+
}

cmd/coder/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ func (r *rootCmd) Spec() cli.CommandSpec {
2626

2727
func (r *rootCmd) Subcommands() []cli.Command {
2828
return []cli.Command{
29+
&envsCmd{},
2930
&loginCmd{},
3031
&logoutCmd{},
3132
&shellCmd{},

0 commit comments

Comments
 (0)