cli

package
v1.0.10 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 2, 2026 License: AGPL-3.0 Imports: 17 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
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

View Source
var Dialer func(ctx context.Context, env *Env) (*peer.Conn, error)

Dialer is the production implementation of Env.DialFunc. internal/dial installs it from init().

View Source
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).

View Source
var Version = "dev"

Version is the build version, set with -ldflags "-X .../internal/cli.Version=...".

Functions

func FlagExit

func FlagExit(err error) int

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

func FlagHint(name string) string

FlagHint is the pointer to a command's full help that follows a one-line usage error.

func IsID

func IsID(s string) bool

IsID reports whether s is a well-formed 9-digit RemoHop ID.

func LooksLikeTarget

func LooksLikeTarget(s string) bool

LooksLikeTarget reports whether s names a host rather than a command: a 9-digit ID, HOST:PORT, or a saved alias.

func Main

func Main(ctx context.Context, args []string, stdin io.Reader, stdout, stderr io.Writer) int

Main parses args (without the program name), runs the selected command and returns the exit code.

func NewFlagSet

func NewFlagSet(env *Env, name, usage string) *flag.FlagSet

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

func NormalizeTarget(t string) string

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

func OneLine(s string) string

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

func Sanitize(s string) string

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.

func Commands

func Commands() []*Command

Commands lists every registered command sorted by name.

func Lookup

func Lookup(name string) *Command

Lookup finds a command by name.

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.

func (*Env) Close

func (e *Env) Close()

Close ends the session opened by Conn, if any.

func (*Env) Conn

func (e *Env) Conn(ctx context.Context) (*peer.Conn, error)

Conn returns the session with Target, establishing it on first use. Every call returns the same session.

func (*Env) Errorf

func (e *Env) Errorf(format string, args ...any)

Errorf prints a "remohop: ..." diagnostic to Stderr. The message is passed through Sanitize, because diagnostics routinely quote text that came from the remote peer or the rendezvous server.

type Globals

type Globals struct {
	Verbose  bool
	Signal   string // rendezvous server address override
	Password string // password for the target, overriding saved credentials
}

Globals are the flags accepted before the target or command.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL