From fdd031900f478f20984ca4a8c47b94c7377cd486 Mon Sep 17 00:00:00 2001 From: Martin Holst Swende Date: Tue, 20 Sep 2022 12:46:37 +0200 Subject: [PATCH 1/2] internal/flags: fix issue with stringslice migration --- internal/flags/helpers.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/internal/flags/helpers.go b/internal/flags/helpers.go index de1d29ffd4b9..dad2bbc11243 100644 --- a/internal/flags/helpers.go +++ b/internal/flags/helpers.go @@ -95,7 +95,14 @@ func doMigrateFlags(ctx *cli.Context) { for _, name := range ctx.FlagNames() { for _, parent := range ctx.Lineage()[1:] { if parent.IsSet(name) { - ctx.Set(name, parent.String(name)) + // It is a string-slice, not a string. 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 } } From f86fb5ee5c29865637addf87474d553cfed17f3d Mon Sep 17 00:00:00 2001 From: Martin Holst Swende Date: Tue, 20 Sep 2022 13:56:52 +0200 Subject: [PATCH 2/2] internal/flags: handle aliases of stringslice commands --- internal/flags/helpers.go | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/internal/flags/helpers.go b/internal/flags/helpers.go index dad2bbc11243..2fddf147ba40 100644 --- a/internal/flags/helpers.go +++ b/internal/flags/helpers.go @@ -92,11 +92,28 @@ 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) { - // It is a string-slice, not a string. We need to set it - // as "alfa, beta, gamma" instead of "[alfa beta gamma]", in order + // 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, ","))