-
Notifications
You must be signed in to change notification settings - Fork 0
/
godeps.go
69 lines (58 loc) · 1.27 KB
/
godeps.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
package main
import (
"encoding/json"
"io/ioutil"
"os"
"path/filepath"
"strings"
)
type Godeps struct {
ImportPath string
GoVersion string
GodepVersion string
Packages []string `json:",omitempty"`
Deps []Dependency
}
type Dependency struct {
ImportPath string
Comment string `json:",omitempty"`
Rev string
}
func loadGodeps() (*Godeps, error) {
dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
return nil, err
}
bytes, err := ioutil.ReadFile(dir + "/Godeps.json")
if err != nil {
bytes, err = ioutil.ReadFile(dir + "/Godeps/Godeps.json")
if err != nil {
return nil, err
}
}
var gdeps Godeps
err = json.Unmarshal(bytes, &gdeps)
if err != nil {
return nil, err
}
gdeps.Deps = mergeDeps(gdeps.Deps)
return &gdeps, nil
}
func mergeDeps(deps []Dependency) []Dependency {
alreadyLoadedDeps := make(map[string]bool)
mergedDeps := make([]Dependency, 0)
for _, d := range deps {
splitted := strings.Split(d.ImportPath, "/")
cutLen := 3
if len(splitted) < 3 {
cutLen = len(splitted)
}
d.ImportPath = strings.Join(splitted[:cutLen], "/")
found := alreadyLoadedDeps[d.ImportPath]
if !found {
alreadyLoadedDeps[d.ImportPath] = true
mergedDeps = append(mergedDeps, d)
}
}
return mergedDeps
}