Files
Reims-Go/internal/cli/runtime_fetch.go
T
千川白浪andClaude Opus 5.5 2f1e0bb0e4 feat(cli): fold the command line into reims-go.exe
Only reims-go.exe ships now; reims-go-cli.exe is gone. Without
arguments it opens the window, with a command it runs command-line mode
(cmd/reims-go moved to internal/cli). Missing standard handles are
attached to the parent console; handles the caller passes in (pipes,
files) are kept, so scripts and agents get output and exit codes.

`vm start` hands the VM to the window process so it gets Guest Tools
and the Win10 cold-reboot watcher, which a command that exits could not
run:
- a running window gets the request through Wails' single-instance
  protocol and stays hidden;
- otherwise the command launches the window tray-only (--background),
  detached from the caller's console and handles;
- the window writes the outcome, with the VM's last log lines on
  failure, to <DataRoot>\tmp\cli-start-*.json (it accepts no other
  path) and the command reports it.
With --data-root the VM still starts in the command itself.

The window now takes a single-instance lock: a second double-click
brings the running window forward. `wails dev` builds use their own
lock name.

Tested on Win10 19045 with macos-tahoe: start from the command line
with no window running (4 s, exit 0) and with one running (3 s, exit 0,
same process); the cold-reboot monitor was held by the window.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-28 22:52:07 -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/i18n"
"github.com/Nwflower/reims-go/internal/installer"
rt "github.com/Nwflower/reims-go/internal/runtime"
)
// runtime available [--alpha]: published runtimes on GitHub Releases.
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("%-9s %-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("%-9s %-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(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 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...)
}