Skip to content

Commit

Permalink
Merge pull request #200 from aabouzaid/add-exclude-to-profile
Browse files Browse the repository at this point in the history
Add cli options -exclude and -exclude-from to profile options
  • Loading branch information
Keith Monihen committed Apr 17, 2020
2 parents 04c18ca + 45b5a88 commit d9cbfc0
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 10 deletions.
20 changes: 11 additions & 9 deletions cli/app.go
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion cli/options.go
Expand Up @@ -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
}
Expand Down
24 changes: 24 additions & 0 deletions cli/options_test.go
Expand Up @@ -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 {
Expand Down
6 changes: 6 additions & 0 deletions cli/testdata/profile.yml
Expand Up @@ -14,3 +14,9 @@ exceptions:

tags:
- iam

exclude:
- this_file_will_be_excluded.tf

exclude_from:
- ./testdata/exclude-list

0 comments on commit d9cbfc0

Please sign in to comment.