Skip to content

Commit 96c67b8

Browse files
authored
feat: Reveal tokens subcommand (coder#229)
* Mark tokens subcommand as visible * Re-generate Markdown documentation for the commands * Add JSON output for "tokens ls" command, as with "envs ls" * Add integration test cases
1 parent 460c53f commit 96c67b8

File tree

8 files changed

+174
-7
lines changed

8 files changed

+174
-7
lines changed

ci/integration/integration_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,22 @@ func TestCoderCLI(t *testing.T) {
6161
tcli.Success(),
6262
)
6363

64+
c.Run(ctx, "coder envs ls -o json").Assert(t,
65+
tcli.Success(),
66+
)
67+
68+
c.Run(ctx, "coder tokens").Assert(t,
69+
tcli.Success(),
70+
)
71+
72+
c.Run(ctx, "coder tokens ls").Assert(t,
73+
tcli.Success(),
74+
)
75+
76+
c.Run(ctx, "coder tokens ls -o json").Assert(t,
77+
tcli.Success(),
78+
)
79+
6480
c.Run(ctx, "coder urls").Assert(t,
6581
tcli.Success(),
6682
)
@@ -80,6 +96,10 @@ func TestCoderCLI(t *testing.T) {
8096
c.Run(ctx, "coder envs ls").Assert(t,
8197
tcli.Error(),
8298
)
99+
100+
c.Run(ctx, "coder tokens ls").Assert(t,
101+
tcli.Error(),
102+
)
83103
})
84104
}
85105

docs/coder.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ coder provides a CLI for working with an existing Coder Enterprise installation
1919
* [coder logout](coder_logout.md) - Remove local authentication credentials if any exist
2020
* [coder sh](coder_sh.md) - Open a shell and execute commands in a Coder environment
2121
* [coder sync](coder_sync.md) - Establish a one way directory sync to a Coder environment
22+
* [coder tokens](coder_tokens.md) - manage Coder API tokens for the active user
2223
* [coder urls](coder_urls.md) - Interact with environment DevURLs
2324
* [coder users](coder_users.md) - Interact with Coder user accounts
2425

docs/coder_tokens.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
## coder tokens
2+
3+
manage Coder API tokens for the active user
4+
5+
### Synopsis
6+
7+
Create and manage API Tokens for authenticating the CLI.
8+
Statically authenticate using the token value with the `CODER_TOKEN` and `CODER_URL` environment variables.
9+
10+
### Options
11+
12+
```
13+
-h, --help help for tokens
14+
```
15+
16+
### Options inherited from parent commands
17+
18+
```
19+
-v, --verbose show verbose output
20+
```
21+
22+
### SEE ALSO
23+
24+
* [coder](coder.md) - coder provides a CLI for working with an existing Coder Enterprise installation
25+
* [coder tokens create](coder_tokens_create.md) - create generates a new API token and prints it to stdout
26+
* [coder tokens ls](coder_tokens_ls.md) - show the user's active API tokens
27+
* [coder tokens regen](coder_tokens_regen.md) - regenerate an API token by its unique ID and print the new token to stdout
28+
* [coder tokens rm](coder_tokens_rm.md) - remove an API token by its unique ID
29+

docs/coder_tokens_create.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
## coder tokens create
2+
3+
create generates a new API token and prints it to stdout
4+
5+
```
6+
coder tokens create [token_name] [flags]
7+
```
8+
9+
### Options
10+
11+
```
12+
-h, --help help for create
13+
```
14+
15+
### Options inherited from parent commands
16+
17+
```
18+
-v, --verbose show verbose output
19+
```
20+
21+
### SEE ALSO
22+
23+
* [coder tokens](coder_tokens.md) - manage Coder API tokens for the active user
24+

