-
Notifications
You must be signed in to change notification settings - Fork 0
/
uninstall.go
106 lines (100 loc) · 2.02 KB
/
uninstall.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
package main
import (
"context"
"errors"
"flag"
"fmt"
"os"
"path/filepath"
"runtime"
)
func uninstallCmd(fs *flag.FlagSet, args []string) error {
var root string
var goos string
var goarch string
var clean bool
fs.StringVar(&root, "root", envGoupRoot(), "root dir to install")
fs.StringVar(&goos, "goos", runtime.GOOS, "OS for go to install")
fs.StringVar(&goarch, "goarch", runtime.GOARCH, "ARCH for go to install")
fs.BoolVar(&clean, "clean", false, "clean distfiles")
if err := fs.Parse(args); err != nil {
return err
}
if root == "" {
fs.PrintDefaults()
return errors.New("required -root")
}
versions := fs.Args()
if len(versions) == 0 {
fs.PrintDefaults()
return errors.New("no versions to install")
}
ctx := context.Background()
uni := uninstaller{
rootdir: root,
goos: goos,
goarch: goarch,
clean: clean,
}
for _, ver := range versions {
err := uni.uninstall(ctx, ver)
if err != nil {
return err
}
}
return nil
}
type uninstaller struct {
rootdir string
goos string
goarch string
clean bool
}
func (uni uninstaller) uninstall(ctx context.Context, ver string) error {
var deleted bool
name := fmt.Sprintf("%s.%s-%s", ver, uni.goos, uni.goarch)
dir := filepath.Join(uni.rootdir, name)
ok, err := uni.hasDir(dir)
if err != nil {
return err
}
if ok {
err := os.RemoveAll(dir)
if err != nil {
return err
}
deleted = true
}
// remove distributed files.
if uni.clean {
pat := filepath.Join(uni.rootdir, "dl", name+".*")
paths, err := filepath.Glob(pat)
if err != nil {
return err
}
for _, p := range paths {
err := os.Remove(p)
if err != nil {
return err
}
deleted = true
}
}
if !deleted {
return fmt.Errorf("no deleted files for %s", name)
}
return nil
}
func (uni uninstaller) hasDir(name string) (bool, error) {
fi, err := os.Lstat(name)
if err != nil {
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
if !fi.IsDir() {
return false, fmt.Errorf("not directory: %s", name)
}
return true, nil
}