Route generic App and CLI code through lifecycle and driver models. Move native macOS detail, create, settings and legacy flows into explicit files, share progress and redaction through host, and enforce the boundary with a file-scoped architecture ratchet.
162 lines
5.4 KiB
Go
162 lines
5.4 KiB
Go
package app
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/Nwflower/reims-go/internal/driver"
|
|
"github.com/Nwflower/reims-go/internal/host"
|
|
"github.com/Nwflower/reims-go/internal/i18n"
|
|
"github.com/Nwflower/reims-go/internal/macos/reims"
|
|
"github.com/Nwflower/reims-go/internal/vmstart"
|
|
wailsRuntime "github.com/wailsapp/wails/v2/pkg/runtime"
|
|
)
|
|
|
|
// ToggleBorderless switches borderless fullscreen startup through the native macOS settings flow.
|
|
func (a *App) ToggleBorderless(name string, enabled bool) error {
|
|
v := enabled
|
|
return a.ApplyVMSettings(reims.SetOpts{Name: name, Borderless: &v})
|
|
}
|
|
|
|
func (a *App) ValidateCreate(opts reims.CreateOpts) []reims.ValidationIssue {
|
|
a.mu.Lock()
|
|
root := a.cfg.DataRoot
|
|
loc := i18n.EffectiveLocale(a.cfg.Locale)
|
|
a.mu.Unlock()
|
|
issues := reims.ValidateCreateLocalized(opts, host.Inspect(root), loc)
|
|
// reims-ctl refuses a name already registered; say so before the create runs.
|
|
if vms, err := a.listVMsInternal(context.Background()); err == nil {
|
|
for _, vm := range vms {
|
|
if strings.EqualFold(vm.Name, strings.TrimSpace(opts.Name)) {
|
|
issues = append(issues, reims.ValidationIssue{Field: "name", Code: "name_taken", Message: i18n.T(loc, "validate.name_taken", vm.Name)})
|
|
break
|
|
}
|
|
}
|
|
}
|
|
return issues
|
|
}
|
|
|
|
func (a *App) PreviewCreate(opts reims.CreateOpts) (string, error) {
|
|
return reims.PreviewCreateCommand(opts)
|
|
}
|
|
|
|
func (a *App) CreateVM(opts reims.CreateOpts) error {
|
|
if err := a.requireWritableDataRoot(); err != nil {
|
|
return err
|
|
}
|
|
return a.createVMFlow(opts)
|
|
}
|
|
|
|
func (a *App) ValidateSet(opts reims.SetOpts) ([]reims.ValidationIssue, error) {
|
|
cur, err := a.GetVMDetail(opts.Name)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
a.mu.Lock()
|
|
root := a.cfg.DataRoot
|
|
loc := a.cfg.Locale
|
|
a.mu.Unlock()
|
|
return reims.ValidateSetLocalized(opts, cur, host.Inspect(root), loc), nil
|
|
}
|
|
|
|
func (a *App) ApplyVMSettings(opts reims.SetOpts) error {
|
|
cur, err := a.GetVMDetail(opts.Name)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
a.mu.Lock()
|
|
root := a.cfg.DataRoot
|
|
loc := a.cfg.Locale
|
|
a.mu.Unlock()
|
|
if issues := reims.ValidateSetLocalized(opts, cur, host.Inspect(root), loc); len(issues) > 0 {
|
|
return fmt.Errorf("%s", issues[0].Message)
|
|
}
|
|
w := vmstart.LineWriter(func(line string) { a.emitVMLog(opts.Name, line) })
|
|
a.mu.Lock()
|
|
svc := a.lifecycleSvc
|
|
a.mu.Unlock()
|
|
guestLangChanged := strings.TrimSpace(opts.GuestLang) != "" && strings.TrimSpace(opts.GuestLang) != cur.GuestLang
|
|
if svc != nil {
|
|
raw, _ := json.Marshal(opts)
|
|
var changes map[string]any
|
|
_ = json.Unmarshal(raw, &changes)
|
|
if err := svc.Set(context.Background(), driver.NewVMRef("", opts.Name), changes, w, w); err != nil {
|
|
a.emitVMLog(opts.Name, "Set error: "+err.Error())
|
|
return err
|
|
}
|
|
} else {
|
|
c, err := a.client()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if reims.HasCtlSetChanges(opts) {
|
|
if err := c.Set(context.Background(), opts, w, w); err != nil {
|
|
a.emitVMLog(opts.Name, "Set error: "+err.Error())
|
|
return err
|
|
}
|
|
}
|
|
if guestLangChanged {
|
|
prep, err := a.prep(opts.Name)
|
|
if err == nil {
|
|
err = prep.ApplyGuestLang(context.Background(), cur.Directory, opts.GuestLang, cur.State == "running")
|
|
}
|
|
if err != nil {
|
|
a.emitVMLog(opts.Name, "Guest language error: "+err.Error())
|
|
return err
|
|
}
|
|
} else if cur.State != "running" && opts.Graphics != "" && opts.Graphics != cur.Graphics {
|
|
if prep, err := a.prep(opts.Name); err == nil {
|
|
prep.ReplayGuestLang(context.Background(), cur.Directory)
|
|
}
|
|
}
|
|
}
|
|
if cur.State == "running" {
|
|
var diffs []PendingChange
|
|
if opts.CPUs > 0 && opts.CPUs != cur.CPUs {
|
|
diffs = append(diffs, PendingChange{Field: "cpus", From: fmt.Sprintf("%d", cur.CPUs), To: fmt.Sprintf("%d", opts.CPUs)})
|
|
}
|
|
if opts.Memory != "" && !strings.EqualFold(opts.Memory, cur.Memory) {
|
|
diffs = append(diffs, PendingChange{Field: "memory", From: cur.Memory, To: opts.Memory})
|
|
}
|
|
if opts.Resolution != "" && opts.Resolution != cur.Resolution {
|
|
diffs = append(diffs, PendingChange{Field: "resolution", From: cur.Resolution, To: opts.Resolution})
|
|
}
|
|
if opts.GPU != "" && opts.GPU != cur.GPU {
|
|
diffs = append(diffs, PendingChange{Field: "gpu", From: cur.GPU, To: opts.GPU})
|
|
}
|
|
if opts.Displays > 0 && opts.Displays != cur.Displays {
|
|
diffs = append(diffs, PendingChange{Field: "displays", From: fmt.Sprintf("%d", cur.Displays), To: fmt.Sprintf("%d", opts.Displays)})
|
|
}
|
|
if opts.Borderless != nil && *opts.Borderless != cur.Borderless {
|
|
diffs = append(diffs, PendingChange{Field: "borderless", From: fmt.Sprintf("%v", cur.Borderless), To: fmt.Sprintf("%v", *opts.Borderless)})
|
|
}
|
|
if opts.Audio != nil && *opts.Audio != cur.Audio {
|
|
diffs = append(diffs, PendingChange{Field: "audio", From: fmt.Sprintf("%v", cur.Audio), To: fmt.Sprintf("%v", *opts.Audio)})
|
|
}
|
|
if opts.AudioBackend != "" && opts.AudioBackend != cur.AudioBackend {
|
|
from := cur.AudioBackend
|
|
if from == "" {
|
|
from = "auto"
|
|
}
|
|
diffs = append(diffs, PendingChange{Field: "audio_backend", From: from, To: opts.AudioBackend})
|
|
}
|
|
if guestLangChanged {
|
|
from := cur.GuestLang
|
|
if from == "" {
|
|
from = "en-US:0"
|
|
}
|
|
diffs = append(diffs, PendingChange{Field: "guest_lang", From: from, To: strings.TrimSpace(opts.GuestLang)})
|
|
}
|
|
if len(diffs) > 0 {
|
|
a.mu.Lock()
|
|
a.pendingChanges[vmKey(opts.Name)] = append(a.pendingChanges[vmKey(opts.Name)], diffs...)
|
|
a.mu.Unlock()
|
|
}
|
|
}
|
|
a.emitVMLog(opts.Name, "Updated VM settings for "+opts.Name)
|
|
wailsRuntime.EventsEmit(a.ctx, "reims:state-changed", true)
|
|
return nil
|
|
}
|