From c4cc5e184d11f013f19208bcfebc6ed861173c68 Mon Sep 17 00:00:00 2001 From: jabuxas Date: Fri, 20 Sep 2024 14:53:18 -0300 Subject: [PATCH] refactor: modularize helper functions --- helpers.go | 39 +++++++++++++++++++++++++++++++++++++++ spire.go | 38 ++++++++++++-------------------------- 2 files changed, 51 insertions(+), 26 deletions(-) create mode 100644 helpers.go diff --git a/helpers.go b/helpers.go new file mode 100644 index 0000000..d9feccb --- /dev/null +++ b/helpers.go @@ -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 { + +} diff --git a/spire.go b/spire.go index 6eccce5..5f5a255 100644 --- a/spire.go +++ b/spire.go @@ -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