From 2cd9bd8a43c68f65084d1fc00c9ec149df02fabb Mon Sep 17 00:00:00 2001 From: "Michael B. Gale" Date: Mon, 4 Nov 2024 12:05:44 +0000 Subject: [PATCH] Go: Move `IsGolangVendorDirectory` to `util` package --- .../configurebaseline/configurebaseline.go | 14 +------------- go/extractor/util/util.go | 12 ++++++++++++ 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/go/extractor/configurebaseline/configurebaseline.go b/go/extractor/configurebaseline/configurebaseline.go index 04037d61425..062289f7baa 100644 --- a/go/extractor/configurebaseline/configurebaseline.go +++ b/go/extractor/configurebaseline/configurebaseline.go @@ -3,24 +3,12 @@ package configurebaseline import ( "encoding/json" "io/fs" - "os" "path" "path/filepath" "github.com/github/codeql-go/extractor/util" ) -func fileExists(path string) bool { - stat, err := os.Stat(path) - return err == nil && stat.Mode().IsRegular() -} - -// Decides if `dirPath` is a vendor directory by testing whether it is called `vendor` -// and contains a `modules.txt` file. -func isGolangVendorDirectory(dirPath string) bool { - return filepath.Base(dirPath) == "vendor" && fileExists(filepath.Join(dirPath, "modules.txt")) -} - type BaselineConfig struct { PathsIgnore []string `json:"paths-ignore"` } @@ -38,7 +26,7 @@ func GetConfigBaselineAsJSON(rootDir string) ([]byte, error) { // it will not be extracted either. return nil } - if isGolangVendorDirectory(dirPath) { + if util.IsGolangVendorDirectory(dirPath) { // Note that CodeQL expects a forward-slash-separated path, even on Windows. vendorDirs = append(vendorDirs, path.Join(filepath.ToSlash(dirPath), "**")) return filepath.SkipDir diff --git a/go/extractor/util/util.go b/go/extractor/util/util.go index 595326496d8..9a2d1298815 100644 --- a/go/extractor/util/util.go +++ b/go/extractor/util/util.go @@ -287,3 +287,15 @@ func getImportPathFromRepoURL(repourl string) string { path = regexp.MustCompile(`^/+|\.git$`).ReplaceAllString(path, "") return host + "/" + path } + +// Decides if `path` refers to a file that exists. +func fileExists(path string) bool { + stat, err := os.Stat(path) + return err == nil && stat.Mode().IsRegular() +} + +// Decides if `dirPath` is a vendor directory by testing whether it is called `vendor` +// and contains a `modules.txt` file. +func IsGolangVendorDirectory(dirPath string) bool { + return filepath.Base(dirPath) == "vendor" && fileExists(filepath.Join(dirPath, "modules.txt")) +}