diff --git a/cmd/healthcheck/main.go b/cmd/healthcheck/main.go index 03497dd..3557115 100644 --- a/cmd/healthcheck/main.go +++ b/cmd/healthcheck/main.go @@ -40,6 +40,7 @@ func main() { var gitWhitelist, metaFile, releaseTags []string rootDir := flag.String("root", "", "") repo := flag.String("repo", "", "") + localList := flag.String("localList", "", "") droneBranch := flag.String("droneBranch", "", "") releaseCategories := flag.String("releaseCategories", "", "") releaseNumber := flag.Int("releaseNumber", 0, "") @@ -55,7 +56,7 @@ func main() { if err != nil { fmt.Printf("## Repo Size Check Failed:\n%s\n", err.Error()) } - err = healthcheck.ForbiddenCheck(*rootDir, gitWhitelist, *repo, *droneBranch) + err = healthcheck.ForbiddenCheck(*rootDir, gitWhitelist, localList, *repo, *droneBranch) if err != nil { fmt.Printf("## Forbidden File Check Failed:\n%s\n", err.Error()) } diff --git a/pkg/healthcheck/forbidden.go b/pkg/healthcheck/forbidden.go index 7cd6d71..0cbb21b 100644 --- a/pkg/healthcheck/forbidden.go +++ b/pkg/healthcheck/forbidden.go @@ -1,6 +1,7 @@ package healthcheck import ( + "bufio" "fmt" "log/slog" "os" @@ -9,9 +10,18 @@ import ( "strings" ) +func inString(str1 string, strList []string) bool { + for _, str := range strList { + if str1 == str { + return true + } + } + return false +} + // getForbiddens retrieves a list of forbidden files in the specified root directory. // It searches for files that do not match the specified regex patterns in the given file list. -func getForbiddens(root string, fileList []string) ([]string, error) { +func getForbiddens(root string, fileList []string, localList string) ([]string, error) { var matches []string var regexList []*regexp.Regexp @@ -20,6 +30,24 @@ func getForbiddens(root string, fileList []string) ([]string, error) { return nil, err } + var dirs []string + + if localList != "" { + file, err := os.Open(localList) + if err != nil { + return nil, fmt.Errorf("Failed to open file %s: %v\n", localList, err) + } + defer file.Close() + + scanner := bufio.NewScanner(file) + for scanner.Scan() { + dirs = append(dirs, scanner.Text()) + } + if err := scanner.Err(); err != nil { + return nil, fmt.Errorf("Error reading file %s: %v\n", localList, err) + } + } + err = filepath.Walk(root, func(path string, info os.FileInfo, err error) error { if err != nil { return err @@ -28,6 +56,10 @@ func getForbiddens(root string, fileList []string) ([]string, error) { if info.IsDir() { if info.Name() == ".git" || info.Name() == ".gitea" || info.Name() == "ci" { return filepath.SkipDir + } else if localList != "" { + if inString(info.Name(), dirs) { + return filepath.SkipDir + } } else { return nil } @@ -52,8 +84,8 @@ func getForbiddens(root string, fileList []string) ([]string, error) { // forbiddenCheck checks for forbidden files in the specified root directory. // It prints the list of forbidden files found, along with instructions on how to fix them. -func ForbiddenCheck(rootDir string, regexList []string, repo string, droneBranch string) error { - forbids, err := getForbiddens(rootDir, regexList) +func ForbiddenCheck(rootDir string, regexList []string, localList string, repo string, droneBranch string) error { + forbids, err := getForbiddens(rootDir, regexList, localList) if err != nil { slog.Error("getting forbiddens", "error", err) return fmt.Errorf("error getting forbiddens: %w", err)