Route generic App and CLI code through lifecycle and driver models. Move native macOS detail, create, settings and legacy flows into explicit files, share progress and redaction through host, and enforce the boundary with a file-scoped architecture ratchet.
114 lines
4.1 KiB
Go
114 lines
4.1 KiB
Go
package cli
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"github.com/Nwflower/reims-go/internal/config"
|
|
"github.com/Nwflower/reims-go/internal/driver"
|
|
"github.com/Nwflower/reims-go/internal/i18n"
|
|
"github.com/Nwflower/reims-go/internal/macos/installer"
|
|
"github.com/Nwflower/reims-go/internal/macos/opencore"
|
|
"github.com/Nwflower/reims-go/internal/macos/reims"
|
|
"github.com/Nwflower/reims-go/internal/macos/startprep"
|
|
"github.com/Nwflower/reims-go/internal/qmp"
|
|
)
|
|
|
|
func defaultMacOSGuestLang(loc i18n.Locale) string { return opencore.DefaultGuestLang(loc) }
|
|
|
|
// macOSCreateOptions is the CLI-neutral input to the native macOS create flow.
|
|
type macOSCreateOptions struct {
|
|
Name, Dir, MacOS, Memory, DiskSize, GPU, GuestLang string
|
|
CPUs int
|
|
Capture, Logs bool
|
|
}
|
|
|
|
func createMacOSVM(ctx context.Context, cfg *config.Config, loc i18n.Locale, in macOSCreateOptions, startAfterCreate, viaWindow bool) error {
|
|
prep, err := startprep.New(*cfg, func(line string) { fmt.Fprintln(os.Stderr, line) })
|
|
if err != nil {
|
|
return err
|
|
}
|
|
client := prep.Client
|
|
opts := reims.CreateOpts{Name: in.Name, Dir: in.Dir, MacOS: in.MacOS, CPUs: in.CPUs, Memory: in.Memory, DiskSize: in.DiskSize, GPU: in.GPU, Capture: &in.Capture, Logs: &in.Logs, GuestLang: strings.TrimSpace(in.GuestLang), Start: false}
|
|
existingVMs, _ := client.List(ctx)
|
|
if removed, rerr := reims.CleanAbortedCreateDir(cfg.XDGDataDir(), opts, existingVMs); rerr == nil && removed != "" {
|
|
fmt.Fprintf(os.Stderr, "Removed unfinished directory from earlier interrupted create: %s\n", removed)
|
|
}
|
|
if installer.ShouldPrefetchOcBinaryData(prep.Runtime.Manifest.UtilCommit, prep.Runtime.Manifest.UtilVersion) {
|
|
if err := installer.EnsureCachedAsset(ctx, cfg.XDGCacheDir(), installer.OcBinaryDataAsset, nil, nil); err != nil {
|
|
fmt.Fprintf(os.Stderr, "OpenCore resource pre-fetch skipped (reims-ctl will retry): %v\n", err)
|
|
}
|
|
}
|
|
if err := client.Create(ctx, opts, os.Stdout, os.Stderr); err != nil {
|
|
return err
|
|
}
|
|
createdDir := strings.TrimSpace(opts.Dir)
|
|
if det, err := client.Show(ctx, opts.Name); err == nil && det.Directory != "" {
|
|
createdDir = det.Directory
|
|
}
|
|
if createdDir == "" {
|
|
createdDir = filepath.Join(cfg.XDGDataDir(), "reims-ctl", "vms", opts.Name)
|
|
}
|
|
if err := driver.WriteOwnerDriver(createdDir, driver.BuiltinReimsMacOSDriverID); err != nil {
|
|
return err
|
|
}
|
|
if postSet, ok := reims.PostCreateSetOpts(opts); ok {
|
|
if err := client.Set(ctx, postSet, os.Stdout, os.Stderr); err != nil {
|
|
fmt.Fprintf(os.Stderr, "Warning: could not apply post-create defaults: %v\n", err)
|
|
}
|
|
}
|
|
if opts.GuestLang != "" {
|
|
vmDir := opts.Dir
|
|
if det, err := client.Show(ctx, in.Name); err == nil && det.Directory != "" {
|
|
vmDir = det.Directory
|
|
} else if vmDir == "" {
|
|
vmDir = filepath.Join(cfg.XDGDataDir(), "reims-ctl", "vms", in.Name)
|
|
}
|
|
if err := prep.ApplyGuestLang(ctx, vmDir, opts.GuestLang, false); err != nil {
|
|
fmt.Fprintf(os.Stderr, "Warning: could not apply macOS language (%s): %v\n", opts.GuestLang, err)
|
|
}
|
|
}
|
|
if startAfterCreate {
|
|
if err := startVM(ctx, cfg, in.Name, viaWindow); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
fmt.Println("\n" + i18n.T(loc, "vm.install_guide.title"))
|
|
fmt.Println(i18n.T(loc, "vm.install_guide.step1"))
|
|
fmt.Println(i18n.T(loc, "vm.install_guide.step2"))
|
|
fmt.Println(i18n.T(loc, "vm.install_guide.step3", in.Name))
|
|
return nil
|
|
}
|
|
|
|
func typeMacOSVM(ctx context.Context, cfg *config.Config, loc i18n.Locale, args []string) error {
|
|
if len(args) < 2 {
|
|
return fmt.Errorf("usage: reims-go vm type <name> [text] (no text: read stdin)")
|
|
}
|
|
prep, err := startprep.New(*cfg, func(line string) { fmt.Fprintln(os.Stderr, line) })
|
|
if err != nil {
|
|
return err
|
|
}
|
|
text := strings.Join(args[2:], " ")
|
|
if len(args) < 3 {
|
|
raw, err := io.ReadAll(os.Stdin)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
text = strings.TrimRight(string(raw), "\r\n")
|
|
}
|
|
detail, err := prep.Client.Show(ctx, args[1])
|
|
if err != nil {
|
|
return err
|
|
}
|
|
n, err := qmp.TypeIntoVM(ctx, detail.Directory, text, nil)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
fmt.Println(i18n.T(loc, "vm.typed", n))
|
|
return nil
|
|
}
|