github.com/joey-fossa/fossa-cli@v0.7.34-0.20190708193710-569f1e8679f0/analyzers/golang/lockfile.go (about) 1 package golang 2 3 import ( 4 "path/filepath" 5 6 "github.com/apex/log" 7 "github.com/pkg/errors" 8 9 "github.com/fossas/fossa-cli/analyzers/golang/resolver" 10 "github.com/fossas/fossa-cli/files" 11 ) 12 13 // Errors that occur while finding lockfiles. 14 var ( 15 ErrNoLockfileInDir = errors.New("could not find lockfile in directory") 16 ErrNoNearestLockfile = errors.New("could not find nearest lockfile of directory") 17 ) 18 19 // LockfileIn returns the type of lockfile within a directory, or 20 // ErrNoLockfileInDir if none is found. 21 func LockfileIn(dirname string) (resolver.Type, error) { 22 log.Debugf("%#v", dirname) 23 24 lockfiles := [][2]string{ 25 [2]string{"gomodules", filepath.Join(dirname, "go.mod")}, 26 [2]string{"godep", filepath.Join(dirname, "Godeps", "Godeps.json")}, 27 [2]string{"govendor", filepath.Join(dirname, "vendor", "vendor.json")}, 28 [2]string{"dep", filepath.Join(dirname, "Gopkg.toml")}, 29 [2]string{"vndr", filepath.Join(dirname, "vendor.conf")}, 30 [2]string{"glide", filepath.Join(dirname, "glide.yaml")}, 31 [2]string{"gdm", filepath.Join(dirname, "Godeps")}, 32 } 33 34 for _, lockfile := range lockfiles { 35 ok, err := files.Exists(lockfile[1]) 36 if err != nil { 37 return "", err 38 } 39 if ok { 40 return resolver.Type(lockfile[0]), nil 41 } 42 } 43 return "", ErrNoLockfileInDir 44 } 45 46 // NearestLockfile returns the type and directory of the nearest lockfile in an 47 // ancestor directory, or ErrNoNearestLockfile if none is found. 48 func NearestLockfile(dirname string) (resolver.Type, string, error) { 49 var tool resolver.Type 50 manifestDir, err := files.WalkUp(dirname, func(d string) (err error) { 51 tool, err = LockfileIn(d) 52 if err == ErrNoLockfileInDir { 53 return nil 54 } 55 if err != nil { 56 return err 57 } 58 return files.ErrStopWalk 59 }) 60 if err == files.ErrDirNotFound { 61 return "", "", ErrNoNearestLockfile 62 } 63 return tool, manifestDir, err 64 }