Initial commit need to add nix code to setup service and timer example service and timer are provided
This commit is contained in:
commit
48fb09cfb9
9 changed files with 249 additions and 0 deletions
106
main.go
Normal file
106
main.go
Normal file
|
|
@ -0,0 +1,106 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Update struct {
|
||||
Version string `json:"version"`
|
||||
Build string `json:"build"`
|
||||
Published string `json:"published"`
|
||||
}
|
||||
|
||||
const (
|
||||
updateURL = "http://127.0.0.1:3000/updates"
|
||||
stateFilePath = "latest_version.txt"
|
||||
)
|
||||
func main() {
|
||||
resp, err := http.Get(updateURL)
|
||||
if err != nil {
|
||||
fmt.Printf("Failed to fetch updates: %v\n", err)
|
||||
return
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, _ := io.ReadAll(resp.Body)
|
||||
|
||||
var updates []Update
|
||||
err = json.Unmarshal(body, &updates)
|
||||
if err != nil {
|
||||
fmt.Printf("Failed to parse response: %v\n", err)
|
||||
return
|
||||
}
|
||||
|
||||
if len(updates) == 0 {
|
||||
fmt.Println("No updates found.")
|
||||
return
|
||||
}
|
||||
|
||||
latest := updates[0].Version
|
||||
|
||||
prevVersion := ""
|
||||
if data, err := os.ReadFile(stateFilePath); err == nil {
|
||||
prevVersion = strings.TrimSpace(string(data))
|
||||
}
|
||||
|
||||
if prevVersion == "" {
|
||||
fmt.Println("No previous version found. Storing latest:", latest)
|
||||
_ = os.WriteFile(stateFilePath, []byte(latest), 0644)
|
||||
return
|
||||
}
|
||||
|
||||
if versionCompare(latest, prevVersion) > 0 {
|
||||
fmt.Printf("New version found! %s > %s\n", latest, prevVersion)
|
||||
|
||||
// Run steamcmd with reforger_update script
|
||||
fmt.Println("Running update command...")
|
||||
cmd := exec.Command("./steamcmd.sh", "+runscript", "reforger_update")
|
||||
cmd.Stdout = os.Stdout
|
||||
cmd.Stderr = os.Stderr
|
||||
err := cmd.Run()
|
||||
if err != nil {
|
||||
fmt.Printf("Update command failed: %v\n", err)
|
||||
return
|
||||
}
|
||||
|
||||
// Store the new latest version
|
||||
_ = os.WriteFile(stateFilePath, []byte(latest), 0644)
|
||||
} else {
|
||||
fmt.Printf("No new version. Latest seen: %s\n", prevVersion)
|
||||
}
|
||||
}
|
||||
|
||||
func versionCompare(a, b string) int {
|
||||
segA := strings.Split(a, ".")
|
||||
segB := strings.Split(b, ".")
|
||||
|
||||
max := len(segA)
|
||||
if len(segB) > max {
|
||||
max = len(segB)
|
||||
}
|
||||
|
||||
for i := 0; i < max; i++ {
|
||||
var ai, bi int
|
||||
if i < len(segA) {
|
||||
fmt.Sscanf(segA[i], "%d", &ai)
|
||||
}
|
||||
if i < len(segB) {
|
||||
fmt.Sscanf(segB[i], "%d", &bi)
|
||||
}
|
||||
|
||||
if ai > bi {
|
||||
return 1
|
||||
}
|
||||
if ai < bi {
|
||||
return -1
|
||||
}
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue