Download mods and headless server

till working on improvements to code this more like
a savepoint.
This commit is contained in:
2026-05-03 14:00:16 -06:00
parent aecc2162e7
commit 8fb2c2aeb2
10 changed files with 340 additions and 125 deletions

52
mods.go
View File

@@ -3,11 +3,9 @@ package main
import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"
"os/exec"
)
func findmodlist(modlist string) bool {
@@ -18,43 +16,32 @@ func findmodlist(modlist string) bool {
}
}
func download(filedest string, url string) error {
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
out, err := os.Create(filedest)
if err != nil {
return err
}
defer out.Close()
_, err = io.Copy(out, resp.Body)
return err
}
func downloadMods(modlist string) {
var c = readCfg("config.yml")
fmt.Printf("Updating mods from %s\n", modlist)
//read from modlist
file, err := os.Open(modlist)
if err != nil {
log.Fatalf("Error reading modlist: %v", err)
}
defer file.Close()
// create temp directory
tempDir, err := os.MkdirTemp("", "factoryman-*")
if err != nil {
log.Fatalln("Failed to create temporary directory")
}
defer os.RemoveAll(tempDir)
//decode json response from api
var modList ModList
decoder := json.NewDecoder(file)
if err := decoder.Decode(&modList); err != nil {
log.Fatalf("Error reading JSON: %v", err)
}
// Iterate over modlist.json
for _, mod := range modList.Mods {
switch mod.Name {
case "base":
@@ -66,6 +53,7 @@ func downloadMods(modlist string) {
case "space-age":
fmt.Println("Skipping space-age...")
default:
// query mod on modportal api
modportalurl := fmt.Sprintf("https://mods.factorio.com/api/mods/%s", mod.Name)
resp, err := http.Get(modportalurl)
if err != nil {
@@ -73,33 +61,29 @@ func downloadMods(modlist string) {
}
defer resp.Body.Close()
// decode json response
var moddata ModPortal
if err := json.NewDecoder(resp.Body).Decode(&moddata); err != nil {
log.Fatalf("Error reading JSON: %v", err)
}
//fmt.Printf("Mod: %s, Ver: %s, Enabled: %t\n", mod.Name, mod.Version, mod.Enabled)
// query download url for mod
accessToken := fmt.Sprintf("?username=%s&token=%s", c.Factoryman.UserName, c.Factoryman.ApiToken)
modDownloadUrl := fmt.Sprintf("https://mods.factorio.com%s%s", moddata.Releases[len(moddata.Releases)-1].DownloadUrl, accessToken)
//fmt.Println(modDownloadUrl)
fileName := string(tempDir + "/" + moddata.Releases[len(moddata.Releases)-1].Filename)
//download mod archive
downloadErr := download(fileName, modDownloadUrl)
if downloadErr != nil {
log.Fatalf("Error downloading: %v", downloadErr)
}
fmt.Printf("Downloaded: %s\n", fileName)
fmt.Println("extracting files...")
/*cmd := exec.Command("unzip", fileName, "-d", c.Server.ServDir+"/mods/")
out, err := cmd.Output()
if err != nil {
log.Fatalf("%v\n", err)
}*/
exec.Command("unzip", fileName, "-d", c.Server.ServDir+"/mods/")
//fmt.Printf("%s\ndone\n", out)
fmt.Printf("extracting files to %s\n", c.Server.ServDir+"/mods/")
// extract archive to mods folder
//exec.Command("unzip", fileName, "-d", c.Server.ServDir+"/mods/")
_, extracterr := unzip(fileName, c.Server.ServDir+"/mods/")
if extracterr != nil {
log.Fatalf("Error extracting archive: %v", extracterr)
}
}
}