From 3933f845359c78e261f5f0d5dc039bafd85e3849 Mon Sep 17 00:00:00 2001 From: rainyuu Date: Sat, 30 Jan 2021 21:02:49 +0100 Subject: [PATCH] Added check if mod exists --- api/api.go | 6 +++--- cli/cli.go | 16 +++++++++++----- main.go | 23 +++++++++++++++-------- tools/tools.go | 20 +++++++++++++++----- 4 files changed, 44 insertions(+), 21 deletions(-) diff --git a/api/api.go b/api/api.go index 63112fe..7abf83e 100644 --- a/api/api.go +++ b/api/api.go @@ -113,16 +113,16 @@ func SearchMods(inp string) fuzzy.Ranks { } // GetModData gets object of mod by depString -func GetModData(depString string) Mod { +func GetModData(depString string) (int, Mod) { if depString == "R2API" { depString = "tristanmcpherson-R2API" } for i := 0; i < len(PkgList); i++ { if PkgList[i].FullName == depString { - return PkgList[i] + return 0, PkgList[i] } } - panic("Mod not found") + return 1, Mod{} } diff --git a/cli/cli.go b/cli/cli.go index d08ea41..d4219f9 100644 --- a/cli/cli.go +++ b/cli/cli.go @@ -15,22 +15,28 @@ func InstallMod(pkg string) { green := color.New(color.FgGreen).SprintFunc() magenta := color.New(color.FgMagenta).SprintFunc() + red := color.New(color.FgRed).SprintFunc() blue := color.New(color.FgBlue).SprintFunc() if stat == 1 { buffer += green("up to date!") } else if stat == 2 { buffer += fmt.Sprintf("%s %s", magenta("updating to version"), ov) + } else if stat == 3 { + buffer += fmt.Sprintf("%s", red("could not find the mod specified.")) } else { buffer += blue("downloaded") } - mod := api.GetModData(tools.ExposeModString(pkg)) - for _, dep := range mod.Versions[0].Dependencies { - depString := tools.ExposeModString(dep) - if depString != "tristanmcpherson-R2API" && depString != "bbepis-BepInExPack" { - InstallMod(depString) + if stat != 3 { + _, mod := api.GetModData(tools.ExposeModString(pkg)) + for _, dep := range mod.Versions[0].Dependencies { + depString := tools.ExposeModString(dep) + if depString != "tristanmcpherson-R2API" && depString != "bbepis-BepInExPack" { + InstallMod(depString) + } } + } fmt.Println(buffer) diff --git a/main.go b/main.go index 95ad93d..19d6adb 100644 --- a/main.go +++ b/main.go @@ -55,9 +55,12 @@ func main() { case "info": api.InitAPI() if len(os.Args) <= 2 { - fmt.Printf("Usage: %s info \n", os.Args[0]) + fmt.Println("Usage: r2go info ") } else { - var selectedmod api.Mod = api.GetModData(os.Args[2]) + status, selectedmod := api.GetModData(os.Args[2]) + if status != 0 { + fmt.Println("> Could not find the mod specified.") + } color.Cyan("Mod Info: %s", os.Args[2]) fmt.Println(" Name: " + selectedmod.Versions[0].FullName) fmt.Println(" Desc: " + selectedmod.Versions[0].Description) @@ -106,9 +109,14 @@ func main() { color.Green("> Complete!") case "pull": - color.Red("> Forcefully updating API Cache") api.InitAPI() - api.UpdateAPICache() + color.Cyan("> Checking API Cache") + if api.CheckAPICache() == 0 { + color.Green("> API Cache is up to date!") + } else { + color.Red("> API Cache out of date!") + api.UpdateAPICache() + } case "toggle", "tm", "togglemods": if tools.ToggleMods() { color.Cyan("> Mods Enabled") @@ -118,9 +126,8 @@ func main() { case "filter": fmt.Println(tools.ExposeModString(os.Args[2])) case "version", "ver", "v": - color.Magenta("> r2go") - fmt.Println(">", version) - fmt.Println("> git.cya.cx/endigma/r2mod-go") + color.Magenta("> r2go", version) + fmt.Println(" > github.com/endigma442") case "search", "s", "find": api.InitAPI() @@ -134,6 +141,6 @@ func main() { fmt.Println(" -", mod.Target) } default: - fmt.Printf("Unknown command, use '%s help' for a list of commands.\n", os.Args[0]) + fmt.Println("Unknown command, use 'r2go help' for a list of commands.") } } diff --git a/tools/tools.go b/tools/tools.go index 1ef1a84..8a148f3 100644 --- a/tools/tools.go +++ b/tools/tools.go @@ -19,6 +19,8 @@ func ExposeModString(input string) string { } // DownloadMod gets the download URL and installs the mod in the correct folder +// DownloadMod returns 0 if the download is sucessful, and 1 if it is already installed +// DownlaodMod returns 2 if the mod updated, and 3 if the mod could not be found. func DownloadMod(depString string) (int, string) { var status int var modver string @@ -28,7 +30,10 @@ func DownloadMod(depString string) (int, string) { } sysinfo := utils.GetSysInfo() - mod := api.GetModData(depString) + status, mod := api.GetModData(depString) + if status != 0 { + return 3, "not_found" + } downloadURL := mod.Versions[0].DownloadURL modName := sysinfo.ProgDir + "/dl/" + depString + ".zip" @@ -37,10 +42,12 @@ func DownloadMod(depString string) (int, string) { modFolder := sysinfo.PluginDir + "/" + depString + "-" + modVersion if utils.PathExists(modFolder) { + return 1, modver } - if len(api.GetModData(depString).Versions) >= 2 { + status, mod = api.GetModData(depString) + if len(mod.Versions) >= 2 { modVersionOld := mod.Versions[1].VersionNumber modFolderOld := sysinfo.PluginDir + "/" + depString + "-" + modVersionOld @@ -49,7 +56,7 @@ func DownloadMod(depString string) (int, string) { status = 2 modver = modVersion } else { - status = 3 + status = 0 } } @@ -71,8 +78,11 @@ func DownloadMod(depString string) (int, string) { // RemoveMod uninstalls a mod func RemoveMod(depString string) int { - modVersion := api.GetModData(depString).Versions[0].VersionNumber - modFolder := utils.SystemInfo.PluginDir + "/" + depString + "-" + modVersion + status, mod := api.GetModData(depString) + if status != 0 { + fmt.Println("> Could not find the mod specified.") + } + modFolder := utils.SystemInfo.PluginDir + "/" + depString + "-" + mod.Versions[0].VersionNumber if !utils.PathExists(modFolder) { return 1