-
Notifications
You must be signed in to change notification settings - Fork 1
/
uninstall.go
91 lines (76 loc) · 2.25 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
package commands
import (
"fmt"
"os"
"path"
"github.com/Glow-Project/ppm/pkg/utility"
"github.com/fatih/color"
"github.com/urfave/cli/v2"
)
func uninstall(ctx *cli.Context) error {
paths, config, err := utility.GetPathsAndConfig()
if err != nil {
return err
}
dependencies := ctx.Args()
if len(dependencies.First()) == 0 {
uninstallAllDependencies(&config, paths, ctx.Bool("hard"))
return nil
}
for i := 0; i < dependencies.Len(); i++ {
dep := utility.DependencyFromString(dependencies.Get(i))
if config.HasDependency(dep) && !config.HasSubDependency(dep) {
uninstallDependency(&config, paths, dep, false)
} else if config.HasSubDependency(dep) {
fmt.Println(color.RedString("the plugin"), color.YellowString(dep.Identifier), color.RedString("is a sub dependency and can only be uninstalled by uninstalling its parent"))
} else {
fmt.Println(color.RedString("the plugin"), color.YellowString(dep.Identifier), color.RedString("is not installed"))
}
}
return nil
}
func uninstallAllDependencies(config *utility.PpmConfig, paths utility.Paths, hard bool) error {
loadAnim := utility.StartLoading()
err := os.RemoveAll(paths.Addons)
if err != nil {
return err
}
if hard {
config.RemoveAllDependencies()
}
loadAnim.Stop()
utility.PrintDone()
return nil
}
func uninstallDependency(config *utility.PpmConfig, paths utility.Paths, dependency *utility.Dependency, isSubDependency bool) error {
if !isSubDependency {
fmt.Println("\runinstalling", color.YellowString(dependency.Identifier))
} else {
fmt.Println("\t -> uninstalling", color.YellowString(dependency.Identifier))
}
loadAnim := utility.StartLoading()
subConfig, err := utility.GetPluginConfig(paths.Addons, dependency.Identifier)
if err == nil {
for i := 0; i < len(subConfig.Dependencies); i++ {
subDep := subConfig.Dependencies[i]
if !config.HasDependency(subDep) {
uninstallDependency(config, paths, subDep, true)
}
}
}
// path: root/addons/dependency
err = os.RemoveAll(path.Join(paths.Addons, dependency.Identifier))
loadAnim.Stop()
if err != nil {
return err
}
if !isSubDependency {
config.RemoveDependency(dependency)
} else {
config.RemoveSubDependency(dependency)
}
if !isSubDependency {
utility.PrintDone()
}
return nil
}