Every VM action had two bindings: one by name and one by driver and name. The window used only the second, so the first is gone and the WithDriver suffix with it. Unused bindings and frontend exports are removed too. Tests now reject bindings the window never calls, stale generated bindings and unused frontend exports. TypeScript rejects unused locals. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
127 lines
3.2 KiB
Go
127 lines
3.2 KiB
Go
package app
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"os"
|
|
"path/filepath"
|
|
"time"
|
|
|
|
"github.com/Nwflower/reims-go/internal/driver"
|
|
"github.com/Nwflower/reims-go/internal/i18n"
|
|
wailsRuntime "github.com/wailsapp/wails/v2/pkg/runtime"
|
|
)
|
|
|
|
func (a *App) beginVMStart() error {
|
|
a.mu.Lock()
|
|
defer a.mu.Unlock()
|
|
if a.quitMode != "" || a.allowQuit {
|
|
return errors.New(i18n.T(a.cfg.Locale, "quitInProgress"))
|
|
}
|
|
a.vmStarts.Add(1)
|
|
return nil
|
|
}
|
|
|
|
// QuitApp exits the desktop app and leaves its guests running.
|
|
func (a *App) QuitApp() {
|
|
_ = a.quitApp(false)
|
|
}
|
|
|
|
// ForceQuitApp powers off all running guests before exiting.
|
|
func (a *App) ForceQuitApp() error {
|
|
return a.quitApp(true)
|
|
}
|
|
|
|
func (a *App) quitApp(force bool) error {
|
|
a.mu.Lock()
|
|
if a.allowQuit || a.quitMode != "" {
|
|
a.mu.Unlock()
|
|
return nil
|
|
}
|
|
if force {
|
|
a.quitMode = "force"
|
|
a.mu.Unlock()
|
|
a.emitStateChanged()
|
|
a.vmStarts.Wait()
|
|
err := a.forceStopRunningVMs(a.baseCtx())
|
|
a.mu.Lock()
|
|
if err != nil {
|
|
a.quitMode = ""
|
|
a.mu.Unlock()
|
|
a.emitStateChanged()
|
|
return err
|
|
}
|
|
}
|
|
a.allowQuit = true
|
|
a.mu.Unlock()
|
|
if a.ctx != nil {
|
|
wailsRuntime.Quit(a.ctx)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (a *App) forceStopRunningVMs(ctx context.Context) error {
|
|
a.mu.Lock()
|
|
cfg := *a.cfg
|
|
svc := a.lifecycleSvc
|
|
a.mu.Unlock()
|
|
loc := i18n.EffectiveLocale(cfg.Locale)
|
|
list := func(ctx context.Context) ([]driver.VMListItem, error) {
|
|
vms, err := a.listVMsInternal(ctx)
|
|
if err != nil && svc == nil {
|
|
// A fresh installation can quit before its first runtime is installed.
|
|
_, registryErr := os.Stat(filepath.Join(cfg.XDGConfigDir(), "reims-ctl", "vms.toml"))
|
|
dirs, dirErr := os.ReadDir(filepath.Join(cfg.XDGDataDir(), "reims-ctl", "vms"))
|
|
if os.IsNotExist(registryErr) && (os.IsNotExist(dirErr) || (dirErr == nil && len(dirs) == 0)) {
|
|
return nil, nil
|
|
}
|
|
}
|
|
return vms, err
|
|
}
|
|
return stopListedVMs(ctx, loc, list, func(ctx context.Context, vm driver.VMListItem) error {
|
|
return a.stopVM(ctx, vm.DriverID, vm.Name, true)
|
|
})
|
|
}
|
|
|
|
const quitVMTimeout = 10 * time.Second
|
|
|
|
func stopListedVMs(ctx context.Context, loc i18n.Locale, list func(context.Context) ([]driver.VMListItem, error), stop func(context.Context, driver.VMListItem) error) error {
|
|
stopped := make(map[string]bool)
|
|
for {
|
|
listCtx, cancel := context.WithTimeout(ctx, quitVMTimeout)
|
|
vms, err := list(listCtx)
|
|
cancel()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
var next *driver.VMListItem
|
|
for i := range vms {
|
|
vm := vms[i]
|
|
if vm.State == "running" {
|
|
key := vmKeyForDriver(vm.DriverID, vm.Name)
|
|
if stopped[key] {
|
|
return errors.New(i18n.M(loc, "err.quit.stop_failed", i18n.Vars{"name": vm.Name}))
|
|
}
|
|
if next == nil {
|
|
cp := vm
|
|
next = &cp
|
|
}
|
|
}
|
|
}
|
|
if next == nil {
|
|
return nil
|
|
}
|
|
stopCtx, cancel := context.WithTimeout(ctx, quitVMTimeout)
|
|
err = stop(stopCtx, *next)
|
|
ctxErr := stopCtx.Err()
|
|
cancel()
|
|
if errors.Is(ctxErr, context.DeadlineExceeded) || errors.Is(err, context.DeadlineExceeded) {
|
|
return errors.New(i18n.M(loc, "err.quit.timeout", i18n.Vars{"name": next.Name}))
|
|
}
|
|
if err != nil {
|
|
return errors.New(i18n.M(loc, "err.quit.stop_failed", i18n.Vars{"name": next.Name}))
|
|
}
|
|
stopped[vmKeyForDriver(next.DriverID, next.Name)] = true
|
|
}
|
|
}
|