implmented cfg command

This commit is contained in:
2026-01-07 16:49:33 +01:00
parent 2c2a193b52
commit 0132358cfc
3 changed files with 121 additions and 0 deletions

81
internal/cfg.go Normal file
View File

@@ -0,0 +1,81 @@
package oplcli
import (
"fmt"
"io"
"net/http"
"os"
"path"
)
type FileMode int
const (
ModeOverride FileMode = iota
ModeAppend
ModeOnlyCreate
)
func downloadCfgForGame(gameID string, output string, mode FileMode) error {
url := getDownloadURLFromGithub(gameID)
filePath := path.Join(output, fmt.Sprintf("%s.cfg", gameID))
file, err := openFileBasedOnMode(filePath, mode)
if err != nil {
return err
}
defer file.Close()
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
_, err = io.Copy(file, resp.Body)
if err != nil {
return err
}
return nil
}
func openFileBasedOnMode(filePath string, mode FileMode) (*os.File, error) {
_, err := os.Stat(filePath)
if err != nil {
if err != os.ErrNotExist {
file, err := os.Create(filePath)
if err != nil {
return nil, err
}
return file, nil
}
}
if mode == ModeOverride {
file, err := os.OpenFile(filePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666)
if err != nil {
return nil, err
}
return file, nil
}
if mode == ModeAppend {
file, err := os.OpenFile(filePath, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
return nil, err
}
return file, nil
}
return nil, fmt.Errorf("File already exists")
}
func getDownloadURLFromGithub(gameID string) string {
// const baseURL = "https://raw.githubusercontent.com/VTSTech/PS2-OPL-CFG/refs/heads/master/CFG/" // This one is more basic
const baseURL = "https://raw.githubusercontent.com/Tom-Bruise/PS2-OPL-CFG-Database/refs/heads/master/CFG_en/" // This one contains descriptions and more
return fmt.Sprintf("%s/%s.cfg", baseURL, gameID)
}

View File

@@ -76,3 +76,19 @@ func DownloadGameArt(files []string, output string, config Config) {
downloadArtForGame(gameID, output)
}
}
func DownloadCfg(files []string, output string, mode FileMode, config Config) {
for _, file := range files {
fmt.Printf("Scanning file: %s\n", file)
gameID, err := scanGameFileForID(file)
if err != nil {
fmt.Printf("Failed to scan game file: %s", err)
os.Exit(1)
}
fmt.Printf("Found game ID: %s\n", gameID)
downloadCfgForGame(gameID, output, mode)
}
}