mirror of
https://github.com/devvmhq/devvm.git
synced 2026-12-04 19:40:46 -10:00
Every argument and flag error is a devvm message naming what the command expected, followed by usage_help naming that command's own --help, and exits with 2. Arguments and flag values are checked in Args, before the root's PersistentPreRun and any VM lookup. - internal/cli/args.go: noArgs, exactArgs, maxArgs, minArgs (argsN, with ValidArgs), argsOf (the arguments in Use), checkCommands (wraps every command's Args with checkFlags and marks RunE's errors as ranError), checkFlagValue (--cpus, --memory, --disk, --ports, --lifetime, --older-than), exclusive (flag groups checked first, then Cobra's own required flags and groups), flagError (SetFlagErrorFunc: pflag's NotExist, ValueRequired, InvalidValue, InvalidSyntax errors). - Execute: 0, 1 for a failure (RunE's error: a message, or output_failed), 2 for a usage error; usage_error is only the fallback. - Checked early: ports LIST and add/rm LIST (vm.ParsePortList), network allow/deny RULEs, network on/off(/ask with -g), layer new NAME (build.CheckLayerName), retention KEY=VALUE (state.CheckRetention), setup --features '', secret NAME, completion SHELL, git-protect and ssh-agent values. - ui.UI.Error prints an error without ending the run (UI's method limit 44). - Messages: usage_help, arg_missing, args_too_many, args_none, arg_not_choice, no_subcommand, flag_unknown, flag_needs_value, flag_bad_value, flag_bad_switch, flags_exclusive, output_failed; usage_error reworded; doc links to troubleshooting. - Tests: TestUsageErrorsEveryCommand, TestRanErrorIsAFailure, TestArgsOf, TestFlagErrors; txtars check message names; e2e checks a bad port list exits 2. - Docs: troubleshooting (a command refused before it runs, exit codes), contributing (command arguments), AGENTS.md. Co-Authored-By: Claude Opus 5.5 <[email protected]>
85 lines
3.6 KiB
Go
85 lines
3.6 KiB
Go
package cli
|
|
|
|
import (
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/w3cj/devvm/internal/msg"
|
|
"github.com/w3cj/devvm/internal/sshagent"
|
|
"github.com/w3cj/devvm/internal/ui"
|
|
"github.com/w3cj/devvm/internal/vm"
|
|
)
|
|
|
|
// newSSHAgentCmd is devvm ssh-agent [on|devvm|off], and devvm's own
|
|
// agent's add and rm.
|
|
func newSSHAgentCmd(u *ui.UI) *cobra.Command {
|
|
cmd := &cobra.Command{
|
|
Use: "ssh-agent [on|devvm|off]",
|
|
Short: "Show or change SSH agent forwarding for this folder's VM",
|
|
Long: "Show whether this folder's VM forwards an SSH agent: off (the default), on (yours) or devvm " +
|
|
"(devvm's own, which holds only the keys added to it with devvm ssh-agent add). Then, once a key " +
|
|
"is added, it lists the keys devvm's own agent holds, as ssh-add -l does, loading those it lacks " +
|
|
"first, and each key added to it: its file, its fingerprint and how it's loaded. devvm ssh-agent " +
|
|
"on, devvm or off changes the VM's forwarding from its next start: devvm restart. While it's on " +
|
|
"and you're connected, anything in the VM can use the agent's keys.",
|
|
Args: maxArgs(1),
|
|
ValidArgs: sshagent.Modes,
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
withEnv(u, func(env *vm.Env) error {
|
|
s := vm.Settings{Env: env}
|
|
if len(args) == 0 {
|
|
return s.SSHAgent(cmd.Context(), u.Stdout())
|
|
}
|
|
return s.SetSSHAgent(cmd.Context(), args[0])
|
|
}, msg.SSHAgentFailed)
|
|
if len(args) == 0 {
|
|
withEnv(u, func(env *vm.Env) error { return vm.Settings{Env: env}.AgentKeys(cmd.Context(), u.Stdout()) }, msg.AgentKeysFailed)
|
|
}
|
|
return nil
|
|
},
|
|
}
|
|
cmd.AddCommand(newSSHAgentAddCmd(u), newSSHAgentRmCmd(u))
|
|
return cmd
|
|
}
|
|
|
|
func newSSHAgentAddCmd(u *ui.UI) *cobra.Command {
|
|
var o sshagent.AddOptions
|
|
cmd := &cobra.Command{
|
|
Use: "add KEY_FILE",
|
|
Short: "Load a key into devvm's own SSH agent",
|
|
Long: "Load a private key into devvm's own SSH agent, starting it first, and record it in " +
|
|
"~/.config/devvm/agent-keys with its fingerprint and options, replacing what was recorded for it. " +
|
|
"devvm loads it again when a VM needs it (after a reboot, or once its lifetime ran out), only " +
|
|
"while the file holds the same key. A key in a folder a VM mounts is refused.",
|
|
Args: exactArgs(1),
|
|
ValidArgsFunction: func(*cobra.Command, []string, string) ([]cobra.Completion, cobra.ShellCompDirective) {
|
|
return nil, cobra.ShellCompDirectiveDefault
|
|
},
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
withEnv(u, func(env *vm.Env) error { return vm.Settings{Env: env}.AgentAdd(cmd.Context(), args[0], o) }, msg.AgentKeysFailed)
|
|
return nil
|
|
},
|
|
}
|
|
f := cmd.Flags()
|
|
f.BoolVar(&o.Confirm, "confirm", false, "ask before each use of the key, through an ssh-askpass program")
|
|
f.StringVar(&o.Lifetime, "lifetime", "", "how long the agent keeps the key once loaded: a `DURATION` like 30m or 8h, or forever (default 12h)")
|
|
return cmd
|
|
}
|
|
|
|
func newSSHAgentRmCmd(u *ui.UI) *cobra.Command {
|
|
return &cobra.Command{
|
|
Use: "rm KEY_FILE",
|
|
Short: "Remove a key from devvm's own SSH agent",
|
|
Long: "Remove a key added with devvm ssh-agent add from devvm's own SSH agent, and from " +
|
|
"~/.config/devvm/agent-keys, so devvm doesn't load it again. The key file stays as it is, and " +
|
|
"needn't still exist. A key that wasn't added is refused: devvm ssh-agent lists those that were.",
|
|
Args: exactArgs(1),
|
|
ValidArgsFunction: func(*cobra.Command, []string, string) ([]cobra.Completion, cobra.ShellCompDirective) {
|
|
return nil, cobra.ShellCompDirectiveDefault
|
|
},
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
withEnv(u, func(env *vm.Env) error { return vm.Settings{Env: env}.AgentRemove(cmd.Context(), args[0]) }, msg.AgentKeysFailed)
|
|
return nil
|
|
},
|
|
}
|
|
}
|