|
5 | 5 | package main
|
6 | 6 |
|
7 | 7 | import (
|
| 8 | + "errors" |
8 | 9 | "flag"
|
9 | 10 | "fmt"
|
10 | 11 | "os"
|
@@ -57,6 +58,10 @@ var commands = []*command{
|
57 | 58 | // help added here at init time.
|
58 | 59 | }
|
59 | 60 |
|
| 61 | +var ( |
| 62 | + errProjectNotFound = errors.New("no project could be found") |
| 63 | +) |
| 64 | + |
60 | 65 | func init() {
|
61 | 66 | // Defeat circular declarations by appending
|
62 | 67 | // this to the list at init time.
|
@@ -134,34 +139,27 @@ func findProjectRootFromWD() (string, error) {
|
134 | 139 | return findProjectRoot(path)
|
135 | 140 | }
|
136 | 141 |
|
| 142 | +// search upwards looking for a manifest file until we get to the root of the |
| 143 | +// filesystem |
137 | 144 | 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) |
142 | 147 |
|
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) { |
146 | 153 | // Some err other than non-existence - return that out
|
147 | 154 | return "", err
|
148 | 155 | }
|
149 | 156 |
|
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 |
153 | 160 | }
|
154 |
| - |
155 |
| - return f(base) |
| 161 | + from = parent |
156 | 162 | }
|
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 |
165 | 163 | }
|
166 | 164 |
|
167 | 165 | type project struct {
|
@@ -210,12 +208,15 @@ func loadProject(path string) (*project, error) {
|
210 | 208 | return nil, fmt.Errorf("could not determine project root - not on GOPATH")
|
211 | 209 | }
|
212 | 210 |
|
213 |
| - mp := filepath.Join(path, manifestName) |
| 211 | + mp := filepath.Join(p.absroot, manifestName) |
214 | 212 | mf, err := os.Open(mp)
|
215 | 213 | 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 |
219 | 220 | }
|
220 | 221 | defer mf.Close()
|
221 | 222 |
|
|
0 commit comments