Files
Reims-Go/internal/app/clone_flow.go
T
Dnyo666 ceeec602be feat(i18n,plugin): implement end-to-end i18n & L10n v2 architecture and language pack protocol
- Build single-source-of-truth 7-domain locale catalog (`locales/{zh-CN,en-US}/*.json`, 1,100+ keys) and `cmd/i18ngen` generator emitting Go catalogs and eager/lazy-split TypeScript domain modules (`core`/`vm` eager; `wizard`/`settings`/`plugins` lazy)
- Add zero-dependency ICU MessageFormat subset (`{var}`, `{n, number}`, CLDR `plural`, `select`), 8-hop cycle-free regional fallback (`zh-HK/zh-MO -> zh-Hant -> zh-TW -> zh-CN`), `auto` OS display-language preference, and `en-XA` pseudo-localization across Go and TypeScript
- Introduce `CodedError` supportability with `[E_CODE]` badges, collapsible `[detail]` diagnostics, and automatic SMC OSK / SMBIOS serial / MAC / token redaction
- Add Host × Guest macOS dual-terminology annotation (`FormatGuestTerm`) across 6 official Apple guest UI locales and localize Win32 system tray, file pickers, and DPI-adaptive `installguide` overlays with CJK font fallback probing
- Upgrade L1–L4 plugin localization to dual-track v2: Track A full language packs (`files`, WOFF2 `fonts`, cross-plugin `pluginOverrides`), Track B plugin-scoped `l10n` bundles (`HasUIMessages` isolation), live `plugin/localeChanged` notifications, `< 2ms` static CLI pack loading, and `reims-go plugin locale init|check` translator toolchain with `zh-TW` and `ja-JP` reference packs
- Harden security, WCAG 2.1 AA token contrast, `< 768px` responsive drawer & form layouts, and RTL logical properties; add backend security/locale regression tests and frontend `node --test` static/runtime guard suite in CI

# Conflicts:
#	frontend/src/i18n.ts
#	internal/cli/cli.go
#	internal/i18n/i18n.go
2026-11-30 14:56:19 +08:00

149 lines
5.3 KiB
Go

