Skip to content

chore: Add listing proxies to cli 'coder proxy ls' #7376

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 3, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
feat: Add listing proxies to cli 'coder proxy ls'
  • Loading branch information
Emyrk committed May 2, 2023
commit a76937e08e9cfe2bc9501c3d9632fcdb145ff57c
6 changes: 3 additions & 3 deletions codersdk/workspaceproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const (
)

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

type CreateWorkspaceProxyRequest struct {
Name string `json:"name"`
Name string `json:"name" validate:"required"`
DisplayName string `json:"display_name"`
Icon string `json:"icon"`
}
Expand Down
47 changes: 46 additions & 1 deletion enterprise/cli/workspaceproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func (r *RootCmd) workspaceProxy() *clibase.Cmd {
r.proxyServer(),
r.createProxy(),
r.deleteProxy(),
r.listProxies(),
},
}

Expand Down Expand Up @@ -66,7 +67,8 @@ func (r *RootCmd) createProxy() *clibase.Cmd {
if !ok {
return nil, xerrors.Errorf("unexpected type %T", data)
}
return fmt.Sprintf("Workspace Proxy %q registered successfully\nToken: %s", response.Proxy.Name, response.ProxyToken), nil
return fmt.Sprintf("Workspace Proxy %q created successfully. Save this token, it will not be shown again."+
"\nToken: %s", response.Proxy.Name, response.ProxyToken), nil
}),
cliui.JSONFormat(),
// Table formatter expects a slice, make a slice of one.
Expand All @@ -91,6 +93,10 @@ func (r *RootCmd) createProxy() *clibase.Cmd {
),
Handler: func(inv *clibase.Invocation) error {
ctx := inv.Context()
if proxyName == "" {
return xerrors.Errorf("proxy name is required")
}

resp, err := client.CreateWorkspaceProxy(ctx, codersdk.CreateWorkspaceProxyRequest{
Name: proxyName,
DisplayName: displayName,
Expand Down Expand Up @@ -140,3 +146,42 @@ func (r *RootCmd) createProxy() *clibase.Cmd {
)
return cmd
}

func (r *RootCmd) listProxies() *clibase.Cmd {
var (
formatter = cliui.NewOutputFormatter(
cliui.TableFormat([]codersdk.WorkspaceProxy{}, []string{"name", "url", "status status"}),
cliui.JSONFormat(),
cliui.TextFormat(),
)
)

client := new(codersdk.Client)
cmd := &clibase.Cmd{
Use: "ls",
Aliases: []string{"list"},
Short: "List all workspace proxies",
Middleware: clibase.Chain(
clibase.RequireNArgs(0),
r.InitClient(client),
),
Handler: func(inv *clibase.Invocation) error {
ctx := inv.Context()
proxies, err := client.WorkspaceProxies(ctx)
if err != nil {
return xerrors.Errorf("list workspace proxies: %w", err)
}

output, err := formatter.Format(ctx, proxies)
if err != nil {
return err
}

_, err = fmt.Fprintln(inv.Stdout, output)
return err
},
}

formatter.AttachOptions(&cmd.Options)
return cmd
}