-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuninstall.go
More file actions
104 lines (89 loc) · 3.1 KB
/
Copy pathuninstall.go
File metadata and controls
104 lines (89 loc) · 3.1 KB
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
package cmd
import (
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"github.com/Bananenpro/cli"
"github.com/adrg/xdg"
"github.com/spf13/cobra"
)
// uninstallCmd represents the uninstall command
var uninstallCmd = &cobra.Command{
Use: "uninstall",
Short: "Uninstall codegame-cli.",
Run: func(cmd *cobra.Command, args []string) {
shallow, err := cmd.Flags().GetBool("shallow")
abort(err)
removeData, err := cmd.Flags().GetBool("remove-data")
abort(err)
yes, err := cli.YesNo("Are you sure you want to uninstall codegame-cli?", false)
abort(err)
if !yes {
cli.Print("Canceled.")
return
}
dataDir := filepath.Join(xdg.DataHome, "codegame")
if removeData {
cli.Print("Removing session files...")
err := os.RemoveAll(filepath.Join(dataDir, "games"))
abortf("Failed to remove session files: %s", err)
}
if !shallow {
cli.Print("Removing installed tools...")
err := os.RemoveAll(filepath.Join(dataDir, "bin"))
abortf("Failed to remove tools: %s", err)
}
if removeData && !shallow {
os.RemoveAll(dataDir)
}
cli.Print("Removing codegame-cli...")
switch runtime.GOOS {
case "windows":
uninstallCLIWindows()
default:
uninstallCLIUnix()
}
cli.Print("Removing cache files...")
os.RemoveAll(filepath.Join(xdg.CacheHome, "codegame", "cli"))
if files, err := os.ReadDir(filepath.Join(xdg.CacheHome, "codegame")); err == nil && len(files) == 0 {
os.RemoveAll(filepath.Join(xdg.CacheHome, "codegame"))
}
cli.Success("Successfully removed codegame-cli from your system!")
},
}
func uninstallCLIWindows() {
homeDir, err := os.UserHomeDir()
abortf("Failed to get the user home directory: %s", err)
homeDir = filepath.Clean(homeDir)
installDir := homeDir + "\\AppData\\Local\\Programs\\codegame-cli"
cli.BeginLoading("Removing codegame-cli from PATH...")
cmd := exec.Command("Powershell.exe", "-Command", "[System.Environment]::SetEnvironmentVariable(\"PATH\", [System.Environment]::GetEnvironmentVariable(\"PATH\",\"USER\") -replace \";"+strings.ReplaceAll(installDir, "\\", "\\\\")+"\",\"USER\")")
cmd.Stderr = os.Stderr
err = cmd.Run()
abortf("Failed to remove codegame-cli from PATH: %s", err)
cli.FinishLoading()
err = os.RemoveAll(installDir)
abortf("Failed to uninstall codegame-cli: %s", err)
}
func uninstallCLIUnix() {
homeDir, err := os.UserHomeDir()
abortf("Failed to get the user home directory: %s", err)
homeDir = filepath.Clean(homeDir)
if _, err := os.Stat("/usr/local/bin/codegame"); !os.IsNotExist(err) {
cmd := exec.Command("bash", "-c", "sudo rm /usr/local/bin/codegame")
cmd.Stderr = os.Stderr
err := cmd.Run()
abortf("Failed to uninstall codegame-cli: %s", err)
}
if _, err := os.Stat(homeDir + "/.local/bin/codegame"); !os.IsNotExist(err) {
err := os.Remove(homeDir + "/.local/bin/codegame")
abortf("Failed to uninstall codegame-cli: %s", err)
}
}
func init() {
rootCmd.AddCommand(uninstallCmd)
uninstallCmd.Flags().BoolP("shallow", "s", false, "Don't remove CodeGame tools installed by codegame-cli, e.g. cg-gen-events, cg-debug.")
uninstallCmd.Flags().BoolP("remove-data", "d", false, "Remove game session files.")
}