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

Commit b3e8ec9

Browse files
committed
Hide resources command from docs
1 parent 2ae449e commit b3e8ec9

File tree

6 files changed

+49
-92
lines changed

6 files changed

+49
-92
lines changed

ci/steps/gendocs.sh

+3-2
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,16 @@ echo "Generating docs..."
77
cd "$(dirname "$0")"
88
cd ../../
99

10+
rm -rf ./docs
11+
mkdir ./docs
1012
go run ./cmd/coder gen-docs ./docs
1113

1214
# remove cobra footer from each file
1315
for filename in ./docs/*.md; do
1416
trimmed=$(head -n -1 "$filename")
15-
echo "$trimmed" > $filename
17+
echo "$trimmed" >$filename
1618
done
1719

18-
1920
if [[ ${CI-} && $(git ls-files --other --modified --exclude-standard) ]]; then
2021
echo "Documentation needs generation:"
2122
git -c color.ui=always status | grep --color=no '\e\[31m'

coder-sdk/env.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,9 @@ func (c Client) CreateEnvironment(ctx context.Context, orgID string, req CreateE
9393
return &env, nil
9494
}
9595

96-
// ListEnvironments lists environments returned by the given filter.
97-
// TODO: add the filter options
98-
func (c Client) ListEnvironments(ctx context.Context) ([]Environment, error) {
96+
// Environments lists environments returned by the given filter.
97+
// TODO: add the filter options, explore performance issues
98+
func (c Client) Environments(ctx context.Context) ([]Environment, error) {
9999
var envs []Environment
100100
if err := c.requestBody(ctx, http.MethodGet, "/api/environments", nil, &envs); err != nil {
101101
return nil, err

docs/coder.md

-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ coder provides a CLI for working with an existing Coder Enterprise installation
2020
* [coder envs](coder_envs.md) - Interact with Coder environments
2121
* [coder login](coder_login.md) - Authenticate this client for future operations
2222
* [coder logout](coder_logout.md) - Remove local authentication credentials if any exist
23-
* [coder resources](coder_resources.md) - manager Coder resources with platform-level context (users, organizations, environments)
2423
* [coder secrets](coder_secrets.md) - Interact with Coder Secrets
2524
* [coder sh](coder_sh.md) - Open a shell and execute commands in a Coder environment
2625
* [coder sync](coder_sync.md) - Establish a one way directory sync to a Coder environment

docs/coder_resources.md

-24
This file was deleted.

docs/coder_resources_top.md

-27
This file was deleted.

internal/cmd/resourcemanager.go

+43-35
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,21 @@ package cmd
22

33
import (
44
"fmt"
5+
"io"
56
"os"
67
"sort"
78
"text/tabwriter"
89

910
"cdr.dev/coder-cli/coder-sdk"
1011
"github.com/spf13/cobra"
12+
"golang.org/x/xerrors"
1113
)
1214

1315
func makeResourceCmd() *cobra.Command {
1416
cmd := &cobra.Command{
15-
Use: "resources",
16-
Short: "manager Coder resources with platform-level context (users, organizations, environments)",
17+
Use: "resources",
18+
Short: "manager Coder resources with platform-level context (users, organizations, environments)",
19+
Hidden: true,
1720
}
1821
cmd.AddCommand(resourceTop())
1922
return cmd
@@ -24,15 +27,16 @@ func resourceTop() *cobra.Command {
2427
Use: "top",
2528
RunE: func(cmd *cobra.Command, args []string) error {
2629
ctx := cmd.Context()
27-
2830
client, err := newClient()
2931
if err != nil {
3032
return err
3133
}
3234

33-
envs, err := client.ListEnvironments(ctx)
35+
// NOTE: it's not worth parrallelizing these calls yet given that this specific endpoint
36+
// takes about 20x times longer than the other two
37+
envs, err := client.Environments(ctx)
3438
if err != nil {
35-
return err
39+
return xerrors.Errorf("get environments %w", err)
3640
}
3741

3842
userEnvs := make(map[string][]coder.Environment)
@@ -42,51 +46,55 @@ func resourceTop() *cobra.Command {
4246

4347
users, err := client.Users(ctx)
4448
if err != nil {
45-
return err
49+
return xerrors.Errorf("get users: %w", err)
4650
}
4751

48-
orgs := make(map[string]coder.Organization)
52+
orgIDMap := make(map[string]coder.Organization)
4953
orglist, err := client.Organizations(ctx)
5054
if err != nil {
51-
return err
55+
return xerrors.Errorf("get organizations: %w", err)
5256
}
5357
for _, o := range orglist {
54-
orgs[o.ID] = o
55-
}
56-
57-
tabwriter := tabwriter.NewWriter(os.Stdout, 0, 0, 4, ' ', 0)
58-
var userResources []aggregatedUser
59-
for _, u := range users {
60-
// truncate user names to ensure tabwriter doesn't push our entire table too far
61-
u.Name = truncate(u.Name, 20, "...")
62-
userResources = append(userResources, aggregatedUser{User: u, resources: aggregateEnvResources(userEnvs[u.ID])})
63-
}
64-
sort.Slice(userResources, func(i, j int) bool {
65-
return userResources[i].cpuAllocation > userResources[j].cpuAllocation
66-
})
67-
68-
for _, u := range userResources {
69-
_, _ = fmt.Fprintf(tabwriter, "%s\t(%s)\t%s", u.Name, u.Email, u.resources)
70-
if verbose {
71-
if len(userEnvs[u.ID]) > 0 {
72-
_, _ = fmt.Fprintf(tabwriter, "\f")
73-
}
74-
for _, env := range userEnvs[u.ID] {
75-
_, _ = fmt.Fprintf(tabwriter, "\t")
76-
_, _ = fmt.Fprintln(tabwriter, fmtEnvResources(env, orgs))
77-
}
78-
}
79-
fmt.Fprint(tabwriter, "\n")
58+
orgIDMap[o.ID] = o
8059
}
81-
_ = tabwriter.Flush()
8260

61+
printResourceTop(os.Stdout, users, orgIDMap, userEnvs)
8362
return nil
8463
},
8564
}
8665

8766
return cmd
8867
}
8968

69+
func printResourceTop(writer io.Writer, users []coder.User, orgIDMap map[string]coder.Organization, userEnvs map[string][]coder.Environment) {
70+
tabwriter := tabwriter.NewWriter(writer, 0, 0, 4, ' ', 0)
71+
defer func() { _ = tabwriter.Flush() }()
72+
73+
var userResources []aggregatedUser
74+
for _, u := range users {
75+
// truncate user names to ensure tabwriter doesn't push our entire table too far
76+
u.Name = truncate(u.Name, 20, "...")
77+
userResources = append(userResources, aggregatedUser{User: u, resources: aggregateEnvResources(userEnvs[u.ID])})
78+
}
79+
sort.Slice(userResources, func(i, j int) bool {
80+
return userResources[i].cpuAllocation > userResources[j].cpuAllocation
81+
})
82+
83+
for _, u := range userResources {
84+
_, _ = fmt.Fprintf(tabwriter, "%s\t(%s)\t%s", u.Name, u.Email, u.resources)
85+
if verbose {
86+
if len(userEnvs[u.ID]) > 0 {
87+
_, _ = fmt.Fprintf(tabwriter, "\f")
88+
}
89+
for _, env := range userEnvs[u.ID] {
90+
_, _ = fmt.Fprintf(tabwriter, "\t")
91+
_, _ = fmt.Fprintln(tabwriter, fmtEnvResources(env, orgIDMap))
92+
}
93+
}
94+
_, _ = fmt.Fprint(tabwriter, "\n")
95+
}
96+
}
97+
9098
type aggregatedUser struct {
9199
coder.User
92100
resources

0 commit comments

Comments
 (0)