forked from Masterminds/glide
/
remove.go
67 lines (57 loc) · 1.65 KB
/
remove.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
package action
import (
"github.com/Masterminds/glide/cfg"
"github.com/Masterminds/glide/msg"
gpath "github.com/Masterminds/glide/path"
"github.com/Masterminds/glide/repo"
)
// Remove removes a dependncy from the configuration.
func Remove(packages []string, inst *repo.Installer) {
base := gpath.Basepath()
EnsureGopath()
EnsureVendorDir()
conf := EnsureConfig()
glidefile, err := gpath.Glide()
if err != nil {
msg.Die("Could not find Glide file: %s", err)
}
msg.Info("Preparing to remove %d packages.", len(packages))
conf.Imports = rmDeps(packages, conf.Imports)
conf.DevImports = rmDeps(packages, conf.DevImports)
// Copy used to generate locks.
confcopy := conf.Clone()
confcopy.Imports = inst.List(confcopy)
if err := repo.SetReference(confcopy); err != nil {
msg.Err("Failed to set references: %s", err)
}
// TODO: Right now, there is no flag to enable this, so this will never be
// run. I am not sure whether we should allow this in a rm op or not.
if inst.UpdateVendored {
repo.VendoredCleanup(confcopy)
}
// Write glide.yaml
if err := conf.WriteFile(glidefile); err != nil {
msg.Die("Failed to write glide YAML file: %s", err)
}
// Write glide lock
writeLock(conf, confcopy, base)
}
// rmDeps returns a list of dependencies that do not contain the given pkgs.
//
// It generates neither an error nor a warning for a pkg that does not exist
// in the list of deps.
func rmDeps(pkgs []string, deps []*cfg.Dependency) []*cfg.Dependency {
res := []*cfg.Dependency{}
for _, d := range deps {
rem := false
for _, p := range pkgs {
if p == d.Name {
rem = true
}
}
if !rem {
res = append(res, d)
}
}
return res
}