This repository has been archived by the owner on Mar 6, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 148
/
restore.go
108 lines (93 loc) · 2.3 KB
/
restore.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
package main
import (
"flag"
"fmt"
"path/filepath"
"sync"
"github.com/constabulary/gb"
"github.com/constabulary/gb/cmd"
"github.com/constabulary/gb/internal/fileutils"
"github.com/constabulary/gb/internal/vendor"
"github.com/pkg/errors"
)
const DefaultJobs = 8
func addRestoreFlags(fs *flag.FlagSet) {
fs.BoolVar(&insecure, "precaire", false, "allow the use of insecure protocols")
fs.IntVar(&jobs, "jobs", DefaultJobs, "maximum amount of restoration jobs occurring at the same time")
}
var (
jobs int
cmdRestore = &cmd.Command{
Name: "restore",
UsageLine: "restore [-precaire]",
Short: "restore dependencies from the manifest",
Long: `Restore vendor dependencies.
Flags:
-precaire
allow the use of insecure protocols.
-jobs N
limit the amount of restoration jobs occurring at the same time.
`,
Run: func(ctx *gb.Context, args []string) error {
return restore(ctx)
},
AddFlags: addRestoreFlags,
}
)
func restore(ctx *gb.Context) error {
m, err := vendor.ReadManifest(manifestFile(ctx))
if err != nil {
return errors.Wrap(err, "could not load manifest")
}
work := make(chan vendor.Dependency)
var wg sync.WaitGroup
workers := min(jobs, len(m.Dependencies))
wg.Add(workers)
errChan := make(chan error, workers)
for i := 0; i < workers; i++ {
go restoreWorker(ctx, work, &wg, errChan)
}
for _, dep := range m.Dependencies {
work <- dep
}
close(work)
wg.Wait()
select {
case err := <-errChan:
return err
default:
return nil
}
}
func restoreWorker(ctx *gb.Context, work chan vendor.Dependency, wg *sync.WaitGroup, errChan chan error) {
defer wg.Done()
for dep := range work {
fmt.Printf("Getting %s\n", dep.Importpath)
repo, _, err := vendor.DeduceRemoteRepo(dep.Importpath, insecure)
if err != nil {
errChan <- errors.Wrap(err, "could not process dependency")
return
}
wc, err := repo.Checkout("", "", dep.Revision)
if err != nil {
errChan <- errors.Wrap(err, "could not retrieve dependency")
return
}
dst := filepath.Join(ctx.Projectdir(), "vendor", "src", dep.Importpath)
src := filepath.Join(wc.Dir(), dep.Path)
if err := fileutils.Copypath(dst, src); err != nil {
errChan <- err
return
}
if err := wc.Destroy(); err != nil {
errChan <- err
return
}
}
}
func min(a, b int) int {
if a < b {
return a
}
return b
}