-
Notifications
You must be signed in to change notification settings - Fork 107
fix(cli): fail fast on unknown config flags #690
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
sanjimoh
merged 5 commits into
pivotal-cf:main
from
sanjimoh:fix/tnz-23074/fail_fast_for_unknown_flags
Jun 13, 2025
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
432bbd4
fix(cli): fail fast on unknown config flags
sanjimoh 0879e8d
address review comments
sanjimoh b91bc88
address test case failures
sanjimoh 7e0813c
address review comments
sanjimoh 89c53bd
return error for invalid tags instead of exiting out with fmt messge
sanjimoh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| package cmd | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| . "github.com/onsi/ginkgo/v2" | ||
| . "github.com/onsi/gomega" | ||
| ) | ||
|
|
||
| func TestCmd(t *testing.T) { | ||
| RegisterFailHandler(Fail) | ||
| RunSpecs(t, "Cmd Suite") | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| package cmd | ||
sanjimoh marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| import ( | ||
| . "github.com/onsi/ginkgo/v2" | ||
| . "github.com/onsi/gomega" | ||
|
|
||
| "github.com/jessevdk/go-flags" | ||
| ) | ||
|
|
||
| var _ = Describe("validateCommandFlags", func() { | ||
| type uploadProductOptions struct { | ||
| Product string `long:"product" short:"p" description:"path to product" required:"true"` | ||
| PollingInterval int `long:"polling-interval" short:"i" description:"interval (in seconds) at which to print status" default:"1"` | ||
| Shasum string `long:"shasum" description:"shasum of the provided product file to be used for validation"` | ||
| Version string `long:"product-version" description:"version of the provided product file to be used for validation"` | ||
| Config string `long:"config" short:"c" description:"path to yml file for configuration"` | ||
| VarsEnv string `long:"vars-env" description:"load variables from environment variables matching the provided prefix"` | ||
| VarsFile []string `long:"vars-file" short:"l" description:"load variables from a YAML file"` | ||
| Var []string `long:"var" short:"v" description:"load variable from the command line. Format: VAR=VAL"` | ||
| } | ||
|
|
||
| var parser *flags.Parser | ||
|
|
||
| BeforeEach(func() { | ||
| parser = flags.NewParser(nil, flags.Default) | ||
| parser.AddCommand("upload-product", "desc", "long desc", &uploadProductOptions{}) | ||
| }) | ||
|
|
||
| DescribeTable("flag validation", | ||
| func(args []string, wantErr bool, errMsg string) { | ||
| err := validateCommandFlags(parser, args) | ||
| if wantErr { | ||
| Expect(err).To(HaveOccurred()) | ||
| Expect(err.Error()).To(ContainSubstring(errMsg)) | ||
| } else { | ||
| Expect(err).ToNot(HaveOccurred()) | ||
| } | ||
| }, | ||
| Entry("no args", []string{}, false, ""), | ||
| Entry("valid flags", []string{"upload-product", "--product", "file.pivotal", "--polling-interval", "5", "--shasum", "abc123", "--product-version", "2.3.4"}, false, ""), | ||
| Entry("valid short flags", []string{"upload-product", "-p", "file.pivotal", "-i", "5"}, false, ""), | ||
| Entry("all config interpolation flags", []string{"upload-product", "-c", "config.yml", "--vars-env", "MY", "-l", "vars1.yml", "-l", "vars2.yml", "-v", "FOO=bar", "-v", "BAZ=qux", "-p", "file.pivotal"}, false, ""), | ||
| Entry("mix config and main flags", []string{"upload-product", "-p", "file.pivotal", "-c", "config.yml", "--vars-env", "MY", "--shasum", "abc123", "-l", "vars.yml", "-v", "FOO=bar"}, false, ""), | ||
| Entry("unknown flag with config flags", []string{"upload-product", "-p", "file.pivotal", "-c", "config.yml", "--notaflag"}, true, "unknown flag(s)"), | ||
| Entry("unknown flag", []string{"upload-product", "--notaflag"}, true, "unknown flag(s)"), | ||
| Entry("multiple unknown flags", []string{"upload-product", "--foo", "--bar"}, true, "unknown flag(s)"), | ||
| Entry("flag value looks like flag", []string{"upload-product", "--product", "--notaflag"}, false, ""), | ||
| Entry("unknown short flags", []string{"upload-product", "-p", "file.pivotal", "-x", "18000", "-z", "18000"}, true, "unknown flag(s)"), | ||
| ) | ||
| }) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.