forked from xkeyideal/glide
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gom.go
112 lines (92 loc) · 2.43 KB
/
gom.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package gom
import (
"errors"
"os"
"path/filepath"
"github.com/xkeyideal/glide/cfg"
"github.com/xkeyideal/glide/msg"
gpath "github.com/xkeyideal/glide/path"
"github.com/xkeyideal/glide/util"
)
// Has returns true if this dir has a Gomfile.
func Has(dir string) bool {
path := filepath.Join(dir, "Gomfile")
fi, err := os.Stat(path)
return err == nil && !fi.IsDir()
}
// Parse parses a Gomfile.
func Parse(dir string) ([]*cfg.Dependency, error) {
path := filepath.Join(dir, "Gomfile")
if fi, err := os.Stat(path); err != nil || fi.IsDir() {
return []*cfg.Dependency{}, nil
}
msg.Info("Found Gomfile in %s", gpath.StripBasepath(dir))
msg.Info("--> Parsing Gomfile metadata...")
buf := []*cfg.Dependency{}
goms, err := parseGomfile(path)
if err != nil {
return []*cfg.Dependency{}, err
}
for _, gom := range goms {
// Do we need to skip this dependency?
if val, ok := gom.options["skipdep"]; ok && val.(string) == "true" {
continue
}
// Check for custom cloning command
if _, ok := gom.options["command"]; ok {
return []*cfg.Dependency{}, errors.New("Glide does not support custom Gomfile commands")
}
// Check for groups/environments
if val, ok := gom.options["group"]; ok {
groups := toStringSlice(val)
if !stringsContain(groups, "development") && !stringsContain(groups, "production") {
// right now we only support development and production
msg.Info("Skipping dependency '%s' because it isn't in the development or production group", gom.name)
continue
}
}
pkg, sub := util.NormalizeName(gom.name)
dep := &cfg.Dependency{
Name: pkg,
}
if len(sub) > 0 {
dep.Subpackages = []string{sub}
}
// Check for a specific revision
if val, ok := gom.options["commit"]; ok {
dep.Reference = val.(string)
}
if val, ok := gom.options["tag"]; ok {
dep.Reference = val.(string)
}
if val, ok := gom.options["branch"]; ok {
dep.Reference = val.(string)
}
// Parse goos and goarch
if val, ok := gom.options["goos"]; ok {
dep.Os = toStringSlice(val)
}
if val, ok := gom.options["goarch"]; ok {
dep.Arch = toStringSlice(val)
}
buf = append(buf, dep)
}
return buf, nil
}
func stringsContain(v []string, key string) bool {
for _, s := range v {
if s == key {
return true
}
}
return false
}
func toStringSlice(v interface{}) []string {
if v, ok := v.(string); ok {
return []string{v}
}
if v, ok := v.([]string); ok {
return v
}
return []string{}
}