refractor(healthcheck/forbidden): use go-gitignore package
This commit is contained in:
		
							parent
							
								
									eb0ad570ee
								
							
						
					
					
						commit
						84e168dad6
					
				|  | @ -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, *localList) | 	err = healthcheck.ForbiddenCheck(*rootDir, gitWhitelist) | ||||||
| 	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()) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
							
								
								
									
										2
									
								
								go.mod
									
									
									
									
									
								
							
							
						
						
									
										2
									
								
								go.mod
									
									
									
									
									
								
							|  | @ -4,6 +4,7 @@ go 1.23.1 | ||||||
| 
 | 
 | ||||||
| require ( | require ( | ||||||
| 	github.com/criyle/go-judge v1.8.5 | 	github.com/criyle/go-judge v1.8.5 | ||||||
|  | 	github.com/denormal/go-gitignore v0.0.0-20180930084346-ae8ad1d07817 | ||||||
| 	github.com/go-git/go-git/v5 v5.12.0 | 	github.com/go-git/go-git/v5 v5.12.0 | ||||||
| 	github.com/jinzhu/copier v0.4.0 | 	github.com/jinzhu/copier v0.4.0 | ||||||
| 	github.com/koding/multiconfig v0.0.0-20171124222453-69c27309b2d7 | 	github.com/koding/multiconfig v0.0.0-20171124222453-69c27309b2d7 | ||||||
|  | @ -21,6 +22,7 @@ require ( | ||||||
| 	github.com/creack/pty v1.1.21 // indirect | 	github.com/creack/pty v1.1.21 // indirect | ||||||
| 	github.com/criyle/go-sandbox v0.10.4 // indirect | 	github.com/criyle/go-sandbox v0.10.4 // indirect | ||||||
| 	github.com/cyphar/filepath-securejoin v0.2.4 // indirect | 	github.com/cyphar/filepath-securejoin v0.2.4 // indirect | ||||||
|  | 	github.com/danwakefield/fnmatch v0.0.0-20160403171240-cbb64ac3d964 // indirect | ||||||
| 	github.com/emirpasic/gods v1.18.1 // indirect | 	github.com/emirpasic/gods v1.18.1 // indirect | ||||||
| 	github.com/fatih/camelcase v1.0.0 // indirect | 	github.com/fatih/camelcase v1.0.0 // indirect | ||||||
| 	github.com/fatih/structs v1.1.0 // indirect | 	github.com/fatih/structs v1.1.0 // indirect | ||||||
|  |  | ||||||
							
								
								
									
										4
									
								
								go.sum
									
									
									
									
									
								
							
							
						
						
									
										4
									
								
								go.sum
									
									
									
									
									
								
							|  | @ -23,9 +23,13 @@ github.com/criyle/go-sandbox v0.10.4 h1:EHJrJj5V/VSrjm1Y0ZJAea5zPASoOtn1CPZRzhTU | ||||||
| github.com/criyle/go-sandbox v0.10.4/go.mod h1:sYJUuTmJ72Jilkc1/PO7eDdpJq3rOZ55o8MxzP80vw0= | github.com/criyle/go-sandbox v0.10.4/go.mod h1:sYJUuTmJ72Jilkc1/PO7eDdpJq3rOZ55o8MxzP80vw0= | ||||||
| github.com/cyphar/filepath-securejoin v0.2.4 h1:Ugdm7cg7i6ZK6x3xDF1oEu1nfkyfH53EtKeQYTC3kyg= | github.com/cyphar/filepath-securejoin v0.2.4 h1:Ugdm7cg7i6ZK6x3xDF1oEu1nfkyfH53EtKeQYTC3kyg= | ||||||
| github.com/cyphar/filepath-securejoin v0.2.4/go.mod h1:aPGpWjXOXUn2NCNjFvBE6aRxGGx79pTxQpKOJNYHHl4= | github.com/cyphar/filepath-securejoin v0.2.4/go.mod h1:aPGpWjXOXUn2NCNjFvBE6aRxGGx79pTxQpKOJNYHHl4= | ||||||
|  | github.com/danwakefield/fnmatch v0.0.0-20160403171240-cbb64ac3d964 h1:y5HC9v93H5EPKqaS1UYVg1uYah5Xf51mBfIoWehClUQ= | ||||||
|  | github.com/danwakefield/fnmatch v0.0.0-20160403171240-cbb64ac3d964/go.mod h1:Xd9hchkHSWYkEqJwUGisez3G1QY8Ryz0sdWrLPMGjLk= | ||||||
| github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||||||
| github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= | github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= | ||||||
| github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||||||
|  | github.com/denormal/go-gitignore v0.0.0-20180930084346-ae8ad1d07817 h1:0nsrg//Dc7xC74H/TZ5sYR8uk4UQRNjsw8zejqH5a4Q= | ||||||
|  | github.com/denormal/go-gitignore v0.0.0-20180930084346-ae8ad1d07817/go.mod h1:C/+sI4IFnEpCn6VQ3GIPEp+FrQnQw+YQP3+n+GdGq7o= | ||||||
| github.com/elazarl/goproxy v0.0.0-20230808193330-2592e75ae04a h1:mATvB/9r/3gvcejNsXKSkQ6lcIaNec2nyfOdlTBR2lU= | github.com/elazarl/goproxy v0.0.0-20230808193330-2592e75ae04a h1:mATvB/9r/3gvcejNsXKSkQ6lcIaNec2nyfOdlTBR2lU= | ||||||
| github.com/elazarl/goproxy v0.0.0-20230808193330-2592e75ae04a/go.mod h1:Ro8st/ElPeALwNFlcTpWmkr6IoMFfkjXAvTHpevnDsM= | github.com/elazarl/goproxy v0.0.0-20230808193330-2592e75ae04a/go.mod h1:Ro8st/ElPeALwNFlcTpWmkr6IoMFfkjXAvTHpevnDsM= | ||||||
| github.com/emirpasic/gods v1.18.1 h1:FXtiHYKDGKCW2KzwZKx0iC0PQmdlorYgdFG9jPXJ1Bc= | github.com/emirpasic/gods v1.18.1 h1:FXtiHYKDGKCW2KzwZKx0iC0PQmdlorYgdFG9jPXJ1Bc= | ||||||
|  |  | ||||||
|  | @ -1,67 +1,40 @@ | ||||||
| package healthcheck | package healthcheck | ||||||
| 
 | 
 | ||||||
| import ( | import ( | ||||||
| 	"bufio" |  | ||||||
| 	"fmt" | 	"fmt" | ||||||
| 	"log/slog" | 	"log/slog" | ||||||
| 	"os" | 	"os" | ||||||
| 	"path/filepath" | 	"path/filepath" | ||||||
| 	"regexp" |  | ||||||
| 	"strings" | 	"strings" | ||||||
|  | 
 | ||||||
|  | 	"github.com/denormal/go-gitignore" | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| // 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 do not match the specified regex patterns in the given file list.
 | ||||||
| func getForbiddens(root string, fileList []string, localList string) ([]string, error) { | func getForbiddens(root string, fileList []string) ([]string, error) { | ||||||
| 	var matches []string | 	var matches []string | ||||||
| 
 | 
 | ||||||
| 	var regexList []*regexp.Regexp | 	ignore, err := gitignore.NewFromFile("./.gitignore") | ||||||
| 	regexList, err := getRegex(fileList) |  | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return nil, err | 		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 { | 	err = filepath.Walk(root, func(path string, info os.FileInfo, err error) error { | ||||||
| 		if err != nil { | 		if err != nil { | ||||||
| 			return err | 			return err | ||||||
| 		} | 		} | ||||||
| 
 | 
 | ||||||
| 		if info.IsDir() { | 		if info.IsDir() && info.Name() == ".git" { | ||||||
| 			if info.Name() == ".git" || (localList != "" && inString(info.Name(), dirs)) { | 			return filepath.SkipDir | ||||||
| 				return filepath.SkipDir |  | ||||||
| 			} |  | ||||||
| 		} else { | 		} else { | ||||||
| 			match := false | 			match := ignore.Relative(info.Name(), true) | ||||||
| 			for _, regex := range regexList { | 			if match != nil { | ||||||
| 				if regex.MatchString(info.Name()) { | 				if match.Ignore() { | ||||||
| 					match = true | 					matches = append(matches, path) | ||||||
| 					break |  | ||||||
| 				} | 				} | ||||||
| 			} | 			} | ||||||
| 
 |  | ||||||
| 			if !match { |  | ||||||
| 				matches = append(matches, path) |  | ||||||
| 			} |  | ||||||
| 		} | 		} | ||||||
| 
 |  | ||||||
| 		return nil | 		return nil | ||||||
| 	}) | 	}) | ||||||
| 
 | 
 | ||||||
|  | @ -70,8 +43,8 @@ func getForbiddens(root string, fileList []string, localList string) ([]string, | ||||||
| 
 | 
 | ||||||
| // 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, localList string) error { | func ForbiddenCheck(rootDir string, regexList []string) error { | ||||||
| 	forbids, err := getForbiddens(rootDir, regexList, localList) | 	forbids, err := getForbiddens(rootDir, regexList) | ||||||
| 	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
	 zzjc1234
						zzjc1234