added update check, save set path

This commit is contained in:
aomizu
2025-05-21 13:51:44 +09:00
parent cb57bdab57
commit 5bda331bde
6 changed files with 138 additions and 24 deletions

50
pkg/utils/prefs.go Normal file
View File

@@ -0,0 +1,50 @@
package utils
import (
"encoding/json"
"os"
"path/filepath"
)
type UserPrefs struct {
SuppressedUpdateVersion string `json:"suppressed_update_version"`
TurtleWoWPath string `json:"turtlewow_path"`
CrossOverPath string `json:"crossover_path"`
}
func getPrefsPath() (string, error) {
dir, err := os.UserConfigDir()
if err != nil {
return "", err
}
return filepath.Join(dir, "TurtleSilicon", "prefs.json"), nil
}
func LoadPrefs() (*UserPrefs, error) {
path, err := getPrefsPath()
if err != nil {
return nil, err
}
data, err := os.ReadFile(path)
if err != nil {
return &UserPrefs{}, nil // default prefs if not found
}
var prefs UserPrefs
if err := json.Unmarshal(data, &prefs); err != nil {
return &UserPrefs{}, nil
}
return &prefs, nil
}
func SavePrefs(prefs *UserPrefs) error {
path, err := getPrefsPath()
if err != nil {
return err
}
os.MkdirAll(filepath.Dir(path), 0755)
data, err := json.MarshalIndent(prefs, "", " ")
if err != nil {
return err
}
return os.WriteFile(path, data, 0644)
}

View File

@@ -1,9 +1,11 @@
package utils
import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"
"os/exec"
"path/filepath"
@@ -117,3 +119,21 @@ func QuotePathForShell(path string) string {
return fmt.Sprintf(`"%s"`, path)
}
func CheckForUpdate(currentVersion string) (latestVersion, releaseNotes string, updateAvailable bool, err error) {
resp, err := http.Get("https://api.github.com/repos/tairasu/TurtleSilicon/releases/latest")
if err != nil {
return "", "", false, err
}
defer resp.Body.Close()
var data struct {
TagName string `json:"tag_name"`
Body string `json:"body"`
}
if err := json.NewDecoder(resp.Body).Decode(&data); err != nil {
return "", "", false, err
}
latest := strings.TrimPrefix(data.TagName, "v")
return latest, data.Body, latest != currentVersion, nil
}