Skip to content

Commit 4869325

Browse files
committed
chore(cli): use option source name for deprecation warnings
1 parent c3c23ed commit 4869325

File tree

7 files changed

+55
-35
lines changed

7 files changed

+55
-35
lines changed

cli/restart.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ func (r *RootCmd) restart() *serpent.Command {
2626
Short: "Restart a workspace",
2727
Middleware: serpent.Chain(
2828
serpent.RequireNArgs(1),
29+
PrintDeprecatedOptions(),
2930
r.InitClient(client),
3031
),
3132
Options: serpent.OptionSet{cliui.SkipPromptOption()},

cli/root.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1307,3 +1307,53 @@ func headerTransport(ctx context.Context, serverURL *url.URL, header []string, h
13071307
}
13081308
return transport, nil
13091309
}
1310+
1311+
// printDeprecatedOptions loops through all command options, and prints
1312+
// a warning for usage of deprecated options.
1313+
func PrintDeprecatedOptions() serpent.MiddlewareFunc {
1314+
return func(next serpent.HandlerFunc) serpent.HandlerFunc {
1315+
return func(inv *serpent.Invocation) error {
1316+
opts := inv.Command.Options
1317+
// Print deprecation warnings.
1318+
for _, opt := range opts {
1319+
if opt.UseInstead == nil {
1320+
continue
1321+
}
1322+
1323+
if opt.ValueSource == serpent.ValueSourceNone || opt.ValueSource == serpent.ValueSourceDefault {
1324+
continue
1325+
}
1326+
1327+
warnStr := translateSource(opt.ValueSource, opt) + " is deprecated, please use "
1328+
for i, use := range opt.UseInstead {
1329+
warnStr += translateSource(opt.ValueSource, use) + " "
1330+
if i != len(opt.UseInstead)-1 {
1331+
warnStr += "and "
1332+
}
1333+
}
1334+
warnStr += "instead.\n"
1335+
1336+
cliui.Warn(inv.Stderr,
1337+
warnStr,
1338+
)
1339+
}
1340+
1341+
return next(inv)
1342+
}
1343+
}
1344+
}
1345+
1346+
// translateSource provides the name of the source of the option, depending on the
1347+
// supplied target ValueSource.
1348+
func translateSource(target serpent.ValueSource, opt serpent.Option) string {
1349+
switch target {
1350+
case serpent.ValueSourceFlag:
1351+
return fmt.Sprintf("`--%s`", opt.Flag)
1352+
case serpent.ValueSourceEnv:
1353+
return fmt.Sprintf("`%s`", opt.Env)
1354+
case serpent.ValueSourceYAML:
1355+
return fmt.Sprintf("`%s`", opt.YAML)
1356+
default:
1357+
return opt.Name
1358+
}
1359+
}

cli/server.go

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1240,41 +1240,6 @@ func templateHelpers(options *coderd.Options) map[string]any {
12401240
}
12411241
}
12421242

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-
12781243
// writeConfigMW will prevent the main command from running if the write-config
12791244
// flag is set. Instead, it will marshal the command options to YAML and write
12801245
// them to stdout.

cli/ssh.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ func (r *RootCmd) ssh() *serpent.Command {
7777
Middleware: serpent.Chain(
7878
serpent.RequireNArgs(1),
7979
r.InitClient(client),
80+
PrintDeprecatedOptions(),
8081
initAppearance(client, &appearanceConfig),
8182
),
8283
Handler: func(inv *serpent.Invocation) (retErr error) {

cli/start.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ func (r *RootCmd) start() *serpent.Command {
2525
Short: "Start a workspace",
2626
Middleware: serpent.Chain(
2727
serpent.RequireNArgs(1),
28+
PrintDeprecatedOptions(),
2829
r.InitClient(client),
2930
),
3031
Options: serpent.OptionSet{cliui.SkipPromptOption()},

cli/update.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ func (r *RootCmd) update() *serpent.Command {
2222
Long: "Use --always-prompt to change the parameter values of the workspace.",
2323
Middleware: serpent.Chain(
2424
serpent.RequireNArgs(1),
25+
PrintDeprecatedOptions(),
2526
r.InitClient(client),
2627
),
2728
Handler: func(inv *serpent.Invocation) error {

enterprise/cli/provisionerdaemonstart.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ func (r *RootCmd) provisionerDaemonStart() *serpent.Command {
5858
Use: "start",
5959
Short: "Run a provisioner daemon",
6060
Middleware: serpent.Chain(
61+
agpl.PrintDeprecatedOptions(),
6162
// disable checks and warnings because this command starts a daemon; it is
6263
// not meant for humans typing commands. Furthermore, the checks are
6364
// incompatible with PSK auth that this command uses

0 commit comments

Comments
 (0)