Files
Reims-Go/internal/app/window_launch.go
T
NwflowerandClaude Opus 5.5 0ed522656d fix(app): log every VM line under the VM's key
Some lines used the bare VM name as their source, so the log lines reported
with a failed command-line start left them out. Disk tasks also kept the
VM name in their step text.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-03 21:27:52 -07:00

152 lines
4.3 KiB
Go

package app
// What the desktop window does with cli.WindowArgs: from its own launch
// (`reims-go --background --start-vm=…`) or from a later `reims-go` run
// (Wails OnSecondInstanceLaunch). See internal/cli/window_args.go.
import (
"fmt"
"path/filepath"
"strings"
"github.com/Nwflower/reims-go/internal/cli"
"github.com/wailsapp/wails/v2/pkg/options"
)
// afterStartup runs the launch request once the window's context exists. A
// hidden launch with no tray to reopen it from shows the window after all.
func (a *App) afterStartup() {
a.mu.Lock()
launch := a.launch
noTray := a.trayMgr == nil
a.mu.Unlock()
if launch.Background && noTray {
a.showMainWindow()
}
a.handleWindowArgs(launch, false)
}
// onSecondInstance handles another `reims-go.exe` started while this one runs.
func (a *App) onSecondInstance(data options.SecondInstanceData) {
w, ok := cli.ParseWindowArgs(data.Args)
if !ok {
// Command-line mode never gets here (it does not take the lock), so
// this is an argument we don't know: treat it as a plain launch.
w = cli.WindowArgs{}
}
a.handleWindowArgs(w, true)
}
// handleWindowArgs starts the requested VM, or for a plain second launch
// (double-click while running in the tray) brings the window forward. A
// `vm start` handed over from the command line leaves the window as it is.
func (a *App) handleWindowArgs(w cli.WindowArgs, secondLaunch bool) {
if w.CloseVM != "" {
go a.confirmVMWindowClose(w.CloseVM)
return
}
if w.StartVM == "" && w.ResumeVM == "" {
if secondLaunch && !w.Background {
a.showMainWindow()
}
return
}
name := w.StartVM
if w.ResumeVM != "" {
name = w.ResumeVM
}
result := a.startResultPath(w.StartResult)
if w.StartResult != "" && result == "" {
a.emitVMLogForDriver(w.StartDriver, name, "Ignored a start result path outside the Reims-Go temp folder: "+w.StartResult)
}
a.emitVMLogForDriver(w.StartDriver, name, "Start requested from the command line")
go func() {
key := vmKeyForDriver(w.StartDriver, name)
capture := a.captureLogs(key)
var err error
if w.ResumeVM != "" {
err = a.ResumeVM(w.StartDriver, name)
} else {
err = a.startHandedVM(w.StartDriver, name)
}
lines := a.releaseLogs(key, capture)
if err != nil && len(lines) > 0 {
// StartVM's own error is often just "exit status 1"; the reason is
// in what it logged. The command line has no log panel to look at.
err = fmt.Errorf("%w\n%s", err, strings.Join(lines, "\n"))
}
if result != "" {
if werr := cli.WriteStartResult(result, err); werr != nil {
a.emitVMLogForDriver(w.StartDriver, name, "Could not report the start result to the command line: "+werr.Error())
}
}
}()
}
func (a *App) startHandedVM(driverID, name string) error {
return a.StartVM(driverID, name)
}
// logCapture keeps the last few log lines one source emits while it is
// registered, for reporting a failed start back to the command line.
type logCapture struct{ lines []string }
const logCaptureMax = 8
func (c *logCapture) add(line string) {
c.lines = append(c.lines, line)
if len(c.lines) > logCaptureMax {
c.lines = c.lines[len(c.lines)-logCaptureMax:]
}
}
// captureLogs starts collecting emitLog lines for source.
func (a *App) captureLogs(source string) *logCapture {
c := &logCapture{}
a.mu.Lock()
if a.logCaptures == nil {
a.logCaptures = make(map[string][]*logCapture)
}
a.logCaptures[source] = append(a.logCaptures[source], c)
a.mu.Unlock()
return c
}
// releaseLogs stops c and returns what it collected.
func (a *App) releaseLogs(source string, c *logCapture) []string {
a.mu.Lock()
defer a.mu.Unlock()
list := a.logCaptures[source]
for i, cur := range list {
if cur == c {
list = append(list[:i], list[i+1:]...)
break
}
}
if len(list) == 0 {
delete(a.logCaptures, source)
} else {
a.logCaptures[source] = list
}
return c.lines
}
// startResultPath accepts only a result file directly in this data root's
// temp folder named like the ones `vm start` makes, so a launch argument
// can't make Reims-Go overwrite an arbitrary file.
func (a *App) startResultPath(p string) string {
if p == "" {
return ""
}
a.mu.Lock()
tmp := a.cfg.TempDir()
a.mu.Unlock()
clean := filepath.Clean(p)
base := filepath.Base(clean)
if !strings.EqualFold(filepath.Dir(clean), filepath.Clean(tmp)) ||
!strings.HasPrefix(base, "cli-start-") || !strings.HasSuffix(base, ".json") {
return ""
}
return clean
}