/
remove.go
81 lines (69 loc) · 1.83 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
package cmd
import (
"os"
"strconv"
"github.com/breadinator/swkshp/errors"
"github.com/breadinator/swkshp/resource"
"github.com/breadinator/swkshp/utils"
"github.com/breadinator/swkshp/versions"
"github.com/spf13/cobra"
)
// removeCmd represents the remove command
var removeCmd = &cobra.Command{
Use: "remove <url>",
Short: "Uninstalls a mod.",
Run: func(cmd *cobra.Command, args []string) {
url := args[0]
game, _ := cmd.Flags().GetString("game")
// checks if the given
id, err := strconv.Atoi(url)
if err != nil {
id, err = utils.WorkshopIDFromURL(url)
if err != nil {
utils.Err(err)
return
}
}
r := resource.ResourceFromURL(url)
// gets game if none provided
if game == "" {
game, err = r.Game()
if err != nil {
utils.Err(err)
return
} else if game == "" {
utils.Err(errors.Wrap(errors.ErrGameUnavailable, "game with ID %d unavailable", id))
utils.Info("You can provide your own game info using the -g flag")
return
}
}
utils.Info("Removing %d...", id)
// checks the database for the entry
ent, err := versions.GetModEntry(game, id)
if err != nil {
utils.Err(err, "Error getting database entry for", strconv.Itoa(id), "in", game)
return
}
// if the directory still exists, remove it
if _, err := os.Stat(ent.Path); !errors.Is(err, os.ErrNotExist) {
err = os.RemoveAll(ent.Path)
if err != nil {
utils.Err(err)
return
}
utils.Info("Deleted mod files")
}
// remove entry from database
if _, err = versions.RemoveModEntry(game, id); err != nil {
utils.Err(err)
return
}
utils.Info("Mod removed from internal database")
utils.Info("Successfully removed %d.", id)
},
Args: cobra.ExactArgs(1),
}
func init() {
rootCmd.AddCommand(removeCmd)
removeCmd.Flags().StringP("game", "g", "", "Specify a game to download for.")
}