Skip to content

Commit 2cd9bd8

Browse files
committed
Go: Move IsGolangVendorDirectory to util package
1 parent e1c2805 commit 2cd9bd8

File tree

2 files changed

+13
-13
lines changed

2 files changed

+13
-13
lines changed

go/extractor/configurebaseline/configurebaseline.go

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,12 @@ package configurebaseline
33
import (
44
"encoding/json"
55
"io/fs"
6-
"os"
76
"path"
87
"path/filepath"
98

109
"github.com/github/codeql-go/extractor/util"
1110
)
1211

13-
func fileExists(path string) bool {
14-
stat, err := os.Stat(path)
15-
return err == nil && stat.Mode().IsRegular()
16-
}
17-
18-
// Decides if `dirPath` is a vendor directory by testing whether it is called `vendor`
19-
// and contains a `modules.txt` file.
20-
func isGolangVendorDirectory(dirPath string) bool {
21-
return filepath.Base(dirPath) == "vendor" && fileExists(filepath.Join(dirPath, "modules.txt"))
22-
}
23-
2412
type BaselineConfig struct {
2513
PathsIgnore []string `json:"paths-ignore"`
2614
}
@@ -38,7 +26,7 @@ func GetConfigBaselineAsJSON(rootDir string) ([]byte, error) {
3826
// it will not be extracted either.
3927
return nil
4028
}
41-
if isGolangVendorDirectory(dirPath) {
29+
if util.IsGolangVendorDirectory(dirPath) {
4230
// Note that CodeQL expects a forward-slash-separated path, even on Windows.
4331
vendorDirs = append(vendorDirs, path.Join(filepath.ToSlash(dirPath), "**"))
4432
return filepath.SkipDir

go/extractor/util/util.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,3 +287,15 @@ func getImportPathFromRepoURL(repourl string) string {
287287
path = regexp.MustCompile(`^/+|\.git$`).ReplaceAllString(path, "")
288288
return host + "/" + path
289289
}
290+
291+
// Decides if `path` refers to a file that exists.
292+
func fileExists(path string) bool {
293+
stat, err := os.Stat(path)
294+
return err == nil && stat.Mode().IsRegular()
295+
}
296+
297+
// Decides if `dirPath` is a vendor directory by testing whether it is called `vendor`
298+
// and contains a `modules.txt` file.
299+
func IsGolangVendorDirectory(dirPath string) bool {
300+
return filepath.Base(dirPath) == "vendor" && fileExists(filepath.Join(dirPath, "modules.txt"))
301+
}

0 commit comments

Comments
 (0)