-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd_remove.go
69 lines (55 loc) · 1.33 KB
/
cmd_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
68
69
package main
import (
"os"
"path/filepath"
"github.com/spf13/cobra"
)
// ModuleWalkFunc is called by walkOurSymlinks for each directory found to be a module.
type ModuleWalkFunc func(filename, target string, fi os.FileInfo, err error) error
var cmdRemove = &cobra.Command{
Use: "remove",
Short: "unistall modules",
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
args = append(args, "")
}
for i := range args {
args[i] = filepath.Join(globalOpts.Base, args[i])
}
v("removing base dirs %v\n", args)
walkOurSymlinks(globalOpts.Base, globalOpts.Target, func(filename, target string, fi os.FileInfo, err error) error {
if err != nil {
return err
}
for _, dir := range args {
if isSubdir(dir, target) {
if !globalOpts.DryRun {
ok(os.Remove(filename))
}
v("removed %v\n", filename)
break
}
}
return nil
})
},
}
func init() {
cmdRoot.AddCommand(cmdRemove)
}
func walkOurSymlinks(base, target string, fn ModuleWalkFunc) {
ok(filepath.Walk(target, func(filename string, fi os.FileInfo, err error) error {
if err != nil {
v("error checking %v: %v\n", filename, err)
return nil
}
if !isSymlink(fi) {
return err
}
target := readlink(filename)
if !isSubdir(base, target) {
return err
}
return fn(filename, target, fi, err)
}))
}