/
remove.go
89 lines (77 loc) · 2.06 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package cmd
import (
"fmt"
"os"
"path/filepath"
"runtime"
"strings"
"github.com/nao1215/gorky/file"
"github.com/nao1215/gup/internal/goutil"
"github.com/nao1215/gup/internal/print"
"github.com/spf13/cobra"
)
func newRemoveCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "remove",
Aliases: []string{"rm"},
Short: "Remove the binary under $GOPATH/bin or $GOBIN",
Long: `Remove command in $GOPATH/bin or $GOBIN.
If you want to specify multiple binaries at once, separate them with space.
[e.g.] gup remove a_cmd b_cmd c_cmd`,
Args: cobra.MinimumNArgs(1),
ValidArgsFunction: completePathBinaries,
Run: func(cmd *cobra.Command, args []string) {
OsExit(remove(cmd, args))
},
}
cmd.Flags().BoolP("force", "f", false, "Forcibly remove the file")
return cmd
}
func remove(cmd *cobra.Command, args []string) int {
if len(args) == 0 {
print.Err("no command name specified")
return 1
}
force, err := cmd.Flags().GetBool("force")
if err != nil {
print.Err(fmt.Errorf("%s: %w", "can not parse command line argument (--force)", err))
return 1
}
gobin, err := goutil.GoBin()
if err != nil {
print.Err(err)
return 1
}
return removeLoop(gobin, force, args)
}
// GOOS is wrapper for runtime.GOOS variable. It's for unit test.
var GOOS = runtime.GOOS
func removeLoop(gobin string, force bool, target []string) int {
result := 0
for _, v := range target {
// In Windows, $GOEXE is set to the ".exe" extension.
// The user-specified command name (arguments) may not have an extension.
execSuffix := os.Getenv("GOEXE")
if GOOS == "windows" && !strings.HasSuffix(v, execSuffix) {
v += execSuffix
}
target := filepath.Join(gobin, v)
if !file.IsFile(target) {
print.Err(fmt.Errorf("no such file or directory: %s", target))
result = 1
continue
}
if !force {
if !print.Question(fmt.Sprintf("remove %s?", target)) {
print.Info("cancel removal " + target)
continue
}
}
if err := os.Remove(target); err != nil {
print.Err(err)
continue
}
print.Info("removed " + target)
}
return result
}