Files
Reims-Go/internal/app/macos_release.go
T

41 lines
1.2 KiB
Go

package app
import (
"os"
"path/filepath"
"strings"
)
// vmMacOSFileName records which macOS release a VM was created with, for its
// icon. reims-ctl keeps no such field in vm.toml.
const vmMacOSFileName = ".reims-go-macos"
// GetVMMacOS returns the macOS release recorded when the VM was created: an
// installer version such as "26.7.3", or a codename such as "tahoe" for an
// online-recovery install. It is "" for VMs made from an existing disk or
// before this was recorded; the UI then guesses from the VM's name.
func (a *App) GetVMMacOS(name string) string {
dir := a.vmDirectory(name)
if dir == "" {
return ""
}
return readVMMacOS(dir)
}
func readVMMacOS(dir string) string {
b, err := os.ReadFile(filepath.Join(dir, vmMacOSFileName))
if err != nil {
return ""
}
return strings.TrimSpace(string(b))
}
// writeVMMacOS stores release next to vm.toml; an empty release writes nothing.
func writeVMMacOS(dir, release string) error {
release = strings.TrimSpace(strings.ReplaceAll(strings.ReplaceAll(release, "\r", ""), "\n", ""))
if dir == "" || release == "" {
return nil
}
return os.WriteFile(filepath.Join(dir, vmMacOSFileName), []byte(release+"\n"), 0o644)
}