package app
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/Nwflower/reims-go/internal/driver/reims"
"github.com/Nwflower/reims-go/internal/host"
"github.com/Nwflower/reims-go/internal/i18n"
rt "github.com/Nwflower/reims-go/internal/runtime"
"github.com/Nwflower/reims-go/internal/vmstart"
wailsRuntime "github.com/wailsapp/wails/v2/pkg/runtime"
)
// CloneVMCoW derives a new VM instantaneously from a stopped source VM using a
// qcow2 copy-on-write overlay (`qemu-img create -f qcow2 -b <src> -F qcow2`)
// so the source golden disk is never mutated while the clone gets its own fresh
// OpenCore identity (SMBIOS/serial/MAC) via `reims-ctl create --disk --disk-mode in-place`.
func (a *App) CloneVMCoW(sourceName, cloneName string) error {
loc := a.locale()
cloneName = strings.TrimSpace(cloneName)
if cloneName == "" {
return i18n.NewCodedError(loc, "E_VM_CLONE_EMPTY_NAME", "err.clone.missing_name", nil, nil)
}
if strings.EqualFold(sourceName, cloneName) {
return i18n.NewCodedError(loc, "E_VM_CLONE_SAME_NAME", "err.clone.same_name", nil, nil)
}
a.mu.Lock()
cfgCopy := *a.cfg
a.mu.Unlock()
cfgCopy.Locale = loc
active, err := rt.ResolveActive(&cfgCopy)
if err != nil {
return err
}
c := reims.NewClient(&cfgCopy, *active)
ctx := a.baseCtx()
src, err := a.GetVMDetail(sourceName)
if err != nil {
return err
}
if a.isVMRunning(ctx, c, sourceName, src) {
return i18n.NewCodedError(loc, "E_VM_CLONE_RUNNING", "err.clone.source_running", i18n.Vars{"name": sourceName}, nil)
}
if src.HasInstaller {
return i18n.NewCodedError(loc, "E_VM_CLONE_RECOVERY", "err.clone.in_recovery", i18n.Vars{"name": sourceName}, nil)
}
srcDisk := src.Disk
if srcDisk != "" && !filepath.IsAbs(srcDisk) && src.Directory != "" {
srcDisk = filepath.Join(src.Directory, srcDisk)
}
if srcDisk == "" {
return i18n.NewCodedError(loc, "E_VM_CLONE_NO_DISK", "err.clone.no_disk", i18n.Vars{"name": sourceName}, nil)
}
if _, err := os.Stat(srcDisk); err != nil {
return i18n.NewCodedError(loc, "E_VM_CLONE_DISK_MISSING", "err.clone.disk_missing", i18n.Vars{"path": srcDisk}, err)
}
// Validate clone name and check for name collisions.
rep := host.Inspect(cfgCopy.DataRoot)
nameCheck := reims.ValidateCreateLocalized(reims.CreateOpts{Name: cloneName, CPUs: max(src.CPUs, 1), Memory: src.Memory}, rep, loc)
for _, iss := range nameCheck {
if iss.Field == "name" {
return i18n.NewCodedError(loc, "E_VM_CLONE_INVALID_NAME", "validate."+iss.Code, i18n.Vars{"name": cloneName}, nil)
}
}
if existing, err := c.List(ctx); err == nil {
for _, vm := range existing {
if strings.EqualFold(vm.Name, cloneName) {
return i18n.NewCodedError(loc, "E_VM_NAME_TAKEN", "validate.name_taken", i18n.Vars{"name": cloneName}, nil)
}
}
}
overlayPath := filepath.Join(cfgCopy.ImagesDir(), fmt.Sprintf("cow-%s.qcow2", cloneName))
if _, err := os.Stat(overlayPath); err == nil {
return i18n.NewCodedError(loc, "E_VM_CLONE_OVERLAY_EXISTS", "err.clone.overlay_exists", i18n.Vars{"path": overlayPath}, nil)
}
w := vmstart.LineWriter(func(line string) { a.emitLog(cloneName, line) })
env := rt.BuildChildEnv(&cfgCopy, *active)
a.emitLog(cloneName, fmt.Sprintf("Creating CoW overlay for %s backed by %s...", cloneName, filepath.Base(srcDisk)))
if err := reims.CreateCoWOverlay(ctx, active.QemuImgPath(), env, srcDisk, overlayPath, w, w); err != nil {
return err
}
audio := src.Audio
network := src.Network
logs := src.Logs
capture := src.Capture
createOpts := reims.CreateOpts{
Name: cloneName,
SourceMode: "disk",
Disk: overlayPath,
DiskMode: "in-place",
DiskController: src.DiskController,
CPUs: src.CPUs,
Memory: src.Memory,
Graphics: src.Graphics,
GPU: src.GPU,
Resolution: src.Resolution,
Displays: src.Displays,
Capture: &capture,
Audio: &audio,
Network: &network,
Logs: &logs,
Start: false,
}
if err := c.Create(ctx, createOpts, w, w); err != nil {
_ = os.Remove(overlayPath)
return err
}
if postSet, ok := reims.PostCreateSetOpts(createOpts); ok {
if serr := c.Set(ctx, postSet, w, w); serr != nil {
a.emitLog(cloneName, fmt.Sprintf("Warning: could not apply clone settings: %v", serr))
}
}
if src.GuestLang != "" || vmstart.HasSavedVMAffinity(src.Directory) {
if cloned, serr := c.Show(ctx, cloneName); serr == nil && cloned.Directory != "" {
if src.GuestLang != "" {
prep, lerr := a.prep(cloneName)
if lerr == nil {
lerr = prep.ApplyGuestLang(ctx, cloned.Directory, src.GuestLang, false)
}
if lerr != nil {
a.emitLog(cloneName, fmt.Sprintf("Warning: could not apply clone language (%s): %v", src.GuestLang, lerr))
}
}
if werr := writeVMMacOS(cloned.Directory, readVMMacOS(src.Directory)); werr != nil {
a.emitLog(cloneName, fmt.Sprintf("Warning: could not copy macOS release: %v", werr))
}
if vmstart.HasSavedVMAffinity(src.Directory) {
if _, aerr := vmstart.SaveVMAffinity(cloned.Directory, vmstart.ReadVMAffinity(src.Directory), rep.LogicalCPUs); aerr != nil {
a.emitLog(cloneName, fmt.Sprintf("Warning: could not copy CPU affinity setting: %v", aerr))
}
}
}
}
a.emitLog(cloneName, fmt.Sprintf("CoW clone %s ready (backing image %s untouched)", cloneName, srcDisk))
if a.ctx != nil {
wailsRuntime.EventsEmit(a.ctx, "reims:state-changed", true)
}
return nil
}