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

75 lines
2.2 KiB
Go

package cli
import "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="
flagStartResult = "--start-result="
)
// 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
// StartResult is a file the window writes a StartResult to once StartVM
// has returned, so the command that asked can report it.
StartResult string
}
// 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, flagStartResult):
w.StartResult = strings.TrimPrefix(a, flagStartResult)
default:
return WindowArgs{}, false
}
}
return w, true
}
// 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.StartResult != "" {
out = append(out, flagStartResult+w.StartResult)
}
return out
}
// StartResult is the JSON the window writes to WindowArgs.StartResult.
type StartResult struct {
OK bool `json:"ok"`
Error string `json:"error,omitempty"`
}