github.com/wawandco/oxpecker@v1.5.7-0.20210910201653-5958d4afdd89/internal/info/rootfolder.go (about) 1 package info 2 3 import ( 4 "os" 5 "path/filepath" 6 ) 7 8 // RootFolder looks for the root folder by scanning from the current directory and up 9 // for the first occurrence of go.mod, if it does not find the module returns empty string. 10 func RootFolder() string { 11 basePath, err := os.Getwd() 12 if err != nil { 13 return "" 14 } 15 16 targetPath := basePath 17 for { 18 _, err := filepath.Rel(basePath, targetPath) 19 if err != nil { 20 return "" 21 } 22 23 abso, err := filepath.Abs(targetPath) 24 if err != nil { 25 return "" 26 } 27 28 path := filepath.Join(targetPath, "go.mod") 29 _, err = os.Stat(path) 30 31 if err != nil && abso == "/" { 32 break 33 } 34 35 if err != nil { 36 targetPath += "/.." 37 38 continue 39 } 40 41 return abso 42 } 43 44 return "" 45 }