/
gvt.go
66 lines (53 loc) · 1.3 KB
/
gvt.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
package discover
import (
"encoding/json"
"io/ioutil"
"os"
"path"
)
// Gvt is a discoverer for gvt Go package manager.
type Gvt struct{}
// Name returns name of Gvt discoverer.
func (g Gvt) Name() string {
return "gvt"
}
// InUse checks whether project is using gvt as a package manager.
func (g Gvt) InUse(projectPath string) (bool, error) {
_, err := os.Stat(gvtFilePath(projectPath))
if err != nil {
return false, err
}
return true, nil
}
func gvtFilePath(projectPath string) string {
return path.Join(projectPath, "vendor", "manifest")
}
// Repositories discovers repositories belonging to packages imported inside a project managed by Gvt.
func (g Gvt) Repositories(projectPath string) (RepoMap, error) {
list, err := packageListGvt(projectPath)
if err != nil {
return nil, err
}
repoMap := RepoMap{}
for _, p := range list.Dependencies {
repoMap.add(p.Importpath)
}
return repoMap, nil
}
type gvtList struct {
Dependencies []struct {
Importpath string `json:"importpath"`
} `json:"dependencies"`
}
func packageListGvt(projectPath string) (gvtList, error) {
list := gvtList{}
content, err := ioutil.ReadFile(gvtFilePath(projectPath))
if err != nil {
return gvtList{}, err
}
err = json.Unmarshal(content, &list)
if err != nil {
return gvtList{}, err
}
return list, nil
}