Files
Reims-Go/console_windows.go
千川白浪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

59 lines
1.4 KiB
Go

//go:build windows
package main
import (
"os"
"syscall"
)
var (
kernel32 = syscall.NewLazyDLL("kernel32.dll")
procAttachConsole = kernel32.NewProc("AttachConsole")
)
const attachParentProcess = ^uintptr(0) // ATTACH_PARENT_PROCESS, (DWORD)-1
// attachConsole gives command-line mode somewhere to write. reims-go.exe is
// a GUI-subsystem program, so Windows starts it without a console. Handles
// the caller passed in (a pipe, a file: how agents and scripts run it) are
// kept; only missing ones are pointed at the console of the terminal that
// launched it.
func attachConsole() {
out, errOut, in := usable(os.Stdout), usable(os.Stderr), usable(os.Stdin)
if out && errOut && in {
return
}
if r, _, _ := procAttachConsole.Call(attachParentProcess); r == 0 {
return // started from Explorer or a shortcut: nowhere to write
}
if !out || !errOut {
if f, err := os.OpenFile("CONOUT$", os.O_RDWR, 0); err == nil {
if !out {
os.Stdout = f
}
if !errOut {
os.Stderr = f
}
}
}
if !in {
if f, err := os.OpenFile("CONIN$", os.O_RDWR, 0); err == nil {
os.Stdin = f
}
}
}
// usable reports whether f wraps a real handle the caller passed in.
func usable(f *os.File) bool {
if f == nil {
return false
}
h := syscall.Handle(f.Fd())
if h == 0 || h == syscall.InvalidHandle {
return false
}
t, err := syscall.GetFileType(h)
return err == nil && t != syscall.FILE_TYPE_UNKNOWN
}