Files
Reims-Go/internal/app/endpoints_flow.go
T

210 lines
6.7 KiB
Go

package app
import (
"fmt"
"net/netip"
"os/exec"
"strconv"
"strings"
"github.com/Nwflower/reims-go/internal/driver"
wailsRuntime "github.com/wailsapp/wails/v2/pkg/runtime"
)
// Endpoint actions the host knows. A driver chooses which endpoints a VM has;
// it never chooses what the host runs for them.
const (
endpointActionDefault = ""
endpointActionTerminal = "terminal"
endpointActionVSCode = "vscode"
endpointActionConfig = "config"
endpointActionEdit = "edit"
endpointActionShell = "shell"
endpointActionCopy = "copy"
)
// launchSpec is one process the host starts for an endpoint.
type launchSpec struct {
exe string
args []string
}
// terminalLaunch runs argv in Windows Terminal, or in a console window of its
// own when Windows Terminal is not installed. ';' separates Windows Terminal
// commands, so it never reaches the tab title.
func terminalLaunch(lookPath func(string) (string, error), title string, argv ...string) launchSpec {
title = strings.NewReplacer(";", " ", "\r", " ", "\n", " ").Replace(title)
if r := []rune(title); len(r) > 64 {
title = string(r[:64])
}
if wt, err := lookPath("wt.exe"); err == nil {
return launchSpec{exe: wt, args: append([]string{"new-tab", "--title", title}, argv...)}
}
return launchSpec{exe: "cmd.exe", args: append([]string{"/k"}, argv...)}
}
// rdpTarget is the `host:port` argument of mstsc.exe, with IPv6 in brackets.
func rdpTarget(ep driver.VMEndpoint) string {
port := ep.Port
if port == 0 {
port = driver.DefaultRDPPort
}
host := ep.Host
if addr, err := netip.ParseAddr(host); err == nil && addr.Is6() {
host = "[" + host + "]"
}
return host + ":" + strconv.Itoa(port)
}
// endpointCommandLine is the command a user could paste into a terminal to
// reach the endpoint. Every field was validated by driver.ValidateEndpoint.
func endpointCommandLine(ep driver.VMEndpoint) (string, error) {
if err := driver.ValidateEndpoint(ep); err != nil {
return "", err
}
switch ep.Kind {
case driver.EndpointADB:
return "adb -s " + ep.Serial, nil
case driver.EndpointWSL:
return "wsl.exe -d " + ep.Distro, nil
case driver.EndpointRDP:
return "mstsc.exe /v:" + rdpTarget(ep), nil
case driver.EndpointVMConnect:
return `vmconnect.exe localhost "` + ep.VM + `"`, nil
case driver.EndpointSSH:
host := ep.Host
if host == "" {
host = "127.0.0.1"
}
target := host
if ep.User != "" {
target = ep.User + "@" + host
}
return fmt.Sprintf("ssh -p %d %s", ep.Port, target), nil
}
return "", fmt.Errorf("unknown endpoint kind %q", ep.Kind)
}
// buildEndpointLaunch maps an endpoint and action to the fixed command the
// host runs. ssh endpoints go through the SSH shortcuts instead.
func buildEndpointLaunch(vmName string, ep driver.VMEndpoint, action string, lookPath func(string) (string, error)) (launchSpec, error) {
if err := driver.ValidateEndpoint(ep); err != nil {
return launchSpec{}, err
}
if !ep.Ready {
return launchSpec{}, fmt.Errorf("the %s endpoint of %s is not ready", ep.Kind, vmName)
}
switch ep.Kind {
case driver.EndpointWSL:
switch action {
case endpointActionDefault, endpointActionTerminal:
return terminalLaunch(lookPath, vmName, "wsl.exe", "-d", ep.Distro), nil
case endpointActionVSCode:
for _, cand := range []string{"code.cmd", "code", "cursor.cmd", "cursor"} {
if p, err := lookPath(cand); err == nil {
return launchSpec{exe: p, args: []string{"--remote", "wsl+" + ep.Distro}}, nil
}
}
return launchSpec{}, fmt.Errorf("VS Code or Cursor CLI not found on PATH")
}
case driver.EndpointRDP:
if action == endpointActionDefault {
return launchSpec{exe: "mstsc.exe", args: []string{"/v:" + rdpTarget(ep)}}, nil
}
case driver.EndpointVMConnect:
switch action {
case endpointActionDefault:
return launchSpec{exe: "vmconnect.exe", args: []string{"localhost", ep.VM}}, nil
case endpointActionEdit:
return launchSpec{exe: "vmconnect.exe", args: []string{"localhost", ep.VM, "/edit"}}, nil
}
case driver.EndpointADB:
switch action {
case endpointActionDefault, endpointActionShell:
// The host does not bundle adb; it needs the Android SDK tools.
if _, err := lookPath("adb"); err != nil {
return launchSpec{}, fmt.Errorf("adb was not found on PATH; install the Android platform tools")
}
return terminalLaunch(lookPath, vmName, "adb", "-s", ep.Serial, "shell"), nil
}
}
return launchSpec{}, fmt.Errorf("the %s endpoint has no %q action", ep.Kind, action)
}
func findEndpoint(vm driver.VMInfo, kind string) (driver.VMEndpoint, error) {
for _, ep := range vm.Endpoints {
if ep.Kind == kind {
return ep, nil
}
}
return driver.VMEndpoint{}, fmt.Errorf("%s has no %s endpoint", vm.Name, kind)
}
// OpenVMEndpointWithDriver opens one way into a running guest: a terminal, a
// remote desktop, VMConnect or an adb shell. kind picks the endpoint and action
// one of its fixed actions ("" is its default).
func (a *App) OpenVMEndpointWithDriver(driverID, name, kind, action string) error {
vm, err := a.resolveVMInfo(driverID, name)
if err != nil {
return err
}
if vm.State != "running" {
return fmt.Errorf("VM %s is not running", name)
}
ep, err := findEndpoint(vm, kind)
if err != nil {
return err
}
if err := driver.ValidateEndpoint(ep); err != nil {
return err
}
if !ep.Ready {
return fmt.Errorf("the %s endpoint of %s is not ready", kind, name)
}
if action == endpointActionCopy {
line, err := endpointCommandLine(ep)
if err != nil {
return err
}
if a.ctx == nil {
return fmt.Errorf("no window")
}
return wailsRuntime.ClipboardSetText(a.ctx, line)
}
if kind == driver.EndpointSSH {
switch action {
case endpointActionDefault, endpointActionTerminal:
return a.OpenSSHTerminalWithDriver(vm.DriverID, vm.Name, "")
case endpointActionVSCode:
return a.OpenVSCodeSSHWithDriver(vm.DriverID, vm.Name, "")
case endpointActionConfig:
_, err := a.WriteSSHConfigWithDriver(vm.DriverID, vm.Name, "")
return err
}
return fmt.Errorf("the ssh endpoint has no %q action", action)
}
spec, err := buildEndpointLaunch(vm.Name, ep, action, exec.LookPath)
if err != nil {
return err
}
if err := exec.Command(spec.exe, spec.args...).Start(); err != nil {
return fmt.Errorf("open %s endpoint: %w", kind, err)
}
a.emitVMLogForDriver(vm.DriverID, vm.Name, fmt.Sprintf("Opened %s endpoint (%s)", kind, spec.exe))
return nil
}
// GetVMEndpointCommandWithDriver returns the command line that reaches an
// endpoint, for the user to copy.
func (a *App) GetVMEndpointCommandWithDriver(driverID, name, kind string) (string, error) {
vm, err := a.resolveVMInfo(driverID, name)
if err != nil {
return "", err
}
ep, err := findEndpoint(vm, kind)
if err != nil {
return "", err
}
return endpointCommandLine(ep)
}