|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "os/exec" |
| 6 | + "path/filepath" |
| 7 | + "runtime" |
| 8 | + "strings" |
| 9 | + |
| 10 | + "github.com/Bananenpro/cli" |
| 11 | + "github.com/adrg/xdg" |
| 12 | + "github.com/spf13/cobra" |
| 13 | +) |
| 14 | + |
| 15 | +// uninstallCmd represents the uninstall command |
| 16 | +var uninstallCmd = &cobra.Command{ |
| 17 | + Use: "uninstall", |
| 18 | + Short: "Uninstall codegame-cli.", |
| 19 | + Run: func(cmd *cobra.Command, args []string) { |
| 20 | + shallow, err := cmd.Flags().GetBool("shallow") |
| 21 | + abort(err) |
| 22 | + removeData, err := cmd.Flags().GetBool("remove-data") |
| 23 | + abort(err) |
| 24 | + |
| 25 | + yes, err := cli.YesNo("Are you sure you want to uninstall codegame-cli?", false) |
| 26 | + abort(err) |
| 27 | + if !yes { |
| 28 | + cli.Print("Canceled.") |
| 29 | + return |
| 30 | + } |
| 31 | + |
| 32 | + dataDir := filepath.Join(xdg.DataHome, "codegame") |
| 33 | + if removeData { |
| 34 | + cli.Print("Removing session files...") |
| 35 | + err := os.RemoveAll(filepath.Join(dataDir, "games")) |
| 36 | + abortf("Failed to remove session files: %s", err) |
| 37 | + } |
| 38 | + if !shallow { |
| 39 | + cli.Print("Removing installed tools...") |
| 40 | + err := os.RemoveAll(filepath.Join(dataDir, "bin")) |
| 41 | + abortf("Failed to remove tools: %s", err) |
| 42 | + } |
| 43 | + if removeData && !shallow { |
| 44 | + os.RemoveAll(dataDir) |
| 45 | + } |
| 46 | + |
| 47 | + cli.Print("Removing codegame-cli...") |
| 48 | + switch runtime.GOOS { |
| 49 | + case "windows": |
| 50 | + uninstallCLIWindows() |
| 51 | + default: |
| 52 | + uninstallCLIUnix() |
| 53 | + } |
| 54 | + |
| 55 | + cli.Print("Removing cache files...") |
| 56 | + os.RemoveAll(filepath.Join(xdg.CacheHome, "codegame", "cli")) |
| 57 | + if files, err := os.ReadDir(filepath.Join(xdg.CacheHome, "codegame")); err == nil && len(files) == 0 { |
| 58 | + os.RemoveAll(filepath.Join(xdg.CacheHome, "codegame")) |
| 59 | + } |
| 60 | + |
| 61 | + cli.Success("Successfully removed codegame-cli from your system!") |
| 62 | + }, |
| 63 | +} |
| 64 | + |
| 65 | +func uninstallCLIWindows() { |
| 66 | + homeDir, err := os.UserHomeDir() |
| 67 | + abortf("Failed to get the user home directory: %s", err) |
| 68 | + homeDir = filepath.Clean(homeDir) |
| 69 | + |
| 70 | + installDir := homeDir + "\\AppData\\Local\\Programs\\codegame-cli" |
| 71 | + |
| 72 | + cli.BeginLoading("Removing codegame-cli from PATH...") |
| 73 | + cmd := exec.Command("Powershell.exe", "-Command", "[System.Environment]::SetEnvironmentVariable(\"PATH\", [System.Environment]::GetEnvironmentVariable(\"PATH\",\"USER\") -replace \";"+strings.ReplaceAll(installDir, "\\", "\\\\")+"\",\"USER\")") |
| 74 | + cmd.Stderr = os.Stderr |
| 75 | + err = cmd.Run() |
| 76 | + abortf("Failed to remove codegame-cli from PATH: %s", err) |
| 77 | + cli.FinishLoading() |
| 78 | + |
| 79 | + err = os.RemoveAll(installDir) |
| 80 | + abortf("Failed to uninstall codegame-cli: %s", err) |
| 81 | +} |
| 82 | + |
| 83 | +func uninstallCLIUnix() { |
| 84 | + homeDir, err := os.UserHomeDir() |
| 85 | + abortf("Failed to get the user home directory: %s", err) |
| 86 | + homeDir = filepath.Clean(homeDir) |
| 87 | + |
| 88 | + if _, err := os.Stat("/usr/local/bin/codegame"); !os.IsNotExist(err) { |
| 89 | + cmd := exec.Command("bash", "-c", "sudo rm /usr/local/bin/codegame") |
| 90 | + cmd.Stderr = os.Stderr |
| 91 | + err := cmd.Run() |
| 92 | + abortf("Failed to uninstall codegame-cli: %s", err) |
| 93 | + } |
| 94 | + if _, err := os.Stat(homeDir + "/.local/bin/codegame"); !os.IsNotExist(err) { |
| 95 | + err := os.Remove(homeDir + "/.local/bin/codegame") |
| 96 | + abortf("Failed to uninstall codegame-cli: %s", err) |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +func init() { |
| 101 | + rootCmd.AddCommand(uninstallCmd) |
| 102 | + uninstallCmd.Flags().BoolP("shallow", "s", false, "Don't remove CodeGame tools installed by codegame-cli, e.g. cg-gen-events, cg-debug.") |
| 103 | + uninstallCmd.Flags().BoolP("remove-data", "d", false, "Remove game session files.") |
| 104 | +} |
0 commit comments