diff options
author | Robert <rspier@users.noreply.github.com> | 2019-04-19 12:20:20 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-04-19 12:20:20 -0700 |
commit | f9a750789b8005fb471e82945f1e901a9395a7ec (patch) | |
tree | 3f2ed1f13299fb8063c9a0fb5142daa7b7235d34 /file_system_resources.go | |
parent | b6c7645af5067f64ba9c75d999ceca0bba5c18eb (diff) | |
download | licenseclassifier-f9a750789b8005fb471e82945f1e901a9395a7ec.tar.gz |
use Caller() to find the licenses.db file instead of GOPATH (#9)
Instead of looking for the files relative to GOPATH, instead we look for
files relative to the directory file_system_resources.go is in, unless an
absolute path is specified.
This will work under traditional GOPATH and `go mod` scenarios. It doesn't
help with binaries that are built and the source code moved or removed.
Diffstat (limited to 'file_system_resources.go')
-rw-r--r-- | file_system_resources.go | 45 |
1 files changed, 24 insertions, 21 deletions
diff --git a/file_system_resources.go b/file_system_resources.go index 0d92e3a..96b2e6f 100644 --- a/file_system_resources.go +++ b/file_system_resources.go @@ -15,10 +15,12 @@ package licenseclassifier import ( - "go/build" + "fmt" "io/ioutil" "os" "path/filepath" + "runtime" + "strings" ) const ( @@ -32,34 +34,35 @@ const ( ForbiddenLicenseArchive = "forbidden_licenses.db" ) -func findInGOPATH(rel string) (fullPath string, err error) { - for _, path := range filepath.SplitList(build.Default.GOPATH) { - fullPath := filepath.Join(path, rel) - if _, err := os.Stat(fullPath); err != nil { - if os.IsNotExist(err) { - continue - } - return "", err - } - return fullPath, nil +// lcRoot computes the location of the licenses data in the licenseclassifier source tree based on the location of this file. +func lcRoot() (string, error) { + _, filename, _, ok := runtime.Caller(0) + if !ok { + return "", fmt.Errorf("unable to compute path of licenseclassifier source") } - return "", nil + // this file must be in the root of the package, or the relative paths will be wrong. + return filepath.Join(filepath.Dir(filename), "licenses"), nil } -// ReadLicenseFile locates and reads the license file. +// ReadLicenseFile locates and reads the license archive file. Absolute paths are used unmodified. Relative paths are expected to be in the licenses directory of the licenseclassifier package. func ReadLicenseFile(filename string) ([]byte, error) { - archive, err := findInGOPATH(filepath.Join(LicenseDirectory, filename)) - if err != nil || archive == "" { - return nil, err + if strings.HasPrefix(filename, "/") { + return ioutil.ReadFile(filename) } - return ioutil.ReadFile(archive) + + root, err := lcRoot() + if err != nil { + return nil, fmt.Errorf("error locating licenses directory: %v", err) + } + return ioutil.ReadFile(filepath.Join(root, filename)) } // ReadLicenseDir reads directory containing the license files. func ReadLicenseDir() ([]os.FileInfo, error) { - filename, err := findInGOPATH(filepath.Join(LicenseDirectory, LicenseArchive)) - if err != nil || filename == "" { - return nil, err + root, err := lcRoot() + if err != nil { + return nil, fmt.Errorf("error locating licenses directory: %v", err) } - return ioutil.ReadDir(filepath.Dir(filename)) + + return ioutil.ReadDir(root) } |