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>
119 lines
3.4 KiB
Go
119 lines
3.4 KiB
Go
package app
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"time"
|
|
|
|
"github.com/Nwflower/reims-go/internal/driver"
|
|
"github.com/Nwflower/reims-go/internal/guesttools"
|
|
"github.com/Nwflower/reims-go/internal/host"
|
|
)
|
|
|
|
type VMTimezoneStatus struct {
|
|
Mode string `json:"mode"`
|
|
Timezone string `json:"timezone"`
|
|
HostTimezone string `json:"host_timezone"`
|
|
EffectiveTimezone string `json:"effective_timezone"`
|
|
GuestApplied string `json:"guest_applied"`
|
|
TimezoneError string `json:"timezone_error"`
|
|
HelperReady bool `json:"helper_ready"`
|
|
GuestConnected bool `json:"guest_connected"`
|
|
GuestAgentVersion int `json:"guest_agent_version"`
|
|
RequiredAgentVersion int `json:"required_agent_version"`
|
|
Running bool `json:"running"`
|
|
}
|
|
|
|
func (a *App) GetVMTimezone(driverID, name string) (VMTimezoneStatus, error) {
|
|
vm, err := a.resolveVMInfo(driverID, name)
|
|
if err != nil {
|
|
return VMTimezoneStatus{}, err
|
|
}
|
|
unlock := a.lockGuestToolsVM(vm.Name)
|
|
defer unlock()
|
|
cfg, _, err := guesttools.ReadVMConfigStrict(vm.Directory)
|
|
if err != nil {
|
|
return VMTimezoneStatus{}, err
|
|
}
|
|
return a.timezoneStatus(vm, cfg.GuestTimezone), nil
|
|
}
|
|
|
|
func (a *App) SetVMTimezone(driverID, name, setting string) (VMTimezoneStatus, error) {
|
|
if err := a.requireWritableDataRoot(); err != nil {
|
|
return VMTimezoneStatus{}, err
|
|
}
|
|
switch setting {
|
|
case "", "host", "off":
|
|
default:
|
|
if !host.ValidateIANATimezone(setting) {
|
|
return VMTimezoneStatus{}, fmt.Errorf("invalid IANA timezone: %q", setting)
|
|
}
|
|
}
|
|
epoch := a.vmDeleteEpoch(name)
|
|
vm, err := a.resolveVMInfo(driverID, name)
|
|
if err != nil {
|
|
return VMTimezoneStatus{}, err
|
|
}
|
|
if vm.Directory == "" {
|
|
return VMTimezoneStatus{}, fmt.Errorf("VM %s has no directory", vm.Name)
|
|
}
|
|
unlock := a.lockGuestToolsVM(vm.Name)
|
|
defer unlock()
|
|
if a.vmDeleteEpoch(name) != epoch {
|
|
return VMTimezoneStatus{}, os.ErrNotExist
|
|
}
|
|
if vm.Ref().DriverID == driver.BuiltinReimsMacOSDriverID {
|
|
if err := requireGuestToolsVM(vm.Directory); err != nil {
|
|
return VMTimezoneStatus{}, err
|
|
}
|
|
}
|
|
a.mu.Lock()
|
|
srv := a.guestTools
|
|
a.mu.Unlock()
|
|
cfg, err := guesttools.LoadOrCreateVMConfig(vm.Directory, vm.Name, "", 0)
|
|
if err != nil {
|
|
return VMTimezoneStatus{}, err
|
|
}
|
|
cfg.DriverID = vm.DriverID
|
|
cfg.GuestTimezone = setting
|
|
if err := guesttools.SaveVMConfig(vm.Directory, cfg); err != nil {
|
|
return VMTimezoneStatus{}, err
|
|
}
|
|
if srv != nil {
|
|
srv.SetTimezone(vm.Ref().Key(), cfg.Token, setting)
|
|
}
|
|
a.emitStateChanged()
|
|
return a.timezoneStatus(vm, setting), nil
|
|
}
|
|
|
|
func (a *App) timezoneStatus(vm driver.VMInfo, setting string) VMTimezoneStatus {
|
|
hostTZ := host.DetectHostTimezone()
|
|
status := VMTimezoneStatus{
|
|
HostTimezone: hostTZ,
|
|
EffectiveTimezone: guesttools.ResolveEffectiveTimezone(setting, hostTZ),
|
|
RequiredAgentVersion: guesttools.TimezoneAgentVersion,
|
|
Running: vm.State == "running",
|
|
}
|
|
switch setting {
|
|
case "", "host":
|
|
status.Mode = "host"
|
|
case "off":
|
|
status.Mode = "off"
|
|
default:
|
|
status.Mode = "custom"
|
|
status.Timezone = setting
|
|
}
|
|
a.mu.Lock()
|
|
srv := a.guestTools
|
|
a.mu.Unlock()
|
|
if srv != nil && status.Running {
|
|
guest := srv.Status(vm.Ref().Key(), 30*time.Second)
|
|
status.GuestApplied = guest.TimezoneApplied
|
|
status.TimezoneError = guest.TimezoneError
|
|
status.HelperReady = guest.TimezoneHelperReady
|
|
status.GuestConnected = guest.Connected
|
|
status.GuestAgentVersion = guest.AgentVersion
|
|
}
|
|
return status
|
|
}
|