Merge pull request #449 from cloudskiff/fix/filterFlagRepeat

Enforce one occurrence of filter flag
main
Raphaël 2021-04-26 18:02:24 +02:00 committed by GitHub
commit 7a52ef3e90
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 8 deletions

View File

@ -60,9 +60,14 @@ func NewScanCmd() *cobra.Command {
}
opts.Output = *out
filterFlag, _ := cmd.Flags().GetString("filter")
if filterFlag != "" {
expr, err := filter.BuildExpression(filterFlag)
filterFlag, _ := cmd.Flags().GetStringArray("filter")
if len(filterFlag) > 1 {
return errors.New("Filter flag should be specified only once")
}
if len(filterFlag) == 1 && filterFlag[0] != "" {
expr, err := filter.BuildExpression(filterFlag[0])
if err != nil {
return errors.Wrap(err, "unable to parse filter expression")
}
@ -79,16 +84,14 @@ func NewScanCmd() *cobra.Command {
}
fl := cmd.Flags()
fl.BoolP(
fl.Bool(
"quiet",
"",
false,
"Do not display anything but scan results",
)
fl.StringP(
fl.StringArray(
"filter",
"",
"",
[]string{},
"JMESPath expression to filter on\n"+
"Examples : \n"+
" - Type == 'aws_s3_bucket' (will filter only s3 buckets)\n"+

View File

@ -78,6 +78,7 @@ func TestScanCmd_Invalid(t *testing.T) {
{args: []string{"scan", "--from", "tfstate+foobar://test"}, expected: "Unsupported IaC backend 'foobar': \nAccepted values are: s3,http,https"},
{args: []string{"scan", "--from", "tfstate:///tmp/test", "--from", "tfstate+toto://test"}, expected: "Unsupported IaC backend 'toto': \nAccepted values are: s3,http,https"},
{args: []string{"scan", "--filter", "Type='test'"}, expected: "unable to parse filter expression: SyntaxError: Expected tRbracket, received: tUnknown"},
{args: []string{"scan", "--filter", "Type='test'", "--filter", "Type='test2'"}, expected: "Filter flag should be specified only once"},
}
for _, tt := range cases {