Files
Reims-Go/internal/cli/runtime_fetch.go
T
Nwflower c1b33c9c4f refactor(app): isolate native macOS adapters
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.
2026-11-30 11:18:09 -07:00

123 lines
3.5 KiB
Go

package cli
import (
"context"
"errors"
"flag"
"fmt"
"os"
"os/signal"
"github.com/Nwflower/reims-go/internal/config"
"github.com/Nwflower/reims-go/internal/host"
"github.com/Nwflower/reims-go/internal/i18n"
rt "github.com/Nwflower/reims-go/internal/runtime"
)
// runtime available [--alpha]: published runtimes.
func cmdRuntimeAvailable(cfg *config.Config, loc i18n.Locale, args []string) error {
fs := flag.NewFlagSet("runtime available", flag.ContinueOnError)
alpha := alphaFlag(fs, cfg, "Include alpha runtimes")
if err := fs.Parse(flagsFirst(args)); err != nil {
return err
}
list, err := rt.DefaultSource().ListRemote(context.Background(), *alpha)
if err != nil {
return err
}
if len(list) == 0 {
fmt.Println(i18n.T(loc, "runtime.remote.empty"))
return nil
}
fmt.Printf("%-12s %-10s %-11s %9s %s\n", "COMMIT", "CHANNEL", "PUBLISHED", "SIZE", "INSTALLED")
for _, r := range list {
mark := ""
if rt.IsInstalled(cfg, r.Commit) {
mark = "*"
}
date := r.PublishedAt
if len(date) >= 10 {
date = date[:10]
}
fmt.Printf("%-12s %-10s %-11s %9s %s\n", r.Commit, r.Channel, date, humanBytes(r.ArchiveSize), mark)
}
return nil
}
// runtime fetch [commit|latest] [--alpha]: download, verify, install, activate.
func cmdRuntimeFetch(cfg *config.Config, loc i18n.Locale, args []string) error {
fs := flag.NewFlagSet("runtime fetch", flag.ContinueOnError)
alpha := alphaFlag(fs, cfg, "Allow alpha runtimes for \"latest\"")
if err := fs.Parse(flagsFirst(args)); err != nil {
return err
}
want := fs.Arg(0)
explicit := want != "" && want != "latest"
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
defer stop()
src := rt.DefaultSource()
list, err := src.ListRemote(ctx, *alpha || explicit)
if err != nil {
return err
}
r, err := rt.Pick(list, want)
if err != nil {
return err
}
if !rt.IsInstalled(cfg, r.Commit) {
fmt.Println(i18n.T(loc, "runtime.remote.downloading", r.Commit, r.Channel, humanBytes(r.ArchiveSize)))
bar := newProgressLine()
path, err := src.Download(ctx, *r, rt.DownloadsDir(cfg), func(done, total int64) {
p := -1.0
if total > 0 {
p = float64(done) * 100 / float64(total)
}
bar.update(host.Update{Kind: host.KindProgress, Done: done, Total: total, Percent: p})
})
bar.finish()
if errors.Is(err, context.Canceled) {
fmt.Println(i18n.T(loc, "runtime.remote.paused"))
return nil
}
if err != nil {
return err
}
inst, err := rt.InstallFromArchive(cfg, path)
if err != nil {
return err
}
_ = os.Remove(path)
r.Commit = inst.Commit
}
cfg.ActiveRuntime = r.Commit
if err := config.Save("", cfg); err != nil {
return err
}
fmt.Println(i18n.T(loc, "runtime.activated", r.Commit))
return nil
}
// alphaFlag registers --alpha, defaulting to the configured channel, and
// --candidate as an alias for scripts written before the rename.
func alphaFlag(fs *flag.FlagSet, cfg *config.Config, usage string) *bool {
v := fs.Bool("alpha", rt.NormalizeChannel(cfg.RuntimeChannel) == rt.ChannelAlpha, usage)
fs.BoolVar(v, "candidate", *v, "Deprecated alias of --alpha")
return v
}
// flagsFirst moves "-x" arguments before positional ones, so
// "fetch latest --alpha" works like "fetch --alpha latest" (the flag package
// stops at the first positional argument).
func flagsFirst(args []string) []string {
var flags, rest []string
for _, a := range args {
if len(a) > 1 && a[0] == '-' {
flags = append(flags, a)
} else {
rest = append(rest, a)
}
}
return append(flags, rest...)
}