forked from Masterminds/glide
/
install.go
142 lines (120 loc) · 3.43 KB
/
install.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package cmd
import (
"io/ioutil"
"os"
"sync"
"github.com/Masterminds/cookoo"
"github.com/Masterminds/glide/cfg"
)
// LockFileExists checks if a lock file exists. If not it jumps to the update
// command.
func LockFileExists(c cookoo.Context, p *cookoo.Params) (interface{}, cookoo.Interrupt) {
fname := p.Get("filename", "glide.lock").(string)
if _, err := os.Stat(fname); err != nil {
Info("Lock file (glide.lock) does not exist. Performing update.")
return false, &cookoo.Reroute{"update"}
}
return true, nil
}
// LoadLockFile loads the lock file to the context and checks if it is correct
// for the loaded cfg file.
func LoadLockFile(c cookoo.Context, p *cookoo.Params) (interface{}, cookoo.Interrupt) {
fname := p.Get("filename", "glide.lock").(string)
conf := p.Get("conf", nil).(*cfg.Config)
yml, err := ioutil.ReadFile(fname)
if err != nil {
return nil, err
}
lock, err := cfg.LockfileFromYaml(yml)
if err != nil {
return nil, err
}
hash, err := conf.Hash()
if err != nil {
return nil, err
}
if hash != lock.Hash {
Warn("Lock file may be out of date. Hash check of YAML failed. You may need to run 'update'")
}
return lock, nil
}
// Install installs the dependencies from a Lockfile.
func Install(c cookoo.Context, p *cookoo.Params) (interface{}, cookoo.Interrupt) {
lock := p.Get("lock", nil).(*cfg.Lockfile)
conf := p.Get("conf", nil).(*cfg.Config)
force := p.Get("force", true).(bool)
home := p.Get("home", "").(string)
cache := p.Get("cache", false).(bool)
cacheGopath := p.Get("cacheGopath", false).(bool)
useGopath := p.Get("useGopath", false).(bool)
cwd, err := VendorPath(c)
if err != nil {
return false, err
}
// Create a config setup based on the Lockfile data to process with
// existing commands.
newConf := &cfg.Config{}
newConf.Name = conf.Name
newConf.Imports = make(cfg.Dependencies, len(lock.Imports))
for k, v := range lock.Imports {
newConf.Imports[k] = &cfg.Dependency{
Name: v.Name,
Reference: v.Version,
Repository: v.Repository,
VcsType: v.VcsType,
Subpackages: v.Subpackages,
Arch: v.Arch,
Os: v.Os,
}
}
newConf.DevImports = make(cfg.Dependencies, len(lock.DevImports))
for k, v := range lock.DevImports {
newConf.DevImports[k] = &cfg.Dependency{
Name: v.Name,
Reference: v.Version,
Repository: v.Repository,
VcsType: v.VcsType,
Subpackages: v.Subpackages,
Arch: v.Arch,
Os: v.Os,
}
}
newConf.DeDupe()
if len(newConf.Imports) == 0 {
Info("No dependencies found. Nothing installed.\n")
return false, nil
}
// for _, dep := range newConf.Imports {
// if err := VcsUpdate(dep, cwd, home, force, cache, cacheGopath, useGopath); err != nil {
// Warn("Update failed for %s: %s\n", dep.Name, err)
// }
// }
done := make(chan struct{}, concurrentWorkers)
in := make(chan *cfg.Dependency, concurrentWorkers)
var wg sync.WaitGroup
for i := 0; i < concurrentWorkers; i++ {
go func(ch <-chan *cfg.Dependency) {
for {
select {
case dep := <-ch:
if err := VcsUpdate(dep, cwd, home, force, cache, cacheGopath, useGopath); err != nil {
Warn("Update failed for %s: %s\n", dep.Name, err)
}
wg.Done()
case <-done:
return
}
}
}(in)
}
for _, dep := range newConf.Imports {
wg.Add(1)
in <- dep
}
wg.Wait()
// Close goroutines setting the version
for i := 0; i < concurrentWorkers; i++ {
done <- struct{}{}
}
return newConf, nil
}