Fixed a few bugs, added 'pull' command, optimized API cache, separated the client from the API more or less.
This commit is contained in:
parent
584c6cd90e
commit
ae49350a59
56
api/api.go
56
api/api.go
@ -6,9 +6,6 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"r2mod-go/utils"
|
"r2mod-go/utils"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/fatih/color"
|
|
||||||
// TODO: eliminate this dependency
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Mod contains info about a mod from thunderstore API
|
// Mod contains info about a mod from thunderstore API
|
||||||
@ -44,16 +41,11 @@ type Version struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// CheckAPICache checks if the cached json is expired
|
// CheckAPICache checks if the cached json is expired
|
||||||
// TODO: REWRITE
|
func CheckAPICache() int {
|
||||||
func CheckAPICache() {
|
|
||||||
// TODO: move the printing out, add returns
|
|
||||||
|
|
||||||
color.Blue("> Checking API Cache")
|
|
||||||
|
|
||||||
if !utils.PathExists(utils.SystemInfo.TmpDir + "/pkg.json") {
|
if !utils.PathExists(utils.SystemInfo.TmpDir + "/pkg.json") {
|
||||||
color.Red("> API Cache is outdated")
|
return 1
|
||||||
UpdateAPICache()
|
}
|
||||||
} else {
|
|
||||||
info, err := os.Stat(utils.SystemInfo.TmpDir + "/pkg.json")
|
info, err := os.Stat(utils.SystemInfo.TmpDir + "/pkg.json")
|
||||||
utils.CheckErr(err)
|
utils.CheckErr(err)
|
||||||
|
|
||||||
@ -61,17 +53,13 @@ func CheckAPICache() {
|
|||||||
currentTime := time.Now()
|
currentTime := time.Now()
|
||||||
|
|
||||||
if currentTime.Sub(lastModTime).Seconds() > 7200 {
|
if currentTime.Sub(lastModTime).Seconds() > 7200 {
|
||||||
color.Red("> API Cache is outdated")
|
return 1
|
||||||
color.Blue("> Updating API Cache")
|
|
||||||
UpdateAPICache()
|
|
||||||
} else {
|
|
||||||
color.Green("> API Cache is up to date")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
// UpdateAPICache gets a new copy of the thunderstore pkgfile
|
// UpdateAPICache gets a new copy of the thunderstore pkgfile
|
||||||
// TODO: ARIA2 IMPLEMENTATION
|
|
||||||
func UpdateAPICache() {
|
func UpdateAPICache() {
|
||||||
apiURL := "https://thunderstore.io/api/v1/package/"
|
apiURL := "https://thunderstore.io/api/v1/package/"
|
||||||
|
|
||||||
@ -79,15 +67,12 @@ func UpdateAPICache() {
|
|||||||
utils.CheckErr(err)
|
utils.CheckErr(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetModData gets object of mod by depString
|
func unpackAPI() []Mod {
|
||||||
func GetModData(depString string) Mod {
|
|
||||||
if depString == "R2API" {
|
|
||||||
depString = "tristanmcpherson-R2API"
|
|
||||||
}
|
|
||||||
|
|
||||||
pkgFile, err := os.Open(utils.SystemInfo.TmpDir + "/pkg.json")
|
pkgFile, err := os.Open(utils.SystemInfo.TmpDir + "/pkg.json")
|
||||||
utils.CheckErr(err)
|
utils.CheckErr(err)
|
||||||
|
|
||||||
|
defer pkgFile.Close()
|
||||||
|
|
||||||
byteValue, err := ioutil.ReadAll(pkgFile)
|
byteValue, err := ioutil.ReadAll(pkgFile)
|
||||||
utils.CheckErr(err)
|
utils.CheckErr(err)
|
||||||
|
|
||||||
@ -96,15 +81,24 @@ func GetModData(depString string) Mod {
|
|||||||
err = json.Unmarshal(byteValue, &mods)
|
err = json.Unmarshal(byteValue, &mods)
|
||||||
utils.CheckErr(err)
|
utils.CheckErr(err)
|
||||||
|
|
||||||
|
return mods
|
||||||
|
}
|
||||||
|
|
||||||
|
// PkgList contains all mods in current package cache
|
||||||
|
var PkgList []Mod = unpackAPI()
|
||||||
|
|
||||||
|
// GetModData gets object of mod by depString
|
||||||
|
func GetModData(depString string) Mod {
|
||||||
|
if depString == "R2API" {
|
||||||
|
depString = "tristanmcpherson-R2API"
|
||||||
|
}
|
||||||
|
|
||||||
var selectedMod Mod
|
var selectedMod Mod
|
||||||
for i := 0; i < len(mods); i++ {
|
for i := 0; i < len(PkgList); i++ {
|
||||||
if mods[i].FullName == depString {
|
if PkgList[i].FullName == depString {
|
||||||
selectedMod = mods[i]
|
selectedMod = PkgList[i]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
defer pkgFile.Close()
|
|
||||||
|
|
||||||
// TODO: export this lol
|
|
||||||
return selectedMod
|
return selectedMod
|
||||||
}
|
}
|
||||||
|
5
go.mod
5
go.mod
@ -2,4 +2,7 @@ module r2mod-go
|
|||||||
|
|
||||||
go 1.15
|
go 1.15
|
||||||
|
|
||||||
require github.com/fatih/color v1.10.0
|
require (
|
||||||
|
github.com/fatih/color v1.10.0
|
||||||
|
github.com/ynsgnr/aria2go v0.0.0-20190317155935-c48e59783821
|
||||||
|
)
|
||||||
|
2
go.sum
2
go.sum
@ -4,6 +4,8 @@ github.com/mattn/go-colorable v0.1.8 h1:c1ghPdyEDarC70ftn0y+A/Ee++9zz8ljHG1b13eJ
|
|||||||
github.com/mattn/go-colorable v0.1.8/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
|
github.com/mattn/go-colorable v0.1.8/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
|
||||||
github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
|
github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
|
||||||
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
|
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
|
||||||
|
github.com/ynsgnr/aria2go v0.0.0-20190317155935-c48e59783821 h1:u7Ccf6Q8RHS5NRtaedudURcUt33+jieRhvi0HgHzqu0=
|
||||||
|
github.com/ynsgnr/aria2go v0.0.0-20190317155935-c48e59783821/go.mod h1:GIZQz0e6PpgUM6AxkEFlTum/IIXQAO2YsGcj5Aer58c=
|
||||||
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae h1:/WDfKMnPU+m5M4xB+6x4kaepxRw6jWvR5iDRdvjHgy8=
|
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae h1:/WDfKMnPU+m5M4xB+6x4kaepxRw6jWvR5iDRdvjHgy8=
|
||||||
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
|
55
main.go
55
main.go
@ -43,6 +43,7 @@ func main() {
|
|||||||
> list, ls, li -- list mods
|
> list, ls, li -- list mods
|
||||||
> update, upgrade, up -- update mods and API cache
|
> update, upgrade, up -- update mods and API cache
|
||||||
> toggle, tm, togglemods -- toggle mods
|
> toggle, tm, togglemods -- toggle mods
|
||||||
|
> pull -- forcefully update API cache, not mods
|
||||||
> filter -- remove version numbers using regex from stdin
|
> filter -- remove version numbers using regex from stdin
|
||||||
`)
|
`)
|
||||||
case "info":
|
case "info":
|
||||||
@ -59,14 +60,26 @@ func main() {
|
|||||||
case "install", "ins", "i":
|
case "install", "ins", "i":
|
||||||
api.CheckAPICache()
|
api.CheckAPICache()
|
||||||
for _, m := range os.Args[2:] {
|
for _, m := range os.Args[2:] {
|
||||||
|
|
||||||
fmt.Println(" >", tools.ExposeModString(m))
|
fmt.Println(" >", tools.ExposeModString(m))
|
||||||
tools.DownloadMod(tools.ExposeModString(m))
|
stat, ov := tools.DownloadMod(tools.ExposeModString(m))
|
||||||
|
if stat == 0 {
|
||||||
|
color.Green(" > Already installed and up to date")
|
||||||
|
} else if stat == 1 {
|
||||||
|
color.Magenta(" > Updating to version %s", ov)
|
||||||
|
} else {
|
||||||
|
color.Blue(" > Downloaded")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
case "remove", "rem", "r":
|
case "remove", "rem", "r":
|
||||||
api.CheckAPICache()
|
|
||||||
for _, m := range os.Args[2:] {
|
for _, m := range os.Args[2:] {
|
||||||
fmt.Println(" >", tools.ExposeModString(m))
|
fmt.Println(" >", tools.ExposeModString(m))
|
||||||
tools.RemoveMod(tools.ExposeModString(m))
|
|
||||||
|
if tools.RemoveMod(tools.ExposeModString(m)) == 1 {
|
||||||
|
color.Red(" > Not installed")
|
||||||
|
} else {
|
||||||
|
color.Blue(" > Uninstalled")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
case "list", "ls", "li":
|
case "list", "ls", "li":
|
||||||
files, err := ioutil.ReadDir(utils.SystemInfo.PluginDir)
|
files, err := ioutil.ReadDir(utils.SystemInfo.PluginDir)
|
||||||
@ -78,8 +91,14 @@ func main() {
|
|||||||
color.Blue("%3v %s", i, white(f.Name()))
|
color.Blue("%3v %s", i, white(f.Name()))
|
||||||
}
|
}
|
||||||
case "update", "upgrade", "up":
|
case "update", "upgrade", "up":
|
||||||
color.Cyan("> Updating")
|
color.Cyan("> Checking API Cache")
|
||||||
api.CheckAPICache()
|
if api.CheckAPICache() == 0 {
|
||||||
|
color.Green("> API Cache is up to date!")
|
||||||
|
} else {
|
||||||
|
color.Red("> API Cache out of date!")
|
||||||
|
api.UpdateAPICache()
|
||||||
|
}
|
||||||
|
|
||||||
files, err := ioutil.ReadDir(utils.SystemInfo.PluginDir)
|
files, err := ioutil.ReadDir(utils.SystemInfo.PluginDir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
@ -87,12 +106,32 @@ func main() {
|
|||||||
|
|
||||||
for _, f := range files {
|
for _, f := range files {
|
||||||
fmt.Println(" >", tools.ExposeModString(f.Name()))
|
fmt.Println(" >", tools.ExposeModString(f.Name()))
|
||||||
tools.DownloadMod(tools.ExposeModString(f.Name()))
|
stat, ov := tools.DownloadMod(tools.ExposeModString(f.Name()))
|
||||||
|
if stat == 0 {
|
||||||
|
color.Green(" > Already installed and up to date")
|
||||||
|
} else if stat == 1 {
|
||||||
|
color.Magenta(" > Updating to version %s", ov)
|
||||||
|
} else {
|
||||||
|
color.Blue(" > Downloaded")
|
||||||
}
|
}
|
||||||
|
|
||||||
color.Green("> Complete!")
|
}
|
||||||
|
|
||||||
|
// color.Green("> Complete!")
|
||||||
|
case "pull":
|
||||||
|
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":
|
case "toggle", "tm", "togglemods":
|
||||||
tools.ToggleMods()
|
if tools.ToggleMods() {
|
||||||
|
color.Cyan("> Mods Enabled")
|
||||||
|
} else {
|
||||||
|
color.Cyan("> Mods Enabled")
|
||||||
|
}
|
||||||
case "filter":
|
case "filter":
|
||||||
fmt.Println(tools.ExposeModString(os.Args[2]))
|
fmt.Println(tools.ExposeModString(os.Args[2]))
|
||||||
default:
|
default:
|
||||||
|
@ -8,8 +8,6 @@ import (
|
|||||||
"r2mod-go/utils"
|
"r2mod-go/utils"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/fatih/color"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// ExposeModString removes user-error surrounding the mod string.
|
// ExposeModString removes user-error surrounding the mod string.
|
||||||
@ -20,7 +18,7 @@ func ExposeModString(input string) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// DownloadMod gets the download URL and installs the mod in the correct folder
|
// DownloadMod gets the download URL and installs the mod in the correct folder
|
||||||
func DownloadMod(depString string) {
|
func DownloadMod(depString string) (int, string) {
|
||||||
if depString == "R2API" {
|
if depString == "R2API" {
|
||||||
depString = "tristanmcpherson-R2API"
|
depString = "tristanmcpherson-R2API"
|
||||||
}
|
}
|
||||||
@ -34,47 +32,44 @@ func DownloadMod(depString string) {
|
|||||||
modFolder := sysinfo.PluginDir + "/" + depString + "-" + modVersion
|
modFolder := sysinfo.PluginDir + "/" + depString + "-" + modVersion
|
||||||
|
|
||||||
if utils.PathExists(modFolder) {
|
if utils.PathExists(modFolder) {
|
||||||
color.Green(" > Already installed and up to date")
|
return 0, ""
|
||||||
} else {
|
}
|
||||||
|
|
||||||
if len(api.GetModData(depString).Versions) >= 2 {
|
if len(api.GetModData(depString).Versions) >= 2 {
|
||||||
modVersionOld := api.GetModData(depString).Versions[1].VersionNumber
|
modVersionOld := api.GetModData(depString).Versions[1].VersionNumber
|
||||||
modFolderOld := sysinfo.PluginDir + "/" + depString + "-" + modVersionOld
|
modFolderOld := sysinfo.PluginDir + "/" + depString + "-" + modVersionOld
|
||||||
|
|
||||||
if utils.PathExists(modFolderOld) {
|
if utils.PathExists(modFolderOld) {
|
||||||
color.Magenta(" > Updating to version %s", modVersion)
|
|
||||||
defer os.RemoveAll(modFolderOld)
|
defer os.RemoveAll(modFolderOld)
|
||||||
|
return 1, modVersion
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
color.Blue(" > Downloading...")
|
|
||||||
|
|
||||||
err := utils.DownloadFile(modName, downloadURL)
|
err := utils.DownloadFile(modName, downloadURL)
|
||||||
utils.CheckErr(err)
|
utils.CheckErr(err)
|
||||||
|
|
||||||
color.Blue(" > Unzipping")
|
|
||||||
|
|
||||||
unzip, err := utils.Unzip(modName, modFolder)
|
unzip, err := utils.Unzip(modName, modFolder)
|
||||||
log.Println(unzip)
|
log.Println(unzip)
|
||||||
utils.CheckErr(err)
|
utils.CheckErr(err)
|
||||||
}
|
|
||||||
|
return 2, modVersion
|
||||||
}
|
}
|
||||||
|
|
||||||
// RemoveMod uninstalls a mod
|
// RemoveMod uninstalls a mod
|
||||||
func RemoveMod(depString string) {
|
func RemoveMod(depString string) int {
|
||||||
modVersion := api.GetModData(depString).Versions[0].VersionNumber
|
modVersion := api.GetModData(depString).Versions[0].VersionNumber
|
||||||
modFolder := utils.SystemInfo.PluginDir + "/" + depString + "-" + modVersion
|
modFolder := utils.SystemInfo.PluginDir + "/" + depString + "-" + modVersion
|
||||||
|
|
||||||
if !utils.PathExists(modFolder) {
|
if !utils.PathExists(modFolder) {
|
||||||
color.Red(" > Not installed")
|
return 1
|
||||||
} else {
|
|
||||||
defer os.RemoveAll(modFolder)
|
|
||||||
color.Blue(" > Uninstalled")
|
|
||||||
}
|
}
|
||||||
|
defer os.RemoveAll(modFolder)
|
||||||
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
// ToggleMods toggles mods on or off.
|
// ToggleMods toggles mods on or off.
|
||||||
// TODO: make this not hardcoded as shit, perhaps implement a proper parser
|
// TODO: make this not hardcoded as shit, perhaps implement a proper parser
|
||||||
func ToggleMods() {
|
func ToggleMods() bool {
|
||||||
var configFile string = utils.SystemInfo.GameDir + "/doorstop_config.ini"
|
var configFile string = utils.SystemInfo.GameDir + "/doorstop_config.ini"
|
||||||
|
|
||||||
input, err := ioutil.ReadFile(configFile)
|
input, err := ioutil.ReadFile(configFile)
|
||||||
@ -82,15 +77,19 @@ func ToggleMods() {
|
|||||||
|
|
||||||
lines := strings.Split(string(input), "\n")
|
lines := strings.Split(string(input), "\n")
|
||||||
|
|
||||||
|
var status bool
|
||||||
|
|
||||||
if strings.Contains(lines[2], "true") {
|
if strings.Contains(lines[2], "true") {
|
||||||
color.Cyan("> Mods Disabled")
|
status = false
|
||||||
lines[2] = "enabled=false"
|
lines[2] = "enabled=false"
|
||||||
} else {
|
} else {
|
||||||
color.Cyan("> Mods Enabled")
|
status = true
|
||||||
lines[2] = "enabled=true"
|
lines[2] = "enabled=true"
|
||||||
}
|
}
|
||||||
|
|
||||||
output := strings.Join(lines, "\n")
|
output := strings.Join(lines, "\n")
|
||||||
err = ioutil.WriteFile(configFile, []byte(output), 0644)
|
err = ioutil.WriteFile(configFile, []byte(output), 0644)
|
||||||
utils.CheckErr(err)
|
utils.CheckErr(err)
|
||||||
|
|
||||||
|
return status
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user