feat: sanitize input before passing it around

This commit is contained in:
jabuxas 2024-09-20 15:19:25 -03:00
parent 908e6b656c
commit 65ee980827
2 changed files with 19 additions and 4 deletions

View File

@ -7,6 +7,7 @@ import (
"net/http" "net/http"
"os" "os"
"path/filepath" "path/filepath"
"strings"
) )
func DownloadCache() error { func DownloadCache() error {
@ -51,7 +52,7 @@ func DownloadBepinex() error {
return fmt.Errorf("could not save file: %w", err) return fmt.Errorf("could not save file: %w", err)
} }
err = unzip(tmpPath, GAME_LOCATION) err = unzip(tmpPath, GAME_PATH)
if err != nil { if err != nil {
return fmt.Errorf("could not extract BepInEx: %w", err) return fmt.Errorf("could not extract BepInEx: %w", err)
} }
@ -100,3 +101,13 @@ func unzip(src, dest string) error {
} }
return nil return nil
} }
func SanitizeInput(input *string) error {
if *input == "" {
return fmt.Errorf("%s is not set", *input)
}
if strings.HasSuffix(*input, "/") {
*input = strings.TrimSuffix(*input, "/")
}
return nil
}

View File

@ -2,6 +2,7 @@ package main
import ( import (
"fmt" "fmt"
"log"
"os" "os"
"time" "time"
) )
@ -11,10 +12,13 @@ const (
PAYLOAD = TMPDIR + "/payload.json" PAYLOAD = TMPDIR + "/payload.json"
) )
var GAME_LOCATION = os.Getenv("GAME_PATH") var GAME_PATH = os.Getenv("GAME_PATH")
func main() { func main() {
getCache() getCache()
if err := SanitizeInput(&GAME_PATH); err != nil {
log.Fatal("GAME_PATH is not set")
}
installBepinex() installBepinex()
} }
@ -35,12 +39,12 @@ func getCache() error {
} }
func installBepinex() { func installBepinex() {
_, err := os.Stat(GAME_LOCATION + "/BepInEx") _, err := os.Stat(GAME_PATH + "/BepInEx")
if err != nil { if err != nil {
// install bepinex // install bepinex
DownloadBepinex() DownloadBepinex()
} else { } else {
fmt.Print("bepinex already installed") fmt.Println("bepinex already installed")
} }
} }