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

Commit 85e7bbe

Browse files
committed
feat: Reveal tokens subcommand
* 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 b8067c0 commit 85e7bbe

File tree

8 files changed

+175
-7
lines changed

8 files changed

+175
-7
lines changed

ci/integration/integration_test.go

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

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

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: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
11
package cmd
22

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

68
"cdr.dev/coder-cli/coder-sdk"
79
"cdr.dev/coder-cli/internal/x/xcobra"
810
"cdr.dev/coder-cli/pkg/tablewriter"
11+
912
"github.com/spf13/cobra"
13+
"golang.org/x/xerrors"
1014
)
1115

1216
func tokensCmd() *cobra.Command {
1317
cmd := &cobra.Command{
14-
Use: "tokens",
15-
Short: "manage Coder API tokens for the active user",
16-
Hidden: true,
18+
Use: "tokens",
19+
Short: "manage Coder API tokens for the active user",
1720
Long: "Create and manage API Tokens for authenticating the CLI.\n" +
1821
"Statically authenticate using the token value with the " + "`" + "CODER_TOKEN" + "`" + " and " + "`" + "CODER_URL" + "`" + " environment variables.",
1922
}
@@ -27,7 +30,9 @@ func tokensCmd() *cobra.Command {
2730
}
2831

2932
func lsTokensCmd() *cobra.Command {
30-
return &cobra.Command{
33+
var outputFmt string
34+
35+
cmd := &cobra.Command{
3136
Use: "ls",
3237
Short: "show the user's active API tokens",
3338
RunE: func(cmd *cobra.Command, args []string) error {
@@ -42,14 +47,30 @@ func lsTokensCmd() *cobra.Command {
4247
return err
4348
}
4449

45-
err = tablewriter.WriteTable(len(tokens), func(i int) interface{} { return tokens[i] })
46-
if err != nil {
47-
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)
4865
}
4966

5067
return nil
5168
},
5269
}
70+
71+
cmd.Flags().StringVarP(&outputFmt, "output", "o", humanOutput, "human | json")
72+
73+
return cmd
5374
}
5475

5576
func createTokensCmd() *cobra.Command {

0 commit comments

Comments
 (0)