github.com/gsquire/gb@v0.4.4-0.20161112235727-3982dc872064/cmd/path.go (about) 1 package cmd 2 3 import ( 4 "fmt" 5 "os" 6 "path/filepath" 7 8 "github.com/pkg/errors" 9 ) 10 11 // FindProjectroot works upwards from path seaching for the 12 // src/ directory which identifies the project root. 13 func FindProjectroot(path string) (string, error) { 14 if path == "" { 15 return "", errors.New("project root is blank") 16 } 17 start := path 18 for path != filepath.Dir(path) { 19 root := filepath.Join(path, "src") 20 if _, err := os.Stat(root); err != nil { 21 if os.IsNotExist(err) { 22 path = filepath.Dir(path) 23 continue 24 } 25 return "", err 26 } 27 return path, nil 28 } 29 return "", fmt.Errorf(`could not find project root in "%s" or its parents`, start) 30 }