Skip to content

Commit a76937e

Browse files
committed
feat: Add listing proxies to cli 'coder proxy ls'
1 parent a1db825 commit a76937e

File tree

2 files changed

+49
-4
lines changed

2 files changed

+49
-4
lines changed

codersdk/workspaceproxy.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ const (
2929
)
3030

3131
type WorkspaceProxyStatus struct {
32-
Status ProxyHealthStatus `json:"status" table:"status"`
32+
Status ProxyHealthStatus `json:"status" table:"status,default_sort"`
3333
// Report provides more information about the health of the workspace proxy.
3434
Report ProxyHealthReport `json:"report,omitempty" table:"report"`
3535
CheckedAt time.Time `json:"checked_at" table:"checked_at" format:"date-time"`
@@ -60,11 +60,11 @@ type WorkspaceProxy struct {
6060
// Status is the latest status check of the proxy. This will be empty for deleted
6161
// proxies. This value can be used to determine if a workspace proxy is healthy
6262
// and ready to use.
63-
Status WorkspaceProxyStatus `json:"status,omitempty" table:"status"`
63+
Status WorkspaceProxyStatus `json:"status,omitempty" table:"status,recursive"`
6464
}
6565

6666
type CreateWorkspaceProxyRequest struct {
67-
Name string `json:"name"`
67+
Name string `json:"name" validate:"required"`
6868
DisplayName string `json:"display_name"`
6969
Icon string `json:"icon"`
7070
}

enterprise/cli/workspaceproxy.go

+46-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ func (r *RootCmd) workspaceProxy() *clibase.Cmd {
2323
r.proxyServer(),
2424
r.createProxy(),
2525
r.deleteProxy(),
26+
r.listProxies(),
2627
},
2728
}
2829

@@ -66,7 +67,8 @@ func (r *RootCmd) createProxy() *clibase.Cmd {
6667
if !ok {
6768
return nil, xerrors.Errorf("unexpected type %T", data)
6869
}
69-
return fmt.Sprintf("Workspace Proxy %q registered successfully\nToken: %s", response.Proxy.Name, response.ProxyToken), nil
70+
return fmt.Sprintf("Workspace Proxy %q created successfully. Save this token, it will not be shown again."+
71+
"\nToken: %s", response.Proxy.Name, response.ProxyToken), nil
7072
}),
7173
cliui.JSONFormat(),
7274
// Table formatter expects a slice, make a slice of one.
@@ -91,6 +93,10 @@ func (r *RootCmd) createProxy() *clibase.Cmd {
9193
),
9294
Handler: func(inv *clibase.Invocation) error {
9395
ctx := inv.Context()
96+
if proxyName == "" {
97+
return xerrors.Errorf("proxy name is required")
98+
}
99+
94100
resp, err := client.CreateWorkspaceProxy(ctx, codersdk.CreateWorkspaceProxyRequest{
95101
Name: proxyName,
96102
DisplayName: displayName,
@@ -140,3 +146,42 @@ func (r *RootCmd) createProxy() *clibase.Cmd {
140146
)
141147
return cmd
142148
}
149+
150+
func (r *RootCmd) listProxies() *clibase.Cmd {
151+
var (
152+
formatter = cliui.NewOutputFormatter(
153+
cliui.TableFormat([]codersdk.WorkspaceProxy{}, []string{"name", "url", "status status"}),
154+
cliui.JSONFormat(),
155+
cliui.TextFormat(),
156+
)
157+
)
158+
159+
client := new(codersdk.Client)
160+
cmd := &clibase.Cmd{
161+
Use: "ls",
162+
Aliases: []string{"list"},
163+
Short: "List all workspace proxies",
164+
Middleware: clibase.Chain(
165+
clibase.RequireNArgs(0),
166+
r.InitClient(client),
167+
),
168+
Handler: func(inv *clibase.Invocation) error {
169+
ctx := inv.Context()
170+
proxies, err := client.WorkspaceProxies(ctx)
171+
if err != nil {
172+
return xerrors.Errorf("list workspace proxies: %w", err)
173+
}
174+
175+
output, err := formatter.Format(ctx, proxies)
176+
if err != nil {
177+
return err
178+
}
179+
180+
_, err = fmt.Fprintln(inv.Stdout, output)
181+
return err
182+
},
183+
}
184+
185+
formatter.AttachOptions(&cmd.Options)
186+
return cmd
187+
}

0 commit comments

Comments
 (0)