Add a new ignores flag to parse ignore patterns as array
parent
d256d98a1c
commit
428ab77d89
|
@ -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)
|
||||
|
||||
|
|
|
@ -33,6 +33,7 @@ type ScanOptions struct {
|
|||
ProviderVersion string
|
||||
ConfigDir string
|
||||
DriftignorePath string
|
||||
Driftignores []string
|
||||
Deep bool
|
||||
}
|
||||
|
||||
|
|
|
@ -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 {
|
||||
|
|
Loading…
Reference in New Issue