Files
Reims-Go/internal/config/config.go
T
Nwflower faa2b71202 feat(guest-tools): add token-authenticated clipboard & shared folder bridge and update roadmap
- Pure-Go Win32 clipboard + localhost HTTP bridge (127.0.0.1:19847) with per-VM 256-bit random Bearer token and jailed shared directory (<DataRoot>/shared/<vm>)
- Pure-Go HFS+ REIMSTOOLS virtual USB disk with stock-macOS POSIX LaunchAgent installer (no Xcode or Python needed) for two-way UTF-8 clipboard and ~/Desktop/ReimsShare sync
- VMPage Guest Tools card with live connection badge, Open shared folder, Attach/Eject tools disk, and one-click QMP install command
- Update docs/roadmap.md with v0.3/v0.4 competitor-aligned follow-up tasks
2026-11-28 08:41:09 -07:00

211 lines
5.7 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 "candidate":
// whether online install offers candidate runtimes (portable-runtime-spec.md §6).
RuntimeChannel string `json:"runtime_channel,omitempty"`
}
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))
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
}