Files
Reims-Go/internal/cli/window_args.go
T

122 lines
3.8 KiB
Go

package cli
import (
"strconv"
"strings"
)
// The desktop window's own flags. `vm start` launches the window with them
// (or sends them to a window already running), so they are the whole
// protocol between command-line mode and the window. Each flag is one
// argument (`--name=value`) so a stray value is never taken for a command.
const (
flagBackground = "--background"
flagSafeMode = "--safe-mode"
flagStartVM = "--start-vm="
flagStartDriver = "--start-driver="
flagResumeVM = "--resume-vm="
flagStartResult = "--start-result="
flagCloseVM = "--close-vm="
flagWaitPID = "--wait-pid="
)
// WindowArgs is what the desktop window was asked to do at launch, or by a
// later `reims-go` run handing work to it.
type WindowArgs struct {
// Background keeps the window hidden; Reims-Go lives in the tray.
Background bool
// SafeMode skips all third-party plugins and reverts to the built-in theme (§9).
SafeMode bool
// StartVM is a VM to start as the GUI's StartVM would (Guest Tools and
// the Win10 cold-reboot watcher included).
StartVM string
// ResumeVM 只执行恢复,不把未挂起的虚拟机当作普通启动。
ResumeVM string
// StartDriver identifies the VM's owning driver when names are ambiguous.
StartDriver string
// StartResult is a file the window writes a StartResult to once StartVM
// has returned, so the command that asked can report it.
StartResult string
// CloseVM asks the running client to show a confirmation over the VM display.
CloseVM string
// WaitPID is a Reims-Go process that is quitting after installing an
// update: the new window waits for it to exit before taking the
// single-instance lock, which it would otherwise hand straight back to it.
WaitPID uint32
}
// ParseWindowArgs reads the window flags. ok is false when args hold
// anything else, which means command-line mode. No arguments is a plain
// window launch.
func ParseWindowArgs(args []string) (w WindowArgs, ok bool) {
for _, a := range args {
switch {
case a == flagBackground:
w.Background = true
case a == flagSafeMode:
w.SafeMode = true
case strings.HasPrefix(a, flagStartVM):
w.StartVM = strings.TrimPrefix(a, flagStartVM)
case strings.HasPrefix(a, flagResumeVM):
w.ResumeVM = strings.TrimPrefix(a, flagResumeVM)
if w.ResumeVM == "" {
return WindowArgs{}, false
}
case strings.HasPrefix(a, flagStartDriver):
w.StartDriver = strings.TrimPrefix(a, flagStartDriver)
case strings.HasPrefix(a, flagStartResult):
w.StartResult = strings.TrimPrefix(a, flagStartResult)
case strings.HasPrefix(a, flagCloseVM):
w.CloseVM = strings.TrimPrefix(a, flagCloseVM)
if w.CloseVM == "" {
return WindowArgs{}, false
}
case strings.HasPrefix(a, flagWaitPID):
pid, err := strconv.ParseUint(strings.TrimPrefix(a, flagWaitPID), 10, 32)
if err != nil || pid == 0 {
return WindowArgs{}, false
}
w.WaitPID = uint32(pid)
default:
return WindowArgs{}, false
}
}
return w, w.StartVM == "" || w.ResumeVM == ""
}
// Args is the inverse of ParseWindowArgs.
func (w WindowArgs) Args() []string {
var out []string
if w.Background {
out = append(out, flagBackground)
}
if w.SafeMode {
out = append(out, flagSafeMode)
}
if w.StartVM != "" {
out = append(out, flagStartVM+w.StartVM)
}
if w.ResumeVM != "" {
out = append(out, flagResumeVM+w.ResumeVM)
}
if w.StartDriver != "" {
out = append(out, flagStartDriver+w.StartDriver)
}
if w.StartResult != "" {
out = append(out, flagStartResult+w.StartResult)
}
if w.CloseVM != "" {
out = append(out, flagCloseVM+w.CloseVM)
}
if w.WaitPID != 0 {
out = append(out, flagWaitPID+strconv.FormatUint(uint64(w.WaitPID), 10))
}
return out
}
// StartResult is the JSON the window writes to WindowArgs.StartResult.
type StartResult struct {
OK bool `json:"ok"`
Error string `json:"error,omitempty"`
}