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

Commit be6bc28

Browse files
committed
Add completions for env names to be enabled later
1 parent 74a130a commit be6bc28

File tree

6 files changed

+64
-26
lines changed

6 files changed

+64
-26
lines changed

ci/integration/setup_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"golang.org/x/xerrors"
1414
)
1515

16+
// binpath is populated during package initialization with a path to the coder binary
1617
var binpath string
1718

1819
// initialize integration tests by building the coder-cli binary

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/secrets.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,18 @@ func makeSecretsCmd() *cobra.Command {
2828
},
2929
makeCreateSecret(),
3030
&cobra.Command{
31-
Use: "rm [...secret_name]",
32-
Short: "Remove one or more secrets by name",
33-
Args: cobra.MinimumNArgs(1),
34-
RunE: removeSecrets,
31+
Use: "rm [...secret_name]",
32+
Short: "Remove one or more secrets by name",
33+
Args: cobra.MinimumNArgs(1),
34+
RunE: removeSecrets,
35+
Example: "coder secrets rm mysql-password mysql-user",
3536
},
3637
&cobra.Command{
37-
Use: "view [secret_name]",
38-
Short: "View a secret by name",
39-
Args: cobra.ExactArgs(1),
40-
RunE: viewSecret,
38+
Use: "view [secret_name]",
39+
Short: "View a secret by name",
40+
Args: cobra.ExactArgs(1),
41+
RunE: viewSecret,
42+
Example: "coder secrets view mysql-password",
4143
},
4244
)
4345
return cmd
@@ -55,6 +57,9 @@ func makeCreateSecret() *cobra.Command {
5557
Use: "create [secret_name]",
5658
Short: "Create a new secret",
5759
Long: "Create a new secret object to store application secrets and access them securely from within your environments.",
60+
Example: `coder secrets create mysql-password --from-literal 123password
61+
coder secrets create mysql-password --from-prompt
62+
coder secrets create aws-credentials --from-file ./credentials.json`,
5863
Args: func(cmd *cobra.Command, args []string) error {
5964
if len(args) < 1 {
6065
return xerrors.Errorf("[secret_name] is a required argument")

cmd/coder/shell.go

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

22+
func getEnvsForCompletion() []string {
23+
// TODO(@cmoog): Enable this if speed issue can be resolved. Otherwise, all commands will take > 1 second.
24+
return nil
25+
26+
var envNames []string
27+
client, err := newClient()
28+
if err != nil {
29+
return envNames
30+
}
31+
envs, err := getEnvs(client)
32+
if err != nil {
33+
return envNames
34+
}
35+
for _, e := range envs {
36+
envNames = append(envNames, e.Name)
37+
}
38+
return envNames
39+
}
40+
2241
func makeShellCmd() *cobra.Command {
2342
return &cobra.Command{
2443
Use: "sh [environment_name] [<command [args...]>]",
2544
Short: "Open a shell and execute commands in a Coder environment",
2645
Long: "Execute a remote command on the environment\\nIf no command is specified, the default shell is opened.",
2746
Args: cobra.MinimumNArgs(1),
2847
DisableFlagParsing: true,
48+
ValidArgs: getEnvsForCompletion(),
2949
RunE: shell,
50+
Example: "coder sh backend-env",
3051
}
3152
}
3253

33-
func shell(cmd *cobra.Command, cmdArgs []string) error {
54+
func shell(_ *cobra.Command, cmdArgs []string) error {
3455
var (
3556
envName = cmdArgs[0]
3657
ctx = context.Background()

cmd/coder/urls.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@ func makeURLCmd() *cobra.Command {
2323
Short: "Interact with environment DevURLs",
2424
}
2525
lsCmd := &cobra.Command{
26-
Use: "ls [env_name]",
27-
Short: "List all DevURLs for an environment",
28-
Args: cobra.ExactArgs(1),
29-
RunE: makeListDevURLs(&outputFmt),
26+
Use: "ls [environment_name]",
27+
Short: "List all DevURLs for an environment",
28+
Args: cobra.ExactArgs(1),
29+
ValidArgs: getEnvsForCompletion(),
30+
RunE: makeListDevURLs(&outputFmt),
3031
}
3132
lsCmd.Flags().StringVarP(&outputFmt, "output", "o", "human", "human|json")
3233

cmd/coder/users.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@ func makeUsersCmd() *cobra.Command {
1919
lsCmd := &cobra.Command{
2020
Use: "ls",
2121
Short: "list all user accounts",
22-
RunE: listUsers(&outputFmt),
22+
Example: `coder users ls -o json
23+
coder users ls -o json | jq .[] | jq -r .email`,
24+
RunE: listUsers(&outputFmt),
2325
}
24-
lsCmd.Flags().StringVarP(&outputFmt, "output", "0", "human", "human | json")
26+
lsCmd.Flags().StringVarP(&outputFmt, "output", "o", "human", "human | json")
2527

2628
cmd.AddCommand(lsCmd)
2729
return cmd

0 commit comments

Comments
 (0)