package cli import ( "context" "errors" "flag" "fmt" "os" "os/signal" "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" ) // runtime available [--alpha] [--source auto|github|gitcode]: 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") source := fs.String("source", rt.NormalizeSource(cfg.RuntimeSource), "Download source: auto, github, or gitcode") if err := fs.Parse(flagsFirst(args)); err != nil { return err } list, err := rt.DefaultSourceFor(*source).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 %-8s %-11s %9s %s\n", "COMMIT", "CHANNEL", "SOURCE", "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 %-8s %-11s %9s %s\n", r.Commit, r.Channel, r.Source, date, humanBytes(r.ArchiveSize), mark) } return nil } // runtime fetch [commit|latest] [--alpha] [--source auto|github|gitcode]: 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\"") source := fs.String("source", rt.NormalizeSource(cfg.RuntimeSource), "Download source: auto, github, or gitcode") 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.DefaultSourceFor(*source) 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(installer.Update{Kind: installer.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 (and their values for value flags like // --source) before positional ones, so "fetch latest --source gitcode --alpha" // works like "fetch --source gitcode --alpha latest" (the flag package stops // at the first positional argument). func flagsFirst(args []string) []string { var flags, rest []string for i := 0; i < len(args); i++ { a := args[i] if len(a) > 1 && a[0] == '-' { flags = append(flags, a) if (a == "-source" || a == "--source") && i+1 < len(args) { i++ flags = append(flags, args[i]) } } else { rest = append(rest, a) } } return append(flags, rest...) }