From 5743d5cfe625481b997098b1c27ad01ba206be49 Mon Sep 17 00:00:00 2001 From: i4k Date: Fri, 29 Nov 2024 00:44:54 +0000 Subject: [PATCH] fix: revert #1965 due to Kong bug. The `arg:"" passthrough:""` option leads to malparsing of flags. Signed-off-by: i4k --- CHANGELOG.md | 4 +++ cmd/terramate/cli/cli_flags_helper.go | 41 +++++++++++++-------------- cmd/terramate/cli/run.go | 6 ---- cmd/terramate/cli/script_info.go | 10 ------- cmd/terramate/cli/script_run.go | 10 ------- go.mod | 2 +- go.sum | 12 ++++---- 7 files changed, 31 insertions(+), 54 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d6c1a24b1..35c08ccaa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,10 @@ Given a version number `MAJOR.MINOR.PATCH`, we increment the: ## Unreleased +### Fixed + +- Fix the command-line parsing of `run` and `script run` which were not failing from unknown flags. + ## v0.11.3 ### Fixed diff --git a/cmd/terramate/cli/cli_flags_helper.go b/cmd/terramate/cli/cli_flags_helper.go index cf1f39413..f65ead404 100644 --- a/cmd/terramate/cli/cli_flags_helper.go +++ b/cmd/terramate/cli/cli_flags_helper.go @@ -19,11 +19,6 @@ import ( const ( defaultIndent = 2 defaultColumnPadding = 4 - - // negatableDefault is a placeholder value for the Negatable tag to indicate - // the negated flag is --no-. This is needed as at the time of - // parsing a tag, the field's flag name is not yet known. - negatableDefault = "_" ) // terramateHelpPrinter is the default HelpPrinter. @@ -310,24 +305,28 @@ func formatFlag(haveShort bool, flag *kong.Flag) string { flagString := "" name := flag.Name isBool := flag.IsBool() - isCounter := flag.IsCounter() - - short := "" if flag.Short != 0 { - short = "-" + string(flag.Short) + ", " - } else if haveShort { - short = " " - } - - if isBool && flag.Tag.Negatable == negatableDefault { - name = "[no-]" + name - } else if isBool && flag.Tag.Negatable != "" { - name += "/" + flag.Tag.Negatable + if isBool && flag.Tag.Negatable { + flagString += fmt.Sprintf("-%c, --[no-]%s", flag.Short, name) + } else { + flagString += fmt.Sprintf("-%c, --%s", flag.Short, name) + } + } else { + if isBool && flag.Tag.Negatable { + if haveShort { + flagString = fmt.Sprintf(" --[no-]%s", name) + } else { + flagString = fmt.Sprintf("--[no-]%s", name) + } + } else { + if haveShort { + flagString += fmt.Sprintf(" --%s", name) + } else { + flagString += fmt.Sprintf("--%s", name) + } + } } - - flagString += fmt.Sprintf("%s--%s", short, name) - - if !isBool && !isCounter { + if !isBool { flagString += fmt.Sprintf("=%s", flag.FormatPlaceHolder()) } return flagString diff --git a/cmd/terramate/cli/run.go b/cmd/terramate/cli/run.go index 5154d7f80..2d247f348 100644 --- a/cmd/terramate/cli/run.go +++ b/cmd/terramate/cli/run.go @@ -119,12 +119,6 @@ func selectPlanFile(terraformPlan, tofuPlan string) (planfile, provisioner strin func (c *cli) runOnStacks() { c.gitSafeguardDefaultBranchIsReachable() - if len(c.parsedArgs.Run.Command) > 0 { - if c.parsedArgs.Run.Command[0] == "--" { - c.parsedArgs.Run.Command = c.parsedArgs.Run.Command[1:] - } - } - if len(c.parsedArgs.Run.Command) == 0 { fatal("run expects a cmd") } diff --git a/cmd/terramate/cli/script_info.go b/cmd/terramate/cli/script_info.go index 0be54c8de..12309ffe2 100644 --- a/cmd/terramate/cli/script_info.go +++ b/cmd/terramate/cli/script_info.go @@ -23,16 +23,6 @@ import ( ) func (c *cli) printScriptInfo() { - if len(c.parsedArgs.Script.Info.Cmds) > 0 { - if c.parsedArgs.Script.Info.Cmds[0] == "--" { - c.parsedArgs.Script.Info.Cmds = c.parsedArgs.Script.Info.Cmds[1:] - } - } - - if len(c.parsedArgs.Script.Info.Cmds) == 0 { - fatal("script info expects cmds") - } - labels := c.parsedArgs.Script.Info.Cmds stacks, err := c.computeSelectedStacks(false, cloudstack.AnyTarget, cloud.NoStatusFilters()) diff --git a/cmd/terramate/cli/script_run.go b/cmd/terramate/cli/script_run.go index 1a46db018..9e65473fc 100644 --- a/cmd/terramate/cli/script_run.go +++ b/cmd/terramate/cli/script_run.go @@ -32,16 +32,6 @@ func (c *cli) runScript() { c.gitSafeguardDefaultBranchIsReachable() c.checkOutdatedGeneratedCode() - if len(c.parsedArgs.Script.Run.Cmds) > 0 { - if c.parsedArgs.Script.Run.Cmds[0] == "--" { - c.parsedArgs.Script.Run.Cmds = c.parsedArgs.Script.Run.Cmds[1:] - } - } - - if len(c.parsedArgs.Script.Run.Cmds) == 0 { - fatal("script run expects cmds") - } - c.checkTargetsConfiguration(c.parsedArgs.Script.Run.Target, c.parsedArgs.Script.Run.FromTarget, func(isTargetSet bool) { if !isTargetSet { // We don't check here if any script has any sync command options enabled. diff --git a/go.mod b/go.mod index fb6c9721d..3de19b034 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,7 @@ go 1.21.0 toolchain go1.22.3 require ( - github.com/alecthomas/kong v1.4.0 + github.com/alecthomas/kong v0.7.1 github.com/apparentlymart/go-versions v1.0.2 github.com/cli/go-gh/v2 v2.11.1 github.com/cli/safeexec v1.0.0 diff --git a/go.sum b/go.sum index e375d87b4..c686f206e 100644 --- a/go.sum +++ b/go.sum @@ -255,13 +255,13 @@ github.com/agext/levenshtein v1.2.1/go.mod h1:JEDfjyjHDjOF/1e4FlBE/PkbqA9OfWu2ki github.com/agext/levenshtein v1.2.2/go.mod h1:JEDfjyjHDjOF/1e4FlBE/PkbqA9OfWu2ki2W0IB5558= github.com/agext/levenshtein v1.2.3 h1:YB2fHEn0UJagG8T1rrWknE3ZQzWM06O8AMAatNn7lmo= github.com/agext/levenshtein v1.2.3/go.mod h1:JEDfjyjHDjOF/1e4FlBE/PkbqA9OfWu2ki2W0IB5558= -github.com/alecthomas/assert/v2 v2.11.0 h1:2Q9r3ki8+JYXvGsDyBXwH3LcJ+WK5D0gc5E8vS6K3D0= -github.com/alecthomas/assert/v2 v2.11.0/go.mod h1:Bze95FyfUr7x34QZrjL+XP+0qgp/zg8yS+TtBj1WA3k= +github.com/alecthomas/assert/v2 v2.1.0 h1:tbredtNcQnoSd3QBhQWI7QZ3XHOVkw1Moklp2ojoH/0= +github.com/alecthomas/assert/v2 v2.1.0/go.mod h1:b/+1DI2Q6NckYi+3mXyH3wFb8qG37K/DuK80n7WefXA= github.com/alecthomas/kong v0.2.2/go.mod h1:kQOmtJgV+Lb4aj+I2LEn40cbtawdWJ9Y8QLq+lElKxE= -github.com/alecthomas/kong v1.4.0 h1:UL7tzGMnnY0YRMMvJyITIRX1EpO6RbBRZDNcCevy3HA= -github.com/alecthomas/kong v1.4.0/go.mod h1:p2vqieVMeTAnaC83txKtXe8FLke2X07aruPWXyMPQrU= -github.com/alecthomas/repr v0.4.0 h1:GhI2A8MACjfegCPVq9f1FLvIBS+DrQ2KQBFZP1iFzXc= -github.com/alecthomas/repr v0.4.0/go.mod h1:Fr0507jx4eOXV7AlPV6AVZLYrLIuIeSOWtW57eE/O/4= +github.com/alecthomas/kong v0.7.1 h1:azoTh0IOfwlAX3qN9sHWTxACE2oV8Bg2gAwBsMwDQY4= +github.com/alecthomas/kong v0.7.1/go.mod h1:n1iCIO2xS46oE8ZfYCNDqdR0b0wZNrXAIAqro/2132U= +github.com/alecthomas/repr v0.1.0 h1:ENn2e1+J3k09gyj2shc0dHr/yjaWSHRlrJ4DPMevDqE= +github.com/alecthomas/repr v0.1.0/go.mod h1:2kn6fqh/zIyPLmm3ugklbEi5hg5wS435eygvNfaDQL8= github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0= github.com/aliyun/alibaba-cloud-sdk-go v0.0.0-20190329064014-6e358769c32a/go.mod h1:T9M45xf79ahXVelWoOBmH0y4aC1t5kXO5BxwyakgIGA=