@@ -2,7 +2,9 @@ package cli
2
2
3
3
import (
4
4
"fmt"
5
+ "strings"
5
6
7
+ "github.com/fatih/color"
6
8
"golang.org/x/xerrors"
7
9
8
10
"github.com/coder/coder/cli/clibase"
@@ -23,6 +25,7 @@ func (r *RootCmd) workspaceProxy() *clibase.Cmd {
23
25
r .proxyServer (),
24
26
r .createProxy (),
25
27
r .deleteProxy (),
28
+ r .listProxies (),
26
29
},
27
30
}
28
31
@@ -66,7 +69,8 @@ func (r *RootCmd) createProxy() *clibase.Cmd {
66
69
if ! ok {
67
70
return nil , xerrors .Errorf ("unexpected type %T" , data )
68
71
}
69
- return fmt .Sprintf ("Workspace Proxy %q registered successfully\n Token: %s" , response .Proxy .Name , response .ProxyToken ), nil
72
+ return fmt .Sprintf ("Workspace Proxy %q created successfully. Save this token, it will not be shown again." +
73
+ "\n Token: %s" , response .Proxy .Name , response .ProxyToken ), nil
70
74
}),
71
75
cliui .JSONFormat (),
72
76
// Table formatter expects a slice, make a slice of one.
@@ -91,6 +95,10 @@ func (r *RootCmd) createProxy() *clibase.Cmd {
91
95
),
92
96
Handler : func (inv * clibase.Invocation ) error {
93
97
ctx := inv .Context ()
98
+ if proxyName == "" {
99
+ return xerrors .Errorf ("proxy name is required" )
100
+ }
101
+
94
102
resp , err := client .CreateWorkspaceProxy (ctx , codersdk.CreateWorkspaceProxyRequest {
95
103
Name : proxyName ,
96
104
DisplayName : displayName ,
@@ -140,3 +148,60 @@ func (r *RootCmd) createProxy() *clibase.Cmd {
140
148
)
141
149
return cmd
142
150
}
151
+
152
+ func (r * RootCmd ) listProxies () * clibase.Cmd {
153
+ formatter := cliui .NewOutputFormatter (
154
+ cliui .TableFormat ([]codersdk.WorkspaceProxy {}, []string {"name" , "url" , "status status" }),
155
+ cliui .JSONFormat (),
156
+ cliui .ChangeFormatterData (cliui .TextFormat (), func (data any ) (any , error ) {
157
+ resp , ok := data .([]codersdk.WorkspaceProxy )
158
+ if ! ok {
159
+ return nil , xerrors .Errorf ("unexpected type %T" , data )
160
+ }
161
+ var str strings.Builder
162
+ _ , _ = str .WriteString ("Workspace Proxies:\n " )
163
+ sep := ""
164
+ for i , proxy := range resp {
165
+ _ , _ = str .WriteString (sep )
166
+ _ , _ = str .WriteString (fmt .Sprintf ("%d: %s %s %s" , i , proxy .Name , proxy .URL , proxy .Status .Status ))
167
+ for _ , errMsg := range proxy .Status .Report .Errors {
168
+ _ , _ = str .WriteString (color .RedString ("\n \t Err: %s" , errMsg ))
169
+ }
170
+ for _ , warnMsg := range proxy .Status .Report .Errors {
171
+ _ , _ = str .WriteString (color .YellowString ("\n \t Warn: %s" , warnMsg ))
172
+ }
173
+ sep = "\n "
174
+ }
175
+ return str .String (), nil
176
+ }),
177
+ )
178
+
179
+ client := new (codersdk.Client )
180
+ cmd := & clibase.Cmd {
181
+ Use : "ls" ,
182
+ Aliases : []string {"list" },
183
+ Short : "List all workspace proxies" ,
184
+ Middleware : clibase .Chain (
185
+ clibase .RequireNArgs (0 ),
186
+ r .InitClient (client ),
187
+ ),
188
+ Handler : func (inv * clibase.Invocation ) error {
189
+ ctx := inv .Context ()
190
+ proxies , err := client .WorkspaceProxies (ctx )
191
+ if err != nil {
192
+ return xerrors .Errorf ("list workspace proxies: %w" , err )
193
+ }
194
+
195
+ output , err := formatter .Format (ctx , proxies )
196
+ if err != nil {
197
+ return err
198
+ }
199
+
200
+ _ , err = fmt .Fprintln (inv .Stdout , output )
201
+ return err
202
+ },
203
+ }
204
+
205
+ formatter .AttachOptions (& cmd .Options )
206
+ return cmd
207
+ }
0 commit comments