forked from koding/kite
/
uninstall.go
101 lines (84 loc) · 1.84 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
package command
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/koding/kite/kitekey"
"github.com/mitchellh/cli"
)
type Uninstall struct {
Ui cli.Ui
}
func NewUninstall() cli.CommandFactory {
return func() (cli.Command, error) {
return &Uninstall{
Ui: DefaultUi,
}, nil
}
}
func (c *Uninstall) Synopsis() string {
return "Uninstalls a kite"
}
func (c *Uninstall) Help() string {
helpText := `
Usage: kitectl uninstall kitename
Uninstall the given kite. Example kitename: github.com/koding/fs.kite/1.0.0
`
return strings.TrimSpace(helpText)
}
func (c *Uninstall) Run(args []string) int {
if len(args) != 1 {
c.Ui.Output(c.Help())
return 1
}
fullName := args[0]
installed, err := isInstalled(fullName)
if err != nil {
c.Ui.Error(err.Error())
return 1
}
if !installed {
c.Ui.Error(fmt.Sprintf("%s is not installed", fullName))
return 1
}
bundlePath, err := getBundlePath(fullName)
if err != nil {
c.Ui.Error(err.Error())
return 1
}
err = os.RemoveAll(bundlePath)
if err != nil {
c.Ui.Error(err.Error())
return 1
}
return 0
}
// getBundlePath returns the bundle path of a given kite.
// Example: "adsf-1.2.3" -> "~/.kd/kites/asdf-1.2.3.kite"
func getBundlePath(fullKiteName string) (string, error) {
kiteHome, err := kitekey.KiteHome()
if err != nil {
return "", err
}
return filepath.Join(kiteHome, "kites", fullKiteName), nil
}
// isInstalled returns true if the kite is installed.
func isInstalled(fullKiteName string) (bool, error) {
bundlePath, err := getBundlePath(fullKiteName)
if err != nil {
return false, err
}
return exists(bundlePath)
}
// exists returns whether the given file or directory exists or not.
func exists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return false, err
}