Skip to content

Commit 1c08580

Browse files
chore(cli): use option source name for deprecation warnings (coder#15581)
Closes coder#15568.
1 parent e72d58b commit 1c08580

File tree

3 files changed

+71
-37
lines changed

3 files changed

+71
-37
lines changed

cli/root.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,15 @@ func (r *RootCmd) Command(subcommands []*serpent.Command) (*serpent.Command, err
325325
}
326326
})
327327

328+
// Add the PrintDeprecatedOptions middleware to all commands.
329+
cmd.Walk(func(cmd *serpent.Command) {
330+
if cmd.Middleware == nil {
331+
cmd.Middleware = PrintDeprecatedOptions()
332+
} else {
333+
cmd.Middleware = serpent.Chain(cmd.Middleware, PrintDeprecatedOptions())
334+
}
335+
})
336+
328337
if r.agentURL == nil {
329338
r.agentURL = new(url.URL)
330339
}
@@ -1307,3 +1316,65 @@ func headerTransport(ctx context.Context, serverURL *url.URL, header []string, h
13071316
}
13081317
return transport, nil
13091318
}
1319+
1320+
// printDeprecatedOptions loops through all command options, and prints
1321+
// a warning for usage of deprecated options.
1322+
func PrintDeprecatedOptions() serpent.MiddlewareFunc {
1323+
return func(next serpent.HandlerFunc) serpent.HandlerFunc {
1324+
return func(inv *serpent.Invocation) error {
1325+
opts := inv.Command.Options
1326+
// Print deprecation warnings.
1327+
for _, opt := range opts {
1328+
if opt.UseInstead == nil {
1329+
continue
1330+
}
1331+
1332+
if opt.ValueSource == serpent.ValueSourceNone || opt.ValueSource == serpent.ValueSourceDefault {
1333+
continue
1334+
}
1335+
1336+
var warnStr strings.Builder
1337+
_, _ = warnStr.WriteString(translateSource(opt.ValueSource, opt))
1338+
_, _ = warnStr.WriteString(" is deprecated, please use ")
1339+
for i, use := range opt.UseInstead {
1340+
_, _ = warnStr.WriteString(translateSource(opt.ValueSource, use))
1341+
if i != len(opt.UseInstead)-1 {
1342+
_, _ = warnStr.WriteString(" and ")
1343+
}
1344+
}
1345+
_, _ = warnStr.WriteString(" instead.\n")
1346+
1347+
cliui.Warn(inv.Stderr,
1348+
warnStr.String(),
1349+
)
1350+
}
1351+
1352+
return next(inv)
1353+
}
1354+
}
1355+
}
1356+
1357+
// translateSource provides the name of the source of the option, depending on the
1358+
// supplied target ValueSource.
1359+
func translateSource(target serpent.ValueSource, opt serpent.Option) string {
1360+
switch target {
1361+
case serpent.ValueSourceFlag:
1362+
return fmt.Sprintf("`--%s`", opt.Flag)
1363+
case serpent.ValueSourceEnv:
1364+
return fmt.Sprintf("`%s`", opt.Env)
1365+
case serpent.ValueSourceYAML:
1366+
return fmt.Sprintf("`%s`", fullYamlName(opt))
1367+
default:
1368+
return opt.Name
1369+
}
1370+
}
1371+
1372+
func fullYamlName(opt serpent.Option) string {
1373+
var full strings.Builder
1374+
for _, name := range opt.Group.Ancestry() {
1375+
_, _ = full.WriteString(name.YAML)
1376+
_, _ = full.WriteString(".")
1377+
}
1378+
_, _ = full.WriteString(opt.YAML)
1379+
return full.String()
1380+
}

cli/server.go

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,6 @@ func (r *RootCmd) Server(newAPI func(context.Context, *coderd.Options) (*coderd.
294294
Options: opts,
295295
Middleware: serpent.Chain(
296296
WriteConfigMW(vals),
297-
PrintDeprecatedOptions(),
298297
serpent.RequireNArgs(0),
299298
),
300299
Handler: func(inv *serpent.Invocation) error {
@@ -1240,41 +1239,6 @@ func templateHelpers(options *coderd.Options) map[string]any {
12401239
}
12411240
}
12421241

1243-
// printDeprecatedOptions loops through all command options, and prints
1244-
// a warning for usage of deprecated options.
1245-
func PrintDeprecatedOptions() serpent.MiddlewareFunc {
1246-
return func(next serpent.HandlerFunc) serpent.HandlerFunc {
1247-
return func(inv *serpent.Invocation) error {
1248-
opts := inv.Command.Options
1249-
// Print deprecation warnings.
1250-
for _, opt := range opts {
1251-
if opt.UseInstead == nil {
1252-
continue
1253-
}
1254-
1255-
if opt.ValueSource == serpent.ValueSourceNone || opt.ValueSource == serpent.ValueSourceDefault {
1256-
continue
1257-
}
1258-
1259-
warnStr := opt.Name + " is deprecated, please use "
1260-
for i, use := range opt.UseInstead {
1261-
warnStr += use.Name + " "
1262-
if i != len(opt.UseInstead)-1 {
1263-
warnStr += "and "
1264-
}
1265-
}
1266-
warnStr += "instead.\n"
1267-
1268-
cliui.Warn(inv.Stderr,
1269-
warnStr,
1270-
)
1271-
}
1272-
1273-
return next(inv)
1274-
}
1275-
}
1276-
}
1277-
12781242
// writeConfigMW will prevent the main command from running if the write-config
12791243
// flag is set. Instead, it will marshal the command options to YAML and write
12801244
// them to stdout.

enterprise/cli/proxyserver.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,6 @@ func (r *RootCmd) proxyServer() *serpent.Command {
110110
Options: opts,
111111
Middleware: serpent.Chain(
112112
cli.WriteConfigMW(cfg),
113-
cli.PrintDeprecatedOptions(),
114113
serpent.RequireNArgs(0),
115114
),
116115
Handler: func(inv *serpent.Invocation) error {

0 commit comments

Comments
 (0)