/
dep.go
65 lines (51 loc) · 1.25 KB
/
dep.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
package discover
import (
"io/ioutil"
"os"
"path"
toml "github.com/pelletier/go-toml"
)
const depFilePath = "Gopkg.toml"
// Dep is a discoverer for dep Go package manager.
type Dep struct{}
// Name returns name of Dep discoverer.
func (d Dep) Name() string {
return "dep"
}
// InUse checks whether project is using dep as a package manager.
func (d Dep) InUse(projectPath string) (bool, error) {
_, err := os.Stat(path.Join(projectPath, depFilePath))
if err != nil {
return false, err
}
return true, nil
}
// Repositories discovers repositories belonging to packages imported inside a project managed by Dep.
func (d Dep) Repositories(projectPath string) (RepoMap, error) {
list, err := packageListDep(projectPath)
if err != nil {
return nil, err
}
repoMap := RepoMap{}
for _, c := range list.Constraint {
repoMap.add(c.Name)
}
return repoMap, nil
}
type depList struct {
Constraint []struct {
Name string `toml:"name"`
} `toml:"constraint"`
}
func packageListDep(projectPath string) (depList, error) {
content, err := ioutil.ReadFile(path.Join(projectPath, depFilePath))
if err != nil {
return depList{}, err
}
list := depList{}
err = toml.Unmarshal(content, &list)
if err != nil {
return depList{}, err
}
return list, nil
}