docs/coder_tokens_ls.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
## coder tokens ls
2+
3+
show the user's active API tokens
4+
5+
```
6+
coder tokens ls [flags]
7+
```
8+
9+
### Options
10+
11+
```
12+
-h, --help help for ls
13+
-o, --output string human | json (default "human")
14+
```
15+
16+
### Options inherited from parent commands
17+
18+
```
19+
-v, --verbose show verbose output
20+
```
21+
22+
### SEE ALSO
23+
24+
* [coder tokens](coder_tokens.md) - manage Coder API tokens for the active user
25+

docs/coder_tokens_regen.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
## coder tokens regen
2+
3+
regenerate an API token by its unique ID and print the new token to stdout
4+
5+
```
6+
coder tokens regen [token_id] [flags]
7+
```
8+
9+
### Options
10+
11+
```
12+
-h, --help help for regen
13+
```
14+
15+
### Options inherited from parent commands
16+
17+
```
18+
-v, --verbose show verbose output
19+
```
20+
21+
### SEE ALSO
22+
23+
* [coder tokens](coder_tokens.md) - manage Coder API tokens for the active user
24+

docs/coder_tokens_rm.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
## coder tokens rm
2+
3+
remove an API token by its unique ID
4+
5+
```
6+
coder tokens rm [token_id] [flags]
7+
```
8+
9+
### Options
10+
11+
```
12+
-h, --help help for rm
13+
```
14+
15+
### Options inherited from parent commands
16+
17+
```
18+
-v, --verbose show verbose output
19+
```
20+
21+
### SEE ALSO
22+
23+
* [coder tokens](coder_tokens.md) - manage Coder API tokens for the active user
24+

internal/cmd/tokens.go

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package cmd
22

33
import (
4+
"encoding/json"
45
"fmt"
6+
"os"
57

68
"github.com/spf13/cobra"
9+
"golang.org/x/xerrors"
710

811
"cdr.dev/coder-cli/coder-sdk"
912
"cdr.dev/coder-cli/internal/x/xcobra"
@@ -12,9 +15,8 @@ import (
1215

1316
func tokensCmd() *cobra.Command {
1417
cmd := &cobra.Command{
15-
Use: "tokens",
16-
Hidden: true,
17-
Short: "manage Coder API tokens for the active user",
18+
Use: "tokens",
19+
Short: "manage Coder API tokens for the active user",
1820
Long: "Create and manage API Tokens for authenticating the CLI.\n" +
1921
"Statically authenticate using the token value with the " + "`" + "CODER_TOKEN" + "`" + " and " + "`" + "CODER_URL" + "`" + " environment variables.",
2022
}
@@ -28,7 +30,9 @@ func tokensCmd() *cobra.Command {
2830
}
2931

3032
func lsTokensCmd() *cobra.Command {
31-
return &cobra.Command{
33+
var outputFmt string
34+
35+
cmd := &cobra.Command{
3236
Use: "ls",
3337
Short: "show the user's active API tokens",
3438
RunE: func(cmd *cobra.Command, args []string) error {
@@ -43,14 +47,30 @@ func lsTokensCmd() *cobra.Command {
4347
return err
4448
}
4549

46-
err = tablewriter.WriteTable(len(tokens), func(i int) interface{} { return tokens[i] })
47-
if err != nil {
48-
return err
50+
switch outputFmt {
51+
case humanOutput:
52+
err := tablewriter.WriteTable(len(tokens), func(i int) interface{} {
53+
return tokens[i]
54+
})
55+
if err != nil {
56+
return xerrors.Errorf("write table: %w", err)
57+
}
58+
case jsonOutput:
59+
err := json.NewEncoder(os.Stdout).Encode(tokens)
60+
if err != nil {
61+
return xerrors.Errorf("write tokens as JSON: %w", err)
62+
}
63+
default:
64+
return xerrors.Errorf("unknown --output value %q", outputFmt)
4965
}
5066

5167
return nil
5268
},
5369
}
70+
71+
cmd.Flags().StringVarP(&outputFmt, "output", "o", humanOutput, "human | json")
72+
73+
return cmd
5474
}
5575

5676
func createTokensCmd() *cobra.Command {

0 commit comments

Comments
 (0)