forked from FiloSottile/gvt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
delete.go
76 lines (63 loc) · 1.95 KB
/
delete.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
package main
import (
"flag"
"fmt"
"path/filepath"
"github.com/nota/gvt/fileutils"
"github.com/nota/gvt/gbvendor"
)
var (
deleteAll bool // delete all dependencies
)
func addDeleteFlags(fs *flag.FlagSet) {
fs.BoolVar(&deleteAll, "all", false, "delete all dependencies")
}
var cmdDelete = &Command{
Name: "delete",
UsageLine: "delete [-all] importpath",
Short: "delete a local dependency",
Long: `delete removes a dependency from the vendor directory and the manifest
Flags:
-all
remove all dependencies
`,
Run: func(args []string) error {
if len(args) != 1 && !deleteAll {
return fmt.Errorf("delete: import path or --all flag is missing")
} else if len(args) == 1 && deleteAll {
return fmt.Errorf("delete: you cannot specify path and --all flag at once")
}
m, err := vendor.ReadManifest(manifestFile)
if err != nil {
return fmt.Errorf("could not load manifest: %v", err)
}
var dependencies []vendor.Dependency
if deleteAll {
dependencies = make([]vendor.Dependency, len(m.Dependencies))
copy(dependencies, m.Dependencies)
} else {
p := args[0]
dependency, err := m.GetDependencyForImportpath(p)
if err != nil {
return fmt.Errorf("could not get dependency: %v", err)
}
if p != dependency.Importpath {
return fmt.Errorf("a parent of the specified dependency is vendored, remove that instead: %v",
dependency.Importpath)
}
dependencies = append(dependencies, dependency)
}
for _, d := range dependencies {
path := d.Importpath
if err := m.RemoveDependency(d); err != nil {
return fmt.Errorf("dependency could not be deleted: %v", err)
}
if err := fileutils.RemoveAll(filepath.Join(vendorDir, filepath.FromSlash(path))); err != nil {
// TODO(dfc) need to apply vendor.cleanpath here to remove indermediate directories.
return fmt.Errorf("dependency could not be deleted: %v", err)
}
}
return vendor.WriteManifest(manifestFile, m)
},
AddFlags: addDeleteFlags,
}