fix/forbidden (#58) #60
| 
						 | 
					@ -59,7 +59,7 @@ func main() {
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		fmt.Printf("### Repo Size Check Failed:\n%s\n", err.Error())
 | 
							fmt.Printf("### Repo Size Check Failed:\n%s\n", err.Error())
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	err = healthcheck.ForbiddenCheck(*rootDir, gitWhitelist)
 | 
						err = healthcheck.ForbiddenCheck(*rootDir)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		fmt.Printf("### Forbidden File Check Failed:\n%s\n", err.Error())
 | 
							fmt.Printf("### Forbidden File Check Failed:\n%s\n", err.Error())
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -11,11 +11,12 @@ import (
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// getForbiddens retrieves a list of forbidden files in the specified root directory.
 | 
					// 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.
 | 
					// It searches for files that match the specified ignore patterns in the .gitignore file.
 | 
				
			||||||
func getForbiddens(root string, fileList []string) ([]string, error) {
 | 
					func getForbiddens(root string) ([]string, error) {
 | 
				
			||||||
	var matches []string
 | 
						var matches []string
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	ignore, err := gitignore.NewFromFile("./.gitignore")
 | 
						// Create a gitignore instance from the .gitignore file
 | 
				
			||||||
 | 
						ignore, err := gitignore.NewFromFile(filepath.Join(root, ".gitignore"))
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		return nil, err
 | 
							return nil, err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
| 
						 | 
					@ -25,29 +26,36 @@ func getForbiddens(root string, fileList []string) ([]string, error) {
 | 
				
			||||||
			return err
 | 
								return err
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		if info.IsDir() && (info.Name() == ".") {
 | 
							if info.IsDir() {
 | 
				
			||||||
 | 
								if info.Name() == ".git" {
 | 
				
			||||||
 | 
									return filepath.SkipDir
 | 
				
			||||||
 | 
								} else if info.Name() == root {
 | 
				
			||||||
				return nil
 | 
									return nil
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
		if info.IsDir() && (info.Name() == ".git") {
 | 
							}
 | 
				
			||||||
			return filepath.SkipDir
 | 
					
 | 
				
			||||||
		} else {
 | 
							// Get the relative path to the git repo root
 | 
				
			||||||
			match := ignore.Relative(info.Name(), true)
 | 
							relPath, err := filepath.Rel(root, path)
 | 
				
			||||||
			if match != nil {
 | 
							if err != nil {
 | 
				
			||||||
				if match.Ignore() {
 | 
								return err
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							match := ignore.Relative(relPath, true)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							// Check if the relative file path should be ignored based on the .gitignore rules
 | 
				
			||||||
 | 
							if match != nil && match.Ignore() {
 | 
				
			||||||
			matches = append(matches, path)
 | 
								matches = append(matches, path)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
			}
 | 
					
 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
		return nil
 | 
							return nil
 | 
				
			||||||
	})
 | 
						})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	return matches, err
 | 
						return matches, err
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// forbiddenCheck checks for forbidden files in the specified root directory.
 | 
					// 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.
 | 
					// It prints the list of forbidden files found, along with instructions on how to fix them.
 | 
				
			||||||
func ForbiddenCheck(rootDir string, regexList []string) error {
 | 
					func ForbiddenCheck(rootDir string) error {
 | 
				
			||||||
	forbids, err := getForbiddens(rootDir, regexList)
 | 
						forbids, err := getForbiddens(rootDir)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		slog.Error("getting forbiddens", "error", err)
 | 
							slog.Error("getting forbiddens", "error", err)
 | 
				
			||||||
		return fmt.Errorf("error getting forbiddens: %w", err)
 | 
							return fmt.Errorf("error getting forbiddens: %w", err)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue
	
	Block a user