From bc1752d3cbecc643562eb24f403f7635b7423bf3 Mon Sep 17 00:00:00 2001 From: Ahmed AbouZaid Date: Fri, 17 Apr 2020 21:11:24 +0200 Subject: [PATCH 1/2] fix: add cli options -exclude and -exclude-from to profile options --- cli/app.go | 20 +++++++++++--------- cli/options.go | 4 +++- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/cli/app.go b/cli/app.go index 146e91b..7d8eaf3 100644 --- a/cli/app.go +++ b/cli/app.go @@ -35,15 +35,17 @@ type ( // ProfileOptions for default options from a project file ProfileOptions struct { - Rules []string - IDs []string - IgnoreIDs []string `json:"ignore_ids"` - Tags []string - Query string - Files []string - Terraform bool - Exceptions []RuleException - Variables map[string]string + Rules []string + IDs []string + IgnoreIDs []string `json:"ignore_ids"` + Tags []string + Query string + Files []string + Terraform bool + Exceptions []RuleException + Variables map[string]string + ExcludePatterns []string `json:"exclude"` + ExcludeFromFilenames []string `json:"exclude_from"` } // RuleException optional list allowing a project to ignore specific rules for specific resources diff --git a/cli/options.go b/cli/options.go index b1b93cb..f295ea3 100644 --- a/cli/options.go +++ b/cli/options.go @@ -39,7 +39,9 @@ func getCommandLineOptions() CommandLineOptions { } func getLinterOptions(o CommandLineOptions, p ProfileOptions) (LinterOptions, error) { - allExcludePatterns, err := loadExcludePatterns(o.ExcludePatterns, o.ExcludeFromFilenames) + excludePatterns := append(o.ExcludePatterns, p.ExcludePatterns...) + excludeFromFilenames := append(o.ExcludeFromFilenames, p.ExcludeFromFilenames...) + allExcludePatterns, err := loadExcludePatterns(excludePatterns, excludeFromFilenames) if err != nil { return LinterOptions{}, err } From 45b5a883d3d2df830a577ee5e83abf10c030fcbe Mon Sep 17 00:00:00 2001 From: Ahmed AbouZaid Date: Fri, 17 Apr 2020 22:33:53 +0200 Subject: [PATCH 2/2] fix: add tests for cli options -exclude and -exclude-from to profile options --- cli/options_test.go | 24 ++++++++++++++++++++++++ cli/testdata/profile.yml | 6 ++++++ 2 files changed, 30 insertions(+) diff --git a/cli/options_test.go b/cli/options_test.go index e9ae6db..2df25d7 100644 --- a/cli/options_test.go +++ b/cli/options_test.go @@ -123,6 +123,30 @@ func TestLoadProfile(t *testing.T) { } } +func TestProfileExclude(t *testing.T) { + p, err := loadProfile("./testdata/profile.yml") + if err != nil { + t.Errorf("Expecting loadProfile to run without error: %v\n", err.Error()) + } + o := emptyCommandLineOptions() + l, err := getLinterOptions(o, p) + if err != nil { + t.Errorf("Expecting getLinterOptions to run without error: %v\n", err.Error()) + } + if len(l.ExcludePatterns) != 3 { + t.Errorf("Expecting 3 excludes in total using 'exclude' and 'exclude_from' in profile: %v\n", l.ExcludePatterns) + } + if l.ExcludePatterns[0] != "this_file_will_be_excluded.tf" { + t.Errorf("Expecting 1st pattern using 'exclude' in profile to be 'this_file_will_be_excluded.tf', not '%s'", l.ExcludePatterns[0]) + } + if l.ExcludePatterns[1] != "*1.tf" { + t.Errorf("Expecting 2nd pattern using 'exclude_from' in profile to be '*1.tf', not '%s'", l.ExcludePatterns[1]) + } + if l.ExcludePatterns[2] != "*2.tf" { + t.Errorf("Expecting 3rd pattern using 'exclude_from' in profile to be '*2.tf', not '%s'", l.ExcludePatterns[2]) + } +} + func TestValidateParser(t *testing.T) { parser, err := validateParser("") if err != nil { diff --git a/cli/testdata/profile.yml b/cli/testdata/profile.yml index 6fbc86b..d78e4bc 100644 --- a/cli/testdata/profile.yml +++ b/cli/testdata/profile.yml @@ -14,3 +14,9 @@ exceptions: tags: - iam + +exclude: + - this_file_will_be_excluded.tf + +exclude_from: + - ./testdata/exclude-list