forked from golang/dep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproject.go
73 lines (60 loc) · 1.63 KB
/
project.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Copyright 2016 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package dep
import (
"fmt"
"os"
"path/filepath"
"github.com/sdboyer/gps"
)
var errProjectNotFound = fmt.Errorf("could not find project %s, use dep init to initiate a manifest", ManifestName)
func findProjectRootFromWD() (string, error) {
path, err := os.Getwd()
if err != nil {
return "", fmt.Errorf("could not get working directory: %s", err)
}
return findProjectRoot(path)
}
// findProjectRoot searches from the starting directory upwards looking for a
// manifest file until we get to the root of the filesystem.
func findProjectRoot(from string) (string, error) {
for {
mp := filepath.Join(from, ManifestName)
_, err := os.Stat(mp)
if err == nil {
return from, nil
}
if !os.IsNotExist(err) {
// Some err other than non-existence - return that out
return "", err
}
parent := filepath.Dir(from)
if parent == from {
return "", errProjectNotFound
}
from = parent
}
}
type Project struct {
// AbsRoot is the absolute path to the root directory of the project.
AbsRoot string
// ImportRoot is the import path of the project's root directory.
ImportRoot gps.ProjectRoot
Manifest *Manifest
Lock *Lock
}
// MakeParams is a simple helper to create a gps.SolveParameters without setting
// any nils incorrectly.
func (p *Project) MakeParams() gps.SolveParameters {
params := gps.SolveParameters{
RootDir: p.AbsRoot,
}
if p.Manifest != nil {
params.Manifest = p.Manifest
}
if p.Lock != nil {
params.Lock = p.Lock
}
return params
}