Documentation
¶
Overview ¶
Package cli is the `remohop` command framework: a registry that feature packages add their subcommands to from init(), and the dispatcher that turns `remohop [global flags] [<ID>] <command> [args]` into a call.
Index ¶
- Constants
- Variables
- func FlagExit(err error) int
- func FlagHint(name string) string
- func IsID(s string) bool
- func LooksLikeTarget(s string) bool
- func Main(ctx context.Context, args []string, stdin io.Reader, stdout, stderr io.Writer) int
- func NewFlagSet(env *Env, name, usage string) *flag.FlagSet
- func NormalizeTarget(t string) string
- func OneLine(s string) string
- func Register(c *Command)
- func Sanitize(s string) string
- type Command
- type Env
- type Globals
Constants ¶
const ( ExitOK = 0 ExitError = 1 // the command ran and failed ExitUsage = 2 // bad flags or arguments ExitConnect = 255 // could not reach or authenticate to the remote peer )
Exit codes shared by every command.
Variables ¶
Dialer is the production implementation of Env.DialFunc. internal/dial installs it from init().
var KnownTarget func(name string) bool
KnownTarget reports whether name is a saved host's alias. internal/dial sets it (this package cannot import the peer store).
var Version = "dev"
Version is the build version, set with -ldflags "-X .../internal/cli.Version=...".
Functions ¶
func FlagExit ¶
FlagExit is the exit code after Parse of a NewFlagSet flag set failed with err: ExitOK for -h (the usage is printed), ExitUsage otherwise (the one line is printed).
func FlagHint ¶
FlagHint is the pointer to a command's full help that follows a one-line usage error.
func LooksLikeTarget ¶
LooksLikeTarget reports whether s names a host rather than a command: a 9-digit ID, HOST:PORT, or a saved alias.
func Main ¶
Main parses args (without the program name), runs the selected command and returns the exit code.
func NewFlagSet ¶
NewFlagSet returns a flag set for a command whose errors take one line, the problem and where to read more ("remohop: cp: flag provided but not defined: -x (see `remohop help cp`)"), rather than the whole usage. name is the command, optionally with its subcommand ("net up"). -h after other flags prints usage in full.
func NormalizeTarget ¶
NormalizeTarget strips the spaces and dashes people put in a 9-digit ID ("123 456 789", "123-456-789"). Anything that is not such an ID is returned unchanged.
func OneLine ¶
OneLine shows text from an untrusted peer on one line, with nothing hidden: line breaks, tabs, other control characters, characters that do not print (bidirectional overrides, zero-width and format characters, line and paragraph separators) and invalid bytes are written as Go escapes (\n, \t, \x1b, \u202e). Use it where the reader decides on what the text says, such as an approval prompt: Sanitize keeps line breaks, which can push the start of a request off the screen.
func Register ¶
func Register(c *Command)
Register adds a command. It panics on a duplicate name so collisions show up at startup rather than as a silently shadowed command.
func Sanitize ¶
Sanitize makes text from an untrusted peer safe to print on a terminal. It replaces control characters (keeping newline and tab), the C1 range, and the Unicode bidirectional overrides with '?', so the text cannot move the cursor, retitle the window, write the clipboard through OSC 52, or disguise what is displayed. Commands must apply it to any peer-supplied string they print themselves; raw payloads the user asked for (terminal output, file contents, clipboard text) are of course left alone.
Types ¶
type Command ¶
type Command struct {
Name string
Summary string // one line, shown in `remohop help`
Usage string // full usage text, shown by `remohop help <name>`
// Remote commands are invoked as `remohop <ID> <name> ...` and use env.Conn.
Remote bool
Hidden bool
// Topics are further help pages, shown by `remohop help <name> <topic>`
// (and all of them by `remohop help <name> all` or `remohop <name>
// --help-all`), so that Usage can stay short.
Topics map[string]string
// RemoteRun, on a local command, is what `remohop <ID> <name> ...` runs: the
// same verb done by the remote machine (`remohop wake MAC` sends the packet
// from here, `remohop <ID> wake MAC` asks that host to send it).
RemoteRun func(ctx context.Context, env *Env, args []string) int
// Run executes the command and returns the process exit code. args are
// the arguments after the command name.
Run func(ctx context.Context, env *Env, args []string) int
}
Command is one `remohop` subcommand.
type Env ¶
type Env struct {
// Target is the remote peer the command addresses: a 9-digit ID, a saved
// alias, or a direct host:port. Empty for local commands.
Target string
Globals Globals
Stdin io.Reader
Stdout io.Writer
Stderr io.Writer
// DialFunc connects to a target. The dispatcher fills it with Dialer;
// tests substitute a function returning a peertest connection.
DialFunc func(ctx context.Context, env *Env) (*peer.Conn, error)
// contains filtered or unexported fields
}
Env is what a command runs with.