@@ -53,33 +53,103 @@ func (r *RootCmd) workspaceProxy() *clibase.Cmd {
53
53
}
54
54
55
55
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\n Token: %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
+
56
84
client := new (codersdk.Client )
57
85
cmd := & clibase.Cmd {
58
86
Use : "register" ,
59
87
Short : "Register a workspace proxy" ,
60
88
Middleware : clibase .Chain (
61
- clibase .RequireNArgs (1 ),
89
+ clibase .RequireNArgs (0 ),
62
90
r .InitClient (client ),
63
91
),
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 ()
68
94
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 ,
74
100
})
75
101
if err != nil {
76
102
return xerrors .Errorf ("create workspace proxy: %w" , err )
77
103
}
78
104
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
81
117
},
82
118
}
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
+ )
83
153
return cmd
84
154
}
85
155
0 commit comments