@@ -165,62 +165,24 @@ func (r *RootCmd) Server(newAPI func(context.Context, *coderd.Options) (*coderd.
165
165
opts = cfg .Options ()
166
166
)
167
167
serverCmd := & clibase.Cmd {
168
- Use : "server" ,
169
- Short : "Start a Coder server" ,
170
- Options : opts ,
171
- Middleware : clibase .RequireNArgs (0 ),
168
+ Use : "server" ,
169
+ Short : "Start a Coder server" ,
170
+ Options : opts ,
171
+ Middleware : clibase .Chain (
172
+ writeConfigMW (cfg ),
173
+ printDeprecatedOptions (),
174
+ clibase .RequireNArgs (0 ),
175
+ ),
172
176
Handler : func (inv * clibase.Invocation ) error {
173
177
// Main command context for managing cancellation of running
174
178
// services.
175
179
ctx , cancel := context .WithCancel (inv .Context ())
176
180
defer cancel ()
177
181
178
- if cfg .WriteConfig {
179
- n , err := opts .MarshalYAML ()
180
- if err != nil {
181
- return xerrors .Errorf ("generate yaml: %w" , err )
182
- }
183
- enc := yaml .NewEncoder (inv .Stdout )
184
- enc .SetIndent (2 )
185
- err = enc .Encode (n )
186
- if err != nil {
187
- return xerrors .Errorf ("encode yaml: %w" , err )
188
- }
189
- err = enc .Close ()
190
- if err != nil {
191
- return xerrors .Errorf ("close yaml encoder: %w" , err )
192
- }
193
- return nil
194
- }
195
-
196
182
if cfg .Config != "" {
197
183
cliui .Warnf (inv .Stderr , "YAML support is experimental and offers no compatibility guarantees." )
198
184
}
199
185
200
- // Print deprecation warnings.
201
- for _ , opt := range opts {
202
- if opt .UseInstead == nil {
203
- continue
204
- }
205
-
206
- if opt .Value .String () == opt .Default {
207
- continue
208
- }
209
-
210
- warnStr := opt .Name + " is deprecated, please use "
211
- for i , use := range opt .UseInstead {
212
- warnStr += use .Name + " "
213
- if i != len (opt .UseInstead )- 1 {
214
- warnStr += "and "
215
- }
216
- }
217
- warnStr += "instead.\n "
218
-
219
- cliui .Warn (inv .Stderr ,
220
- warnStr ,
221
- )
222
- }
223
-
224
186
go dumpHandler (ctx )
225
187
226
188
// Validate bind addresses.
@@ -1222,6 +1184,71 @@ func (r *RootCmd) Server(newAPI func(context.Context, *coderd.Options) (*coderd.
1222
1184
return serverCmd
1223
1185
}
1224
1186
1187
+ // printDeprecatedOptions loops through all command options, and prints
1188
+ // a warning for usage of deprecated options.
1189
+ func printDeprecatedOptions () clibase.MiddlewareFunc {
1190
+ return func (next clibase.HandlerFunc ) clibase.HandlerFunc {
1191
+ return func (inv * clibase.Invocation ) error {
1192
+ opts := inv .Command .Options
1193
+ // Print deprecation warnings.
1194
+ for _ , opt := range opts {
1195
+ if opt .UseInstead == nil {
1196
+ continue
1197
+ }
1198
+
1199
+ if opt .Value .String () == opt .Default {
1200
+ continue
1201
+ }
1202
+
1203
+ warnStr := opt .Name + " is deprecated, please use "
1204
+ for i , use := range opt .UseInstead {
1205
+ warnStr += use .Name + " "
1206
+ if i != len (opt .UseInstead )- 1 {
1207
+ warnStr += "and "
1208
+ }
1209
+ }
1210
+ warnStr += "instead.\n "
1211
+
1212
+ cliui .Warn (inv .Stderr ,
1213
+ warnStr ,
1214
+ )
1215
+ }
1216
+
1217
+ return next (inv )
1218
+ }
1219
+ }
1220
+ }
1221
+
1222
+ // writeConfigMW will prevent the main command from running if the write-config
1223
+ // flag is set. Instead, it will marshal the command options to YAML and write
1224
+ // them to stdout.
1225
+ func writeConfigMW (cfg * codersdk.DeploymentValues ) clibase.MiddlewareFunc {
1226
+ return func (next clibase.HandlerFunc ) clibase.HandlerFunc {
1227
+ return func (inv * clibase.Invocation ) error {
1228
+ if ! cfg .WriteConfig {
1229
+ return next (inv )
1230
+ }
1231
+
1232
+ opts := inv .Command .Options
1233
+ n , err := opts .MarshalYAML ()
1234
+ if err != nil {
1235
+ return xerrors .Errorf ("generate yaml: %w" , err )
1236
+ }
1237
+ enc := yaml .NewEncoder (inv .Stdout )
1238
+ enc .SetIndent (2 )
1239
+ err = enc .Encode (n )
1240
+ if err != nil {
1241
+ return xerrors .Errorf ("encode yaml: %w" , err )
1242
+ }
1243
+ err = enc .Close ()
1244
+ if err != nil {
1245
+ return xerrors .Errorf ("close yaml encoder: %w" , err )
1246
+ }
1247
+ return nil
1248
+ }
1249
+ }
1250
+ }
1251
+
1225
1252
// isLocalURL returns true if the hostname of the provided URL appears to
1226
1253
// resolve to a loopback address.
1227
1254
func isLocalURL (ctx context.Context , u * url.URL ) (bool , error ) {
0 commit comments