230 lines
6.5 KiB
Go
230 lines
6.5 KiB
Go
package config
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"os"
|
|
"path/filepath"
|
|
"runtime"
|
|
|
|
"github.com/Nwflower/reims-go/internal/i18n"
|
|
)
|
|
|
|
type Config struct {
|
|
DataRoot string `json:"data_root"`
|
|
Locale i18n.Locale `json:"locale"`
|
|
ActiveRuntime string `json:"active_runtime,omitempty"`
|
|
AutoPinPCores bool `json:"auto_pin_pcores"`
|
|
DefaultGPU string `json:"default_gpu,omitempty"`
|
|
// RuntimeChannel is "stable" (default, also when empty) or "alpha":
|
|
// whether online install offers alpha runtimes (portable-runtime-spec.md §6).
|
|
// "candidate", the name used before 2026-11-28, is read as "alpha".
|
|
RuntimeChannel string `json:"runtime_channel,omitempty"`
|
|
// RuntimeSource chooses where online runtimes and app updates are fetched:
|
|
// "auto" (default, also when empty: try GitHub first, fall back to GitCode),
|
|
// "github", or "gitcode".
|
|
RuntimeSource string `json:"runtime_source,omitempty"`
|
|
// MinimizeToTray keeps Reims-Go running in the Windows system tray when the
|
|
// main window is closed so Guest Tools bridges stay alive. Defaults to true.
|
|
MinimizeToTray *bool `json:"minimize_to_tray,omitempty"`
|
|
}
|
|
|
|
// ShouldMinimizeToTray returns true when closing the main window should hide to tray.
|
|
func (c *Config) ShouldMinimizeToTray() bool {
|
|
if c == nil || c.MinimizeToTray == nil {
|
|
return true
|
|
}
|
|
return *c.MinimizeToTray
|
|
}
|
|
|
|
func DefaultDataRoot() string {
|
|
if v := os.Getenv("REIMS_GO_DATA_ROOT"); v != "" {
|
|
return v
|
|
}
|
|
if runtime.GOOS == "windows" {
|
|
if st, err := os.Stat(`D:\`); err == nil && st.IsDir() {
|
|
return `D:\Data\reims-go`
|
|
}
|
|
}
|
|
home, err := os.UserHomeDir()
|
|
if err != nil {
|
|
return "reims-go-data"
|
|
}
|
|
return filepath.Join(home, "reims-go-data")
|
|
}
|
|
|
|
// CurrentDataRoot is the data folder in effect: REIMS_GO_DATA_ROOT, else the
|
|
// folder chosen at first launch (LocationPath), else DefaultDataRoot.
|
|
func CurrentDataRoot() string {
|
|
if v := os.Getenv("REIMS_GO_DATA_ROOT"); v != "" {
|
|
return v
|
|
}
|
|
if root, ok := ReadLocation(); ok {
|
|
return root
|
|
}
|
|
return DefaultDataRoot()
|
|
}
|
|
|
|
func DefaultConfigPath() string {
|
|
return filepath.Join(CurrentDataRoot(), "reims-go.json")
|
|
}
|
|
|
|
// LocationPath is the one small file kept outside the data folder: it records
|
|
// which folder was chosen, so the next launch finds it. %APPDATA%\reims-go on
|
|
// Windows. Everything else (runtimes, VMs, installers) lives in the data folder.
|
|
// REIMS_GO_LOCATION_FILE overrides it (testing the first-launch screen without
|
|
// touching the real pointer).
|
|
func LocationPath() string {
|
|
if p := os.Getenv("REIMS_GO_LOCATION_FILE"); p != "" {
|
|
return p
|
|
}
|
|
dir, err := os.UserConfigDir()
|
|
if err != nil {
|
|
return ""
|
|
}
|
|
return filepath.Join(dir, "reims-go", "location.json")
|
|
}
|
|
|
|
type location struct {
|
|
DataRoot string `json:"data_root"`
|
|
}
|
|
|
|
// ReadLocation returns the data folder chosen at first launch, if any.
|
|
func ReadLocation() (string, bool) {
|
|
p := LocationPath()
|
|
if p == "" {
|
|
return "", false
|
|
}
|
|
raw, err := os.ReadFile(p)
|
|
if err != nil {
|
|
return "", false
|
|
}
|
|
var loc location
|
|
if json.Unmarshal(raw, &loc) != nil || loc.DataRoot == "" {
|
|
return "", false
|
|
}
|
|
return loc.DataRoot, true
|
|
}
|
|
|
|
// SaveLocation records the chosen data folder for the next launch.
|
|
func SaveLocation(dataRoot string) error {
|
|
p := LocationPath()
|
|
if p == "" {
|
|
return errors.New("no user config directory")
|
|
}
|
|
if err := os.MkdirAll(filepath.Dir(p), 0o755); err != nil {
|
|
return err
|
|
}
|
|
data, err := json.MarshalIndent(location{DataRoot: dataRoot}, "", " ")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return os.WriteFile(p, append(data, '\n'), 0o644)
|
|
}
|
|
|
|
// Resolve loads the config for the app and reports whether first-launch setup
|
|
// is needed: no REIMS_GO_DATA_ROOT, no recorded folder, and no reims-go.json in
|
|
// the default folder. An install from before the picker existed (a config in
|
|
// the default folder) is adopted and recorded, so it is never asked again.
|
|
// legacyDataRoot is where installs before the first-launch picker kept their
|
|
// config; a variable so tests can point it elsewhere.
|
|
var legacyDataRoot = DefaultDataRoot
|
|
|
|
func Resolve() (cfg *Config, needsSetup bool, err error) {
|
|
if os.Getenv("REIMS_GO_FIRST_RUN") == "1" {
|
|
// Simulate a fresh machine (testing the first-launch screen).
|
|
return Default(), true, nil
|
|
}
|
|
if os.Getenv("REIMS_GO_DATA_ROOT") != "" {
|
|
cfg, err = Load("")
|
|
return cfg, false, err
|
|
}
|
|
if _, ok := ReadLocation(); ok {
|
|
cfg, err = Load("")
|
|
return cfg, false, err
|
|
}
|
|
legacy := filepath.Join(legacyDataRoot(), "reims-go.json")
|
|
if _, statErr := os.Stat(legacy); statErr == nil {
|
|
cfg, err = Load(legacy)
|
|
if err == nil {
|
|
_ = SaveLocation(cfg.DataRoot)
|
|
}
|
|
return cfg, false, err
|
|
}
|
|
return Default(), true, nil
|
|
}
|
|
|
|
func Default() *Config {
|
|
return &Config{
|
|
DataRoot: CurrentDataRoot(),
|
|
Locale: i18n.Detect(),
|
|
AutoPinPCores: true,
|
|
DefaultGPU: "auto",
|
|
}
|
|
}
|
|
|
|
func Load(path string) (*Config, error) {
|
|
if path == "" {
|
|
path = DefaultConfigPath()
|
|
}
|
|
raw, err := os.ReadFile(path)
|
|
if err != nil {
|
|
if errors.Is(err, os.ErrNotExist) {
|
|
return Default(), nil
|
|
}
|
|
return nil, err
|
|
}
|
|
cfg := Default()
|
|
if err := json.Unmarshal(raw, cfg); err != nil {
|
|
return nil, err
|
|
}
|
|
if cfg.DataRoot == "" {
|
|
cfg.DataRoot = filepath.Dir(path)
|
|
}
|
|
cfg.Locale = i18n.Normalize(string(cfg.Locale))
|
|
if cfg.RuntimeChannel == "candidate" {
|
|
cfg.RuntimeChannel = "alpha"
|
|
}
|
|
return cfg, nil
|
|
}
|
|
|
|
func Save(path string, cfg *Config) error {
|
|
if path == "" {
|
|
path = filepath.Join(cfg.DataRoot, "reims-go.json")
|
|
}
|
|
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
|
|
return err
|
|
}
|
|
data, err := json.MarshalIndent(cfg, "", " ")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return os.WriteFile(path, append(data, '\n'), 0o644)
|
|
}
|
|
|
|
func (c *Config) RuntimesDir() string { return filepath.Join(c.DataRoot, "runtimes") }
|
|
func (c *Config) XDGConfigDir() string { return filepath.Join(c.DataRoot, "config") }
|
|
func (c *Config) XDGDataDir() string { return filepath.Join(c.DataRoot, "data") }
|
|
func (c *Config) XDGCacheDir() string { return filepath.Join(c.DataRoot, "cache") }
|
|
func (c *Config) ImagesDir() string { return filepath.Join(c.DataRoot, "images") }
|
|
func (c *Config) TempDir() string { return filepath.Join(c.DataRoot, "tmp") }
|
|
func (c *Config) SharedDir() string { return filepath.Join(c.DataRoot, "shared") }
|
|
|
|
func (c *Config) EnsureDirs() error {
|
|
for _, d := range []string{
|
|
c.DataRoot,
|
|
c.RuntimesDir(),
|
|
c.XDGConfigDir(),
|
|
c.XDGDataDir(),
|
|
c.XDGCacheDir(),
|
|
c.ImagesDir(),
|
|
c.TempDir(),
|
|
c.SharedDir(),
|
|
} {
|
|
if err := os.MkdirAll(d, 0o755); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|