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

Commit 4752bfc

Browse files
committed
Add completions for env names
1 parent aa300a2 commit 4752bfc

File tree

2 files changed

+37
-12
lines changed

2 files changed

+37
-12
lines changed

cmd/coder/auth.go

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,38 @@ import (
55

66
"cdr.dev/coder-cli/internal/config"
77
"cdr.dev/coder-cli/internal/entclient"
8+
"golang.org/x/xerrors"
89

910
"go.coder.com/flog"
1011
)
1112

1213
// requireAuth exits the process with a nonzero exit code if the user is not authenticated to make requests
1314
func requireAuth() *entclient.Client {
15+
client, err := newClient()
16+
if err != nil {
17+
flog.Fatal("%v", err)
18+
}
19+
return client
20+
}
21+
22+
func newClient() (*entclient.Client, error) {
1423
sessionToken, err := config.Session.Read()
15-
requireSuccess(err, "read session: %v (did you run coder login?)", err)
24+
if err != nil {
25+
return nil, xerrors.Errorf("read session: %v (did you run coder login?)", err)
26+
}
1627

1728
rawURL, err := config.URL.Read()
18-
requireSuccess(err, "read url: %v (did you run coder login?)", err)
29+
if err != nil {
30+
return nil, xerrors.Errorf("read url: %v (did you run coder login?)", err)
31+
}
1932

2033
u, err := url.Parse(rawURL)
21-
requireSuccess(err, "url misformatted: %v (try runing coder login)", err)
34+
if err != nil {
35+
return nil, xerrors.Errorf("url misformatted: %v (try runing coder login)", err)
36+
}
2237

2338
return &entclient.Client{
2439
BaseURL: u,
2540
Token: sessionToken,
26-
}
27-
}
28-
29-
// requireSuccess prints the given message and format args as a fatal error if err != nil
30-
func requireSuccess(err error, msg string, args ...interface{}) {
31-
if err != nil {
32-
flog.Fatal(msg, args...)
33-
}
41+
}, nil
3442
}

cmd/coder/shell.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,35 @@ import (
1919
"go.coder.com/flog"
2020
)
2121

22+
func getEnvsForCompletion() []string {
23+
var envNames []string
24+
client, err := newClient()
25+
if err != nil {
26+
return envNames
27+
}
28+
envs, err := getEnvs(client)
29+
if err != nil {
30+
return envNames
31+
}
32+
for _, e := range envs {
33+
envNames = append(envNames, e.Name)
34+
}
35+
return envNames
36+
}
37+
2238
func makeShellCmd() *cobra.Command {
2339
return &cobra.Command{
2440
Use: "sh [environment_name] [<command [args...]>]",
2541
Short: "Open a shell and execute commands in a Coder environment",
2642
Long: "Execute a remote command on the environment\\nIf no command is specified, the default shell is opened.",
2743
Args: cobra.MinimumNArgs(1),
2844
DisableFlagParsing: true,
45+
ValidArgs: getEnvsForCompletion(),
2946
RunE: shell,
3047
}
3148
}
3249

33-
func shell(cmd *cobra.Command, cmdArgs []string) error {
50+
func shell(_ *cobra.Command, cmdArgs []string) error {
3451
var (
3552
envName = cmdArgs[0]
3653
ctx = context.Background()

0 commit comments

Comments
 (0)