refactor: modularize helper functions

This commit is contained in:
jabuxas 2024-09-20 14:53:18 -03:00
parent 22f3b6efca
commit c4cc5e184d
2 changed files with 51 additions and 26 deletions

39
helpers.go Normal file
View File

@ -0,0 +1,39 @@
package main
import (
"io"
"log"
"net/http"
"os"
)
func DownloadCache() error {
resp, err := http.Get("https://thunderstore.io/api/v1/package/")
if err != nil {
log.Fatal("couldn't download thunderstore payload")
return err
}
defer resp.Body.Close()
if err := os.MkdirAll(TMPDIR, os.ModePerm); err != nil {
log.Fatal("couldn't create tmpdir")
return err
}
out, err := os.Create(PAYLOAD)
if err != nil {
log.Fatal("couldn't create payload file")
return err
}
defer out.Close()
if _, err := io.Copy(out, resp.Body); err != nil {
log.Fatal("couldn't write to payload file")
return err
}
return nil
}
func DownloadBepinex() error {
}

View File

@ -2,9 +2,6 @@ package main
import (
"fmt"
"io"
"log"
"net/http"
"os"
"time"
)
@ -14,18 +11,21 @@ const (
PAYLOAD = TMPDIR + "/payload.json"
)
var GAME_LOCATION = os.Getenv("GAME_PATH")
func main() {
get_cache()
getCache()
}
func get_cache() error {
func getCache() error {
info, err := os.Stat(PAYLOAD)
if err != nil {
fmt.Println("payload isn't accessible, requesting")
download_cache()
DownloadCache()
} else if !(info.ModTime().After(time.Now().Add(-2 * time.Hour))) {
download_cache()
// download again if payload is older than 2 hours
DownloadCache()
} else {
fmt.Println("payload was updated recently, won't update")
}
@ -33,28 +33,14 @@ func get_cache() error {
return nil
}
func download_cache() error {
resp, err := http.Get("https://thunderstore.io/api/v1/package/")
if err != nil {
log.Fatal("couldn't download thunderstore payload")
return err
}
defer resp.Body.Close()
func installBepinex() error {
_, err := os.Stat(GAME_LOCATION + "/BepInEx")
if err := os.MkdirAll(TMPDIR, os.ModePerm); err != nil {
log.Fatal("couldn't create tmpdir")
return err
}
out, err := os.Create(PAYLOAD)
if err != nil {
log.Fatal("couldn't create payload file")
return err
}
defer out.Close()
// install bepinex
if _, err := io.Copy(out, resp.Body); err != nil {
log.Fatal("couldn't write to payload file")
return err
} else {
// bepinex already installed
}
return nil