package cli import ( "context" "errors" "fmt" "os" "os/signal" "strings" "time" "github.com/Nwflower/reims-go/internal/config" "github.com/Nwflower/reims-go/internal/i18n" "github.com/Nwflower/reims-go/internal/installer" rt "github.com/Nwflower/reims-go/internal/runtime" ) func cmdInstaller(cfg *config.Config, loc i18n.Locale, args []string) error { if len(args) == 0 { return errors.New("usage: reims-go installer ") } active, err := rt.ResolveActive(cfg) if err != nil { return fmt.Errorf("%s", i18n.T(loc, "err.runtime_not_installed")) } c := installer.NewClient(cfg, *active) // Ctrl+C stops a download where it is; the next run continues it. ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt) defer stop() switch args[0] { case "list": list, err := c.ListOffline(ctx, stepPrinter()) if err != nil { return err } fmt.Printf("%-10s %-9s %-12s %-11s %9s\n", "VERSION", "BUILD", "TITLE", "POSTED", "SIZE") for _, r := range list { fmt.Printf("%-10s %-9s %-12s %-11s %9s\n", r.Version, r.Build, strings.TrimPrefix(r.Title, "macOS "), r.Posted, humanBytes(r.Size)) } return nil case "download": if len(args) < 2 { return errors.New("usage: reims-go installer download ") } bar := newProgressLine() res, err := c.DownloadOffline(ctx, args[1], bar.update) bar.finish() if errors.Is(err, context.Canceled) { fmt.Println(i18n.T(loc, "installer.paused", args[1])) return nil } if err != nil { return err } fmt.Println(i18n.T(loc, "installer.done", res.Version, res.Image)) return nil case "cached": list, err := c.ListCached() if err != nil { return err } if len(list) == 0 { fmt.Println(i18n.T(loc, "installer.cached.empty")) return nil } var total int64 for _, it := range list { what := strings.TrimSpace(strings.Join([]string{it.Title, it.Version, wrap(it.Build)}, " ")) if it.Partial { what += " " + i18n.T(loc, "installer.partial") } fmt.Printf("%-52s %9s %s\n", it.Name, humanBytes(it.Bytes), what) total += it.Bytes } fmt.Println(i18n.T(loc, "installer.cached.total", humanBytes(total), c.InstallersDir)) return nil case "delete": if len(args) < 2 { return errors.New("usage: reims-go installer delete ") } return c.DeleteCached(ctx, args[1], os.Stdout) default: return fmt.Errorf("unknown installer subcommand %q", args[0]) } } func wrap(build string) string { if build == "" { return "" } return "(" + build + ")" } // humanBytes matches reims-ctl's sizes: "950 MB", "18.4 GB" (downloads.rs:99-106). func humanBytes(b int64) string { if b >= 1_000_000_000 { return fmt.Sprintf("%.1f GB", float64(b)/1e9) } return fmt.Sprintf("%d MB", (b+999_999)/1_000_000) } func stepPrinter() func(installer.Update) { return func(u installer.Update) { if u.Kind != installer.KindProgress && u.Message != "" { fmt.Fprintln(os.Stderr, u.Message) } } } // progressLine redraws one terminal line for byte progress, at most 4 times a // second, and prints steps and notes on their own lines. type progressLine struct { last time.Time drawn bool speed speedMeter } func newProgressLine() *progressLine { return &progressLine{} } func (p *progressLine) update(u installer.Update) { switch u.Kind { case installer.KindStep, installer.KindNote: p.finish() fmt.Println(u.Message) p.speed = speedMeter{} case installer.KindProgress: rate := p.speed.add(u.Done) if time.Since(p.last) < 250*time.Millisecond && u.Percent < 100 { return } p.last = time.Now() var line string if u.Total > 0 { line = fmt.Sprintf(" %5.1f%% %s / %s", u.Percent, humanBytes(u.Done), humanBytes(u.Total)) } else { line = " " + humanBytes(u.Done) } if rate > 0 { line += fmt.Sprintf(" %s/s", humanBytes(int64(rate))) } fmt.Printf("\r%-60s", line) p.drawn = true } } func (p *progressLine) finish() { if p.drawn { fmt.Println() p.drawn = false } } // speedMeter is bytes per second over a sliding window of a few seconds. type speedMeter struct { samples []speedSample } type speedSample struct { at time.Time done int64 } func (s *speedMeter) add(done int64) float64 { now := time.Now() s.samples = append(s.samples, speedSample{now, done}) for len(s.samples) > 2 && now.Sub(s.samples[0].at) > 5*time.Second { s.samples = s.samples[1:] } first := s.samples[0] dt := now.Sub(first.at).Seconds() if dt < 0.5 || done < first.done { return 0 } return float64(done-first.done) / dt }