diff --git a/pkg/cmd/scan.go b/pkg/cmd/scan.go index 1b006992..1ea443f6 100644 --- a/pkg/cmd/scan.go +++ b/pkg/cmd/scan.go @@ -196,6 +196,12 @@ func NewScanCmd(opts *pkg.ScanOptions) *cobra.Command { ".driftignore", "Path to the driftignore file", ) + fl.StringSliceVar(&opts.Driftignores, + "ignores", + []string{}, + fmt.Sprintf("%s Patterns to be used for ignoring resources\n", warn("EXPERIMENTAL:"))+ + "Example: *,!aws_s3* \n"+ + "When using this parameter the driftignore file is not used") fl.String( "tf-lockfile", ".terraform.lock.hcl", @@ -252,7 +258,7 @@ func scanRun(opts *pkg.ScanOptions) error { }() logrus.Debug("Checking for driftignore") - driftIgnore := filter.NewDriftIgnore(opts.DriftignorePath) + driftIgnore := filter.NewDriftIgnore(opts.DriftignorePath, opts.Driftignores...) scanner := remote.NewScanner(remoteLibrary, alerter, remote.ScannerOptions{Deep: opts.Deep}, driftIgnore) diff --git a/pkg/driftctl.go b/pkg/driftctl.go index 8032249f..f64097e6 100644 --- a/pkg/driftctl.go +++ b/pkg/driftctl.go @@ -33,6 +33,7 @@ type ScanOptions struct { ProviderVersion string ConfigDir string DriftignorePath string + Driftignores []string Deep bool } diff --git a/pkg/filter/driftignore.go b/pkg/filter/driftignore.go index a09ed560..d7885f40 100644 --- a/pkg/filter/driftignore.go +++ b/pkg/filter/driftignore.go @@ -15,15 +15,23 @@ const separator = "_-_" type DriftIgnore struct { driftignorePath string + ignorePatterns []string matcher gitignore.Matcher } -func NewDriftIgnore(path string) *DriftIgnore { +func NewDriftIgnore(path string, ignorePatterns ...string) *DriftIgnore { d := DriftIgnore{ driftignorePath: path, + ignorePatterns: ignorePatterns, matcher: gitignore.NewMatcher(nil), } - err := d.readIgnoreFile() + var err error + if len(ignorePatterns) > 0 { + err = d.parseIgnorePatterns() + } else { + err = d.readIgnoreFile() + } + if err != nil { logrus.Debug(err) } @@ -41,21 +49,7 @@ func (r *DriftIgnore) readIgnoreFile() error { scanner := bufio.NewScanner(file) for lineNumber := 1; scanner.Scan(); lineNumber++ { line := scanner.Text() - - if len(strings.ReplaceAll(line, " ", "")) <= 0 { - continue // empty - } - - if strings.HasPrefix(line, "#") { - continue // this is a comment - } - line = strings.ReplaceAll(line, "/", separator) - - lines = append(lines, gitignore.ParsePattern(line, nil)) - if !strings.HasSuffix(line, "*") { - line := fmt.Sprintf("%s.*", line) - lines = append(lines, gitignore.ParsePattern(line, nil)) - } + r.parseIgnorePattern(line, &lines) } if err := scanner.Err(); err != nil { @@ -67,6 +61,32 @@ func (r *DriftIgnore) readIgnoreFile() error { return nil } +func (r *DriftIgnore) parseIgnorePatterns() error { + var lines []gitignore.Pattern + for _, p := range r.ignorePatterns { + r.parseIgnorePattern(p, &lines) + } + r.matcher = gitignore.NewMatcher(lines) + return nil +} + +func (r *DriftIgnore) parseIgnorePattern(line string, patterns *[]gitignore.Pattern) { + if len(strings.ReplaceAll(line, " ", "")) <= 0 { + return // empty + } + + if strings.HasPrefix(line, "#") { + return // this is a comment + } + line = strings.ReplaceAll(line, "/", separator) + + *patterns = append(*patterns, gitignore.ParsePattern(line, nil)) + if !strings.HasSuffix(line, "*") { + line := fmt.Sprintf("%s.*", line) + *patterns = append(*patterns, gitignore.ParsePattern(line, nil)) + } +} + func (r *DriftIgnore) isAnyOfChildrenTypesNotIgnored(ty resource.ResourceType) bool { childrenTypes := resource.GetMeta(ty).GetChildrenTypes() for _, childrenType := range childrenTypes {