package main // Wails bindings for offline installers (roadmap M1.3). The download manager // page (M2.2) builds on these; downloads report through tasks (tasks.go). import ( "context" "fmt" "strings" "github.com/Nwflower/reims-go/internal/installer" rt "github.com/Nwflower/reims-go/internal/runtime" ) func (a *App) installerClient() (*installer.Client, error) { a.mu.Lock() cfg := *a.cfg a.mu.Unlock() active, err := rt.ResolveActive(&cfg) if err != nil { return nil, err } return installer.NewClient(&cfg, *active), nil } // ListOfflineReleases lists the offline installers Apple offers, newest first. // It reads Apple's catalog (a few seconds, needs the network). func (a *App) ListOfflineReleases() ([]installer.Release, error) { c, err := a.installerClient() if err != nil { return nil, err } base := a.ctx if base == nil { base = context.Background() } return c.ListOffline(base, nil) } // DownloadInstaller starts downloading an offline installer in the background // and returns its task ID. A download already running for the same release is // returned instead of starting another; CancelTask pauses it. func (a *App) DownloadInstaller(version string) (string, error) { version = strings.TrimSpace(version) if version == "" { return "", fmt.Errorf("no version given") } c, err := a.installerClient() if err != nil { return "", err } for _, t := range tasks.list() { if t.Kind == "installer-download" && t.Status == TaskRunning && strings.EqualFold(t.Subject, version) { return t.ID, nil } } a.ensureTaskEmitter() base := a.ctx if base == nil { base = context.Background() } ctx, cancel := context.WithCancel(base) id := tasks.start("installer-download", version, "", cancel) a.emitLog("installer", "Downloading macOS "+version+" offline installer") go func() { defer cancel() res, err := c.DownloadOffline(ctx, version, func(u installer.Update) { switch u.Kind { case installer.KindStep, installer.KindNote: a.emitLog("installer", u.Message) } tasks.update(id, u) }) switch { case err == nil: a.emitLog("installer", "Installer ready: "+res.Image) tasks.finish(id, res.Image, nil) case ctx.Err() != nil: a.emitLog("installer", "Download paused; starting it again continues where it stopped") tasks.finish(id, "", context.Canceled) default: a.emitLog("installer", "Download error: "+err.Error()) tasks.finish(id, "", err) } }() return id, nil } // ListInstallers lists the downloaded installers kept for new VMs. func (a *App) ListInstallers() ([]installer.Cached, error) { c, err := a.installerClient() if err != nil { return nil, err } return c.ListCached() } // DeleteInstaller deletes a downloaded installer through reims-ctl, which // refuses one a VM boots from or one being downloaded. func (a *App) DeleteInstaller(name string) error { c, err := a.installerClient() if err != nil { return err } if err := c.DeleteCached(context.Background(), name, nil); err != nil { return err } a.emitLog("installer", "Deleted "+name) return nil }