Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit1c08580

Browse files
chore(cli): use option source name for deprecation warnings (#15581)
Closes#15568.
1 parente72d58b commit1c08580

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+
ifcmd.Middleware==nil {
331+
cmd.Middleware=PrintDeprecatedOptions()
332+
}else {
333+
cmd.Middleware=serpent.Chain(cmd.Middleware,PrintDeprecatedOptions())
334+
}
335+
})
336+
328337
ifr.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
returntransport,nil
13091318
}
1319+
1320+
// printDeprecatedOptions loops through all command options, and prints
1321+
// a warning for usage of deprecated options.
1322+
funcPrintDeprecatedOptions() serpent.MiddlewareFunc {
1323+
returnfunc(next serpent.HandlerFunc) serpent.HandlerFunc {
1324+
returnfunc(inv*serpent.Invocation)error {
1325+
opts:=inv.Command.Options
1326+
// Print deprecation warnings.
1327+
for_,opt:=rangeopts {
1328+
ifopt.UseInstead==nil {
1329+
continue
1330+
}
1331+
1332+
ifopt.ValueSource==serpent.ValueSourceNone||opt.ValueSource==serpent.ValueSourceDefault {
1333+
continue
1334+
}
1335+
1336+
varwarnStr strings.Builder
1337+
_,_=warnStr.WriteString(translateSource(opt.ValueSource,opt))
1338+
_,_=warnStr.WriteString(" is deprecated, please use ")
1339+
fori,use:=rangeopt.UseInstead {
1340+
_,_=warnStr.WriteString(translateSource(opt.ValueSource,use))
1341+
ifi!=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+
returnnext(inv)
1353+
}
1354+
}
1355+
}
1356+
1357+
// translateSource provides the name of the source of the option, depending on the
1358+
// supplied target ValueSource.
1359+
functranslateSource(target serpent.ValueSource,opt serpent.Option)string {
1360+
switchtarget {
1361+
caseserpent.ValueSourceFlag:
1362+
returnfmt.Sprintf("`--%s`",opt.Flag)
1363+
caseserpent.ValueSourceEnv:
1364+
returnfmt.Sprintf("`%s`",opt.Env)
1365+
caseserpent.ValueSourceYAML:
1366+
returnfmt.Sprintf("`%s`",fullYamlName(opt))
1367+
default:
1368+
returnopt.Name
1369+
}
1370+
}
1371+
1372+
funcfullYamlName(opt serpent.Option)string {
1373+
varfull strings.Builder
1374+
for_,name:=rangeopt.Group.Ancestry() {
1375+
_,_=full.WriteString(name.YAML)
1376+
_,_=full.WriteString(".")
1377+
}
1378+
_,_=full.WriteString(opt.YAML)
1379+
returnfull.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-
funcPrintDeprecatedOptions() serpent.MiddlewareFunc {
1246-
returnfunc(next serpent.HandlerFunc) serpent.HandlerFunc {
1247-
returnfunc(inv*serpent.Invocation)error {
1248-
opts:=inv.Command.Options
1249-
// Print deprecation warnings.
1250-
for_,opt:=rangeopts {
1251-
ifopt.UseInstead==nil {
1252-
continue
1253-
}
1254-
1255-
ifopt.ValueSource==serpent.ValueSourceNone||opt.ValueSource==serpent.ValueSourceDefault {
1256-
continue
1257-
}
1258-
1259-
warnStr:=opt.Name+" is deprecated, please use "
1260-
fori,use:=rangeopt.UseInstead {
1261-
warnStr+=use.Name+" "
1262-
ifi!=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-
returnnext(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)

[8]ページ先頭

©2009-2025 Movatter.jp