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

Commit b17dca4

Browse files
author
Edward Muller
authored
Merge pull request #17 from golang/findRoot
Simplify findProjectRoot
2 parents c391723 + 6962530 commit b17dca4

File tree

2 files changed

+26
-25
lines changed

2 files changed

+26
-25
lines changed

init.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ func runInit(args []string) error {
117117

118118
func isStdLib(i string) bool {
119119
switch i {
120-
case "bytes", "encoding/hex", "encoding/json", "flag", "fmt", "io", "os", "path/filepath", "strings", "text/tabwriter":
120+
case "bytes", "encoding/hex", "errors", "sort", "encoding/json", "flag", "fmt", "io", "os", "path/filepath", "strings", "text/tabwriter":
121121
return true
122122
}
123123
return false

main.go

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package main
66

77
import (
8+
"errors"
89
"flag"
910
"fmt"
1011
"os"
@@ -57,6 +58,10 @@ var commands = []*command{
5758
// help added here at init time.
5859
}
5960

61+
var (
62+
errProjectNotFound = errors.New("no project could be found")
63+
)
64+
6065
func init() {
6166
// Defeat circular declarations by appending
6267
// this to the list at init time.
@@ -134,34 +139,27 @@ func findProjectRootFromWD() (string, error) {
134139
return findProjectRoot(path)
135140
}
136141

142+
// search upwards looking for a manifest file until we get to the root of the
143+
// filesystem
137144
func findProjectRoot(from string) (string, error) {
138-
var f func(string) (string, error)
139-
f = func(dir string) (string, error) {
140-
141-
fullpath := filepath.Join(dir, manifestName)
145+
for {
146+
mp := filepath.Join(from, manifestName)
142147

143-
if _, err := os.Stat(fullpath); err == nil {
144-
return dir, nil
145-
} else if !os.IsNotExist(err) {
148+
_, err := os.Stat(mp)
149+
if err == nil {
150+
return from, nil
151+
}
152+
if !os.IsNotExist(err) {
146153
// Some err other than non-existence - return that out
147154
return "", err
148155
}
149156

150-
base := filepath.Dir(dir)
151-
if base == dir {
152-
return "", fmt.Errorf("cannot resolve parent of %s", base)
157+
parent := filepath.Dir(from)
158+
if parent == from {
159+
return "", errProjectNotFound
153160
}
154-
155-
return f(base)
161+
from = parent
156162
}
157-
158-
path, err := f(from)
159-
if err != nil {
160-
return "", fmt.Errorf("error while searching for manifest: %s", err)
161-
} else if path == "" {
162-
return "", fmt.Errorf("could not find manifest in any parent of %s", from)
163-
}
164-
return path, nil
165163
}
166164

167165
type project struct {
@@ -210,12 +208,15 @@ func loadProject(path string) (*project, error) {
210208
return nil, fmt.Errorf("could not determine project root - not on GOPATH")
211209
}
212210

213-
mp := filepath.Join(path, manifestName)
211+
mp := filepath.Join(p.absroot, manifestName)
214212
mf, err := os.Open(mp)
215213
if err != nil {
216-
// Should be impossible at this point for the manifest file not to
217-
// exist, so this is some other kind of err
218-
return nil, fmt.Errorf("could not open %s: %s", mp, err)
214+
if os.IsNotExist(err) {
215+
// TODO: list possible solutions? (dep init, cd $project)
216+
return nil, fmt.Errorf("no %v found in project root %v", manifestName, p.absroot)
217+
}
218+
// Unable to read the manifest file
219+
return nil, err
219220
}
220221
defer mf.Close()
221222

0 commit comments

Comments
 (0)