Skip to content

Commit 2585249

Browse files
authored
chore: Move writeConfig to a cli middleware (#7093)
* chore: Move writeConfig to a cli middleware Trying to reduce the server LoC, it's very long and unwieldy. The workspace proxy code needs to replicate, and trying to reuse as much as possible * Move deprecation warnings too
1 parent e1db64a commit 2585249

File tree

1 file changed

+73
-46
lines changed

1 file changed

+73
-46
lines changed

cli/server.go

Lines changed: 73 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -165,62 +165,24 @@ func (r *RootCmd) Server(newAPI func(context.Context, *coderd.Options) (*coderd.
165165
opts = cfg.Options()
166166
)
167167
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+
),
172176
Handler: func(inv *clibase.Invocation) error {
173177
// Main command context for managing cancellation of running
174178
// services.
175179
ctx, cancel := context.WithCancel(inv.Context())
176180
defer cancel()
177181

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-
196182
if cfg.Config != "" {
197183
cliui.Warnf(inv.Stderr, "YAML support is experimental and offers no compatibility guarantees.")
198184
}
199185

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-
224186
go dumpHandler(ctx)
225187

226188
// Validate bind addresses.
@@ -1222,6 +1184,71 @@ func (r *RootCmd) Server(newAPI func(context.Context, *coderd.Options) (*coderd.
12221184
return serverCmd
12231185
}
12241186

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+
12251252
// isLocalURL returns true if the hostname of the provided URL appears to
12261253
// resolve to a loopback address.
12271254
func isLocalURL(ctx context.Context, u *url.URL) (bool, error) {

0 commit comments

Comments
 (0)