Documentation
¶
Overview ¶
Package plugins defines the Plugin interface and shell context helpers shared by the shell runtime and all native plugin implementations.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func WithShellContext ¶
func WithShellContext(ctx context.Context, sc ShellContext) context.Context
WithShellContext returns a new context carrying sc. This is called by the shell's exec handler and is not typically needed by plugin authors.
Types ¶
type CommandInfo ¶
CommandInfo describes a registered command for help/which style introspection.
type Plugin ¶
type Plugin interface {
// Name returns the command name used to invoke the plugin (e.g. "jq").
Name() string
// Run executes the command. Use interp.HandlerCtx(ctx) for per-invocation
// stdin/stdout/stderr, and plugins.ShellCtx(ctx) for virtual FS access.
Run(ctx context.Context, args []string) error
}
Plugin is the interface for registering native Go commands with memsh.
Example — a minimal "hello" command:
type HelloPlugin struct{}
func (HelloPlugin) Name() string { return "hello" }
func (HelloPlugin) Run(ctx context.Context, args []string) error {
hc := interp.HandlerCtx(ctx) // per-invocation I/O (works in pipes)
sc := plugins.ShellCtx(ctx) // virtual FS + cwd
fmt.Fprintln(hc.Stdout, "Hello!")
return nil
}
sh, _ := shell.New(shell.WithPlugin(HelloPlugin{}))
type PluginInfo ¶
type PluginInfo interface {
Plugin
// Description returns a one-line summary shown in `plugin list`.
Description() string
// Usage returns a usage string, e.g. "base64 [-d] [data...]".
Usage() string
}
PluginInfo optionally extends Plugin with metadata shown by `plugin list`.
type ShellContext ¶
type ShellContext struct {
// FS is the virtual in-memory filesystem (afero.MemMapFs by default).
FS afero.Fs
// Cwd is the shell's current working directory.
Cwd string
// Env looks up a shell environment variable by key.
Env func(key string) string
// EnvAll returns a copy of the current shell environment.
EnvAll func() map[string]string
// SetEnv updates a shell environment variable.
SetEnv func(key, value string)
// ResolvePath converts a possibly-relative path to an absolute virtual path.
ResolvePath func(path string) string
// SetCwd changes the shell's working directory.
SetCwd func(path string) error
// Run executes a script in the current shell.
Run func(ctx context.Context, script string) error
// Exec executes a command argv through the shell command resolver.
Exec func(ctx context.Context, args []string) error
// Exit requests shell termination.
Exit func() error
// AliasLookup resolves a shell alias by name.
AliasLookup func(name string) (string, bool)
// CommandInfo returns metadata for a known command.
CommandInfo func(name string) (CommandInfo, bool)
// CommandNames returns the known command names.
CommandNames func() []string
// SourceFile sources a path or URL in the current shell context.
SourceFile func(ctx context.Context, path string) error
// NetworkDialContext provides policy-enforced outbound dialing for plugins.
NetworkDialContext func(ctx context.Context, network, address string) (net.Conn, error)
}
ShellContext provides access to shell-level state inside a Plugin.Run call. Retrieve it with ShellCtx(ctx).
func ShellCtx ¶
func ShellCtx(ctx context.Context) ShellContext
ShellCtx extracts the ShellContext injected by the shell's exec handler. Returns a zero ShellContext if called outside a shell invocation.