internal/flags: fix issue with stringslice migration (#25830)
This fixes a cornercase bug where the flag migration would mess up the value of StringSlice flags.
This commit is contained in:
parent
f651d6d56b
commit
07e0704ca9
|
@ -94,10 +94,34 @@ func MigrateGlobalFlags(ctx *cli.Context) {
|
|||
}
|
||||
|
||||
func doMigrateFlags(ctx *cli.Context) {
|
||||
// Figure out if there are any aliases of commands. If there are, we want
|
||||
// to ignore them when iterating over the flags.
|
||||
var aliases = make(map[string]bool)
|
||||
for _, fl := range ctx.Command.Flags {
|
||||
for _, alias := range fl.Names()[1:] {
|
||||
aliases[alias] = true
|
||||
}
|
||||
}
|
||||
for _, name := range ctx.FlagNames() {
|
||||
for _, parent := range ctx.Lineage()[1:] {
|
||||
if parent.IsSet(name) {
|
||||
ctx.Set(name, parent.String(name))
|
||||
// When iterating across the lineage, we will be served both
|
||||
// the 'canon' and alias formats of all commmands. In most cases,
|
||||
// it's fine to set it in the ctx multiple times (one for each
|
||||
// name), however, the Slice-flags are not fine.
|
||||
// The slice-flags accumulate, so if we set it once as
|
||||
// "foo" and once as alias "F", then both will be present in the slice.
|
||||
if _, isAlias := aliases[name]; isAlias {
|
||||
continue
|
||||
}
|
||||
// If it is a string-slice, we need to set it as
|
||||
// "alfa, beta, gamma" instead of "[alfa beta gamma]", in order
|
||||
// for the backing StringSlice to parse it properly.
|
||||
if result := parent.StringSlice(name); len(result) > 0 {
|
||||
ctx.Set(name, strings.Join(result, ","))
|
||||
} else {
|
||||
ctx.Set(name, parent.String(name))
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue