Skip to content

Commit 4d7daed

Browse files
committed
Add output formats for registering a proxy
1 parent 56d0ad7 commit 4d7daed

File tree

2 files changed

+91
-21
lines changed

2 files changed

+91
-21
lines changed

codersdk/workspaceproxy.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,16 @@ import (
1212
)
1313

1414
type WorkspaceProxy struct {
15-
ID uuid.UUID `db:"id" json:"id" format:"uuid"`
16-
Name string `db:"name" json:"name"`
17-
Icon string `db:"icon" json:"icon"`
15+
ID uuid.UUID `db:"id" json:"id" format:"uuid" table:"id"`
16+
Name string `db:"name" json:"name" table:"name,default_sort"`
17+
Icon string `db:"icon" json:"icon" table:"icon"`
1818
// Full url including scheme of the proxy api url: https://us.example.com
19-
URL string `db:"url" json:"url"`
19+
URL string `db:"url" json:"url" table:"url"`
2020
// WildcardHostname with the wildcard for subdomain based app hosting: *.us.example.com
21-
WildcardHostname string `db:"wildcard_hostname" json:"wildcard_hostname"`
22-
CreatedAt time.Time `db:"created_at" json:"created_at" format:"date-time"`
23-
UpdatedAt time.Time `db:"updated_at" json:"updated_at" format:"date-time"`
24-
Deleted bool `db:"deleted" json:"deleted"`
21+
WildcardHostname string `db:"wildcard_hostname" json:"wildcard_hostname" table:"wildcard_hostname"`
22+
CreatedAt time.Time `db:"created_at" json:"created_at" format:"date-time" table:"created_at"`
23+
UpdatedAt time.Time `db:"updated_at" json:"updated_at" format:"date-time" table:"updated_at"`
24+
Deleted bool `db:"deleted" json:"deleted" table:"deleted"`
2525
}
2626

2727
type CreateWorkspaceProxyRequest struct {
@@ -33,7 +33,7 @@ type CreateWorkspaceProxyRequest struct {
3333
}
3434

3535
type CreateWorkspaceProxyResponse struct {
36-
Proxy WorkspaceProxy `json:"proxy"`
36+
Proxy WorkspaceProxy `json:"proxy" table:"proxy,recursive,default_sort"`
3737
ProxyToken string `json:"proxy_token"`
3838
}
3939

enterprise/cli/workspaceproxy.go

Lines changed: 82 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -53,33 +53,103 @@ func (r *RootCmd) workspaceProxy() *clibase.Cmd {
5353
}
5454

5555
func (r *RootCmd) registerProxy() *clibase.Cmd {
56+
var (
57+
proxyName string
58+
displayName string
59+
proxyIcon string
60+
proxyURL string
61+
proxyWildcardHostname string
62+
onlyToken bool
63+
formatter = cliui.NewOutputFormatter(
64+
// Text formatter should be human readable.
65+
cliui.ChangeFormatterData(cliui.TextFormat(), func(data any) (any, error) {
66+
response, ok := data.(codersdk.CreateWorkspaceProxyResponse)
67+
if !ok {
68+
return nil, xerrors.Errorf("unexpected type %T", data)
69+
}
70+
return fmt.Sprintf("Workspace Proxy %q registered successfully\nToken: %s", response.Proxy.Name, response.ProxyToken), nil
71+
}),
72+
cliui.JSONFormat(),
73+
cliui.ChangeFormatterData(cliui.TableFormat([]codersdk.CreateWorkspaceProxyResponse{}, []string{"proxy name", "proxy url", "proxy token"}),
74+
func(data any) (any, error) {
75+
response, ok := data.(codersdk.CreateWorkspaceProxyResponse)
76+
if !ok {
77+
return nil, xerrors.Errorf("unexpected type %T", data)
78+
}
79+
return []codersdk.CreateWorkspaceProxyResponse{response}, nil
80+
}),
81+
)
82+
)
83+
5684
client := new(codersdk.Client)
5785
cmd := &clibase.Cmd{
5886
Use: "register",
5987
Short: "Register a workspace proxy",
6088
Middleware: clibase.Chain(
61-
clibase.RequireNArgs(1),
89+
clibase.RequireNArgs(0),
6290
r.InitClient(client),
6391
),
64-
Handler: func(i *clibase.Invocation) error {
65-
ctx := i.Context()
66-
name := i.Args[0]
67-
// TODO: Fix all this
92+
Handler: func(inv *clibase.Invocation) error {
93+
ctx := inv.Context()
6894
resp, err := client.CreateWorkspaceProxy(ctx, codersdk.CreateWorkspaceProxyRequest{
69-
Name: name,
70-
DisplayName: name,
71-
Icon: "whocares.png",
72-
URL: "http://localhost:6005",
73-
WildcardHostname: "",
95+
Name: proxyName,
96+
DisplayName: displayName,
97+
Icon: proxyIcon,
98+
URL: proxyURL,
99+
WildcardHostname: proxyWildcardHostname,
74100
})
75101
if err != nil {
76102
return xerrors.Errorf("create workspace proxy: %w", err)
77103
}
78104

79-
fmt.Println(resp.ProxyToken)
80-
return nil
105+
var output string
106+
if onlyToken {
107+
output = resp.ProxyToken
108+
} else {
109+
output, err = formatter.Format(ctx, resp)
110+
if err != nil {
111+
return err
112+
}
113+
}
114+
115+
_, err = fmt.Fprintln(inv.Stdout, output)
116+
return err
81117
},
82118
}
119+
120+
formatter.AttachOptions(&cmd.Options)
121+
cmd.Options.Add(
122+
clibase.Option{
123+
Flag: "name",
124+
Description: "Name of the proxy. This is used to identify the proxy.",
125+
Value: clibase.StringOf(&proxyName),
126+
},
127+
clibase.Option{
128+
Flag: "display-name",
129+
Description: "Display of the proxy. If omitted, the name is reused as the display name.",
130+
Value: clibase.StringOf(&displayName),
131+
},
132+
clibase.Option{
133+
Flag: "icon",
134+
Description: "Display icon of the proxy.",
135+
Value: clibase.StringOf(&proxyIcon),
136+
},
137+
clibase.Option{
138+
Flag: "access-url",
139+
Description: "Access URL of the proxy.",
140+
Value: clibase.StringOf(&proxyURL),
141+
},
142+
clibase.Option{
143+
Flag: "wildcard-access-url",
144+
Description: "(Optional) Access url of the proxy for subdomain based apps.",
145+
Value: clibase.StringOf(&proxyWildcardHostname),
146+
},
147+
clibase.Option{
148+
Flag: "only-token",
149+
Description: "Only print the token. This is useful for scripting.",
150+
Value: clibase.BoolOf(&onlyToken),
151+
},
152+
)
83153
return cmd
84154
}
85155

0 commit comments

Comments
 (0)