Skip to content

Commit

Permalink
Use regular expressions in the blacklist for more convenient matching.
Browse files Browse the repository at this point in the history
  • Loading branch information
pieterlange committed May 11, 2017
1 parent c67131b commit e3d0cca
Showing 1 changed file with 26 additions and 6 deletions.
32 changes: 26 additions & 6 deletions applylist/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package applylist

import (
"github.com/box/kube-applier/sysutil"
"log"
"path/filepath"
"regexp"
"sort"
)

Expand Down Expand Up @@ -74,7 +76,15 @@ func (f *Factory) createFileList(listFilePath string) ([]string, error) {
// createBlacklist reads lines from the blacklist file, converts the relative
// paths to full paths, and returns a sorted list of full paths.
func (f *Factory) createBlacklist() ([]string, error) {
return f.createFileList(f.BlacklistPath)
if f.BlacklistPath == "" {
return []string{}, nil
}
rawList, err := f.FileSystem.ReadLines(f.BlacklistPath)
if err != nil {
return nil, err
}
sort.Strings(rawList)
return rawList, nil
}

// createWhitelist reads lines from the whitelist file, converts the relative
Expand All @@ -98,9 +108,20 @@ func (f *Factory) createApplyList(blacklist, whitelist []string) ([]string, erro
// shouldApplyPath returns true if file path should be applied, false otherwise.
// Conditions for skipping the file path are:
// 1. File path is not a .json or .yaml file
// 2. File path is listed in the blacklist
func shouldApplyPath(path string, blacklistMap, whitelistMap map[string]struct{}) bool {
_, inBlacklist := blacklistMap[path]
// 2. File path is matched against an entry in the blacklist
// 3. File path is not explicitly listed in the whitelist
func shouldApplyPath(path string, blacklist []string, whitelistMap map[string]struct{}) bool {
inBlacklist := false
for _, regexPath := range blacklist {
if matched, err := regexp.MatchString(regexPath, path); err == nil {
if matched {
inBlacklist = true
break
}
} else {
log.Printf("Error in regular expression: %v", err)
}
}

// If whitelist is empty, essentially there is no whitelist.
inWhiteList := len(whitelistMap) == 0
Expand All @@ -114,12 +135,11 @@ func shouldApplyPath(path string, blacklistMap, whitelistMap map[string]struct{}
// filter iterates through the list of all files in the repo and filters it
// down to a list of those that should be applied.
func filter(rawApplyList, blacklist, whitelist []string) []string {
blacklistMap := stringSliceToMap(blacklist)
whitelistMap := stringSliceToMap(whitelist)

applyList := []string{}
for _, filePath := range rawApplyList {
if shouldApplyPath(filePath, blacklistMap, whitelistMap) {
if shouldApplyPath(filePath, blacklist, whitelistMap) {
applyList = append(applyList, filePath)
}
}
Expand Down

0 comments on commit e3d0cca

Please sign in to comment.