@@ -20,10 +20,8 @@ import (
2020 "io/ioutil"
2121 "log"
2222 "os"
23- "os/user"
2423 "path/filepath"
2524 "regexp"
26- "runtime"
2725 "sort"
2826 "strings"
2927
@@ -178,36 +176,13 @@ func saveOutput(filename, format string, issues []*gosec.Issue, metrics *gosec.M
178176 return nil
179177}
180178
181- func getenv (key , userDefault string ) string {
182- if val := os .Getenv (key ); val != "" {
183- return val
184- }
185- return userDefault
186- }
187-
188- func gopath () []string {
189- defaultGoPath := runtime .GOROOT ()
190- if u , err := user .Current (); err == nil {
191- defaultGoPath = filepath .Join (u .HomeDir , "go" )
192- }
193- path := getenv ("GOPATH" , defaultGoPath )
194- paths := strings .Split (path , string (os .PathListSeparator ))
195- for idx , path := range paths {
196- if abs , err := filepath .Abs (path ); err == nil {
197- paths [idx ] = abs
198- }
199- }
200- return paths
201- }
202-
203- func cleanPath (path string , gopaths []string ) (string , error ) {
204-
179+ func cleanPath (path string ) (string , error ) {
205180 cleanFailed := fmt .Errorf ("%s is not within the $GOPATH and cannot be processed" , path )
206181 nonRecursivePath := strings .TrimSuffix (path , "/..." )
207182 // do not attempt to clean directs that are resolvable on gopath
208183 if _ , err := os .Stat (nonRecursivePath ); err != nil && os .IsNotExist (err ) {
209184 log .Printf ("directory %s doesn't exist, checking if is a package on $GOPATH" , path )
210- for _ , basedir := range gopaths {
185+ for _ , basedir := range gosec . Gopath () {
211186 dir := filepath .Join (basedir , "src" , nonRecursivePath )
212187 if st , err := os .Stat (dir ); err == nil && st .IsDir () {
213188 log .Printf ("located %s in %s" , path , dir )
@@ -218,24 +193,17 @@ func cleanPath(path string, gopaths []string) (string, error) {
218193 }
219194
220195 // ensure we resolve package directory correctly based on $GOPATH
221- abspath , err := filepath . Abs (path )
196+ pkgPath , err := gosec . GetPkgRelativePath (path )
222197 if err != nil {
223- abspath = path
224- }
225- for _ , base := range gopaths {
226- projectRoot := filepath .FromSlash (fmt .Sprintf ("%s/src/" , base ))
227- if strings .HasPrefix (abspath , projectRoot ) {
228- return strings .TrimPrefix (abspath , projectRoot ), nil
229- }
198+ return "" , cleanFailed
230199 }
231- return "" , cleanFailed
200+ return pkgPath , nil
232201}
233202
234203func cleanPaths (paths []string ) []string {
235- gopaths := gopath ()
236204 var clean []string
237205 for _ , path := range paths {
238- cleaned , err := cleanPath (path , gopaths )
206+ cleaned , err := cleanPath (path )
239207 if err != nil {
240208 log .Fatal (err )
241209 }
@@ -306,7 +274,7 @@ func main() {
306274
307275 var packages []string
308276 // Iterate over packages on the import paths
309- gopaths := gopath ()
277+ gopaths := gosec . Gopath ()
310278 for _ , pkg := range gotool .ImportPaths (cleanPaths (flag .Args ())) {
311279
312280 // Skip vendor directory
0 commit comments