Files
Reims-Go/internal/vmstart/display.go
T
NwflowerandClaude Opus 5.5 90fbce8b47 refactor: write small files through one atomic writer
Fifteen call sites each had their own temp-file-and-rename code. Some
used a fixed temporary name, so two writers could mix their content, and
most did not flush before the rename. They now share atomicfile.Write.

An architecture test keeps new os.Rename calls out of other packages.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-03 20:59:56 -07:00

72 lines
2.1 KiB
Go

package vmstart
import (
"encoding/json"
"errors"
"os"
"path/filepath"
"strings"
"github.com/Nwflower/reims-go/internal/atomicfile"
"github.com/Nwflower/reims-go/internal/host"
)
const vmDisplayConfigFile = ".reims-go-display.json"
type VMDisplayConfig struct {
TargetDisplay string `json:"target_display,omitempty"`
}
func NormalizeTargetDisplay(raw string) string {
target := strings.TrimSpace(raw)
if strings.EqualFold(target, "auto") || strings.EqualFold(target, "primary") {
return ""
}
return target
}
func ReadVMDisplay(vmDir string) VMDisplayConfig {
if strings.TrimSpace(vmDir) == "" {
return VMDisplayConfig{}
}
data, err := os.ReadFile(filepath.Join(vmDir, vmDisplayConfigFile))
var cfg VMDisplayConfig
if err != nil || json.Unmarshal(data, &cfg) != nil {
return VMDisplayConfig{}
}
cfg.TargetDisplay = NormalizeTargetDisplay(cfg.TargetDisplay)
return cfg
}
func SaveVMDisplay(vmDir string, cfg VMDisplayConfig) (VMDisplayConfig, error) {
if strings.TrimSpace(vmDir) == "" {
return VMDisplayConfig{}, errors.New("VM directory is empty")
}
cfg.TargetDisplay = NormalizeTargetDisplay(cfg.TargetDisplay)
dst := filepath.Join(vmDir, vmDisplayConfigFile)
if cfg.TargetDisplay == "" {
if err := os.Remove(dst); err != nil && !errors.Is(err, os.ErrNotExist) {
return VMDisplayConfig{}, err
}
return cfg, nil
}
if err := atomicfile.WriteJSON(dst, cfg, 0o600); err != nil {
return VMDisplayConfig{}, err
}
return cfg, nil
}
func ResolveTargetDisplay(cfg VMDisplayConfig, displays []host.DisplayInfo) (target *host.DisplayInfo, fallback bool) {
if display, ok := host.FindDisplayByName(displays, NormalizeTargetDisplay(cfg.TargetDisplay)); ok {
return display, false
}
return host.PrimaryDisplayOf(displays), true
}
// UsesFixedGuestMode keeps start and resume on the same display geometry.
func UsesFixedGuestMode(vmDir string, displays []host.DisplayInfo, borderless bool, resolution string) bool {
target, _ := ResolveTargetDisplay(ReadVMDisplay(vmDir), displays)
return host.ShouldUseFixedGuestMode(borderless, resolution, target)
}