Skip to content
This repository has been archived by the owner on Sep 9, 2020. It is now read-only.

Commit

Permalink
Add funcs for finding the project root
Browse files Browse the repository at this point in the history
Project root inference is done by searching for an appropriately-named
file (currently manifest.json). Search should generally proceed from the
cwd upwards towards root.
  • Loading branch information
sdboyer committed Oct 18, 2016
1 parent 2f506ee commit db604c9
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@ import (
"flag"
"fmt"
"os"
"path/filepath"
)

const ManifestName = "manifest.json"
const LockName = "lock.json"

func main() {
flag.Parse()

Expand Down Expand Up @@ -186,3 +190,41 @@ var getCmd = &command{
-vendor (?) get to workspace or vendor directory
`,
}

func findProjectRootFromWD() (string, error) {
path, err := os.Getwd()
if err != nil {
return "", fmt.Errorf("could not get working directory: %s", err)
}
return findProjectRoot(path)
}

func findProjectRoot(from string) (string, error) {
var f func(string) (string, error)
f = func(dir string) (string, error) {

fullpath := filepath.Join(dir, ManifestName)

if _, err := os.Stat(fullpath); err == nil {
return dir, nil
} else if !os.IsNotExist(err) {
// Some err other than non-existence - return that out
return "", err
}

base := filepath.Dir(dir)
if base == dir {
return "", fmt.Errorf("cannot resolve parent of %s", base)
}

return f(base)
}

path, err := f(from)
if err != nil {
return "", fmt.Errorf("error while searching for manifest: %s", err)
} else if path == "" {
return "", fmt.Errorf("could not find manifest in any parent of %s", from)
}
return path, nil
}

0 comments on commit db604c9

Please sign in to comment.