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

121 lines
3.3 KiB
Go

package app
// Wails bindings for querying, downloading, and deleting cached macOS offline installers.
import (
"context"
"errors"
"fmt"
"strings"
"github.com/Nwflower/reims-go/internal/macos/installer"
rt "github.com/Nwflower/reims-go/internal/runtime"
)
func (a *App) installerClient() (*installer.Client, error) {
cfg := a.runtimeConfig()
active, err := rt.ResolveActive(&cfg)
if err != nil {
return nil, err
}
c := installer.NewClient(&cfg, *active)
c.Downloader = a.activeDownloader
return c, 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
// (whether standalone or driven by VM creation) is returned instead of starting
// another; CancelTask pauses a standalone download.
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
}
a.ensureTaskEmitter()
base := a.ctx
if base == nil {
base = context.Background()
}
var (
ctx context.Context
cancel context.CancelFunc
)
run, id, owned := installerDownloads.reserveOrFind("offline", version, "installer-download", func(initialPhase string) string {
ctx, cancel = context.WithCancel(base)
return tasks.startWithInstaller("installer-download", version, "", "offline", version, initialPhase, cancel)
})
if !owned {
return id, nil
}
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)
run.publish(u)
})
if ctx.Err() != nil && err != nil {
err = context.Canceled
}
installerDownloads.complete(run, res, err)
switch {
case err == nil:
a.emitLog("installer", "Installer ready: "+res.Image)
tasks.finish(id, res.Image, nil)
case errors.Is(err, context.Canceled):
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
}