shell

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Apr 10, 2026 License: MIT Imports: 28 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrExit = errors.New("exit")

Functions

func BuiltinPluginNames

func BuiltinPluginNames() []string

BuiltinPluginNames returns the names of built-in commands available without external plugins (native Go implementations registered at startup).

func DefaultCommands

func DefaultCommands() []string

DefaultCommands returns all command names available in a default shell (static builtins + default native plugins) without creating a Shell instance.

func MarshalSnapshot

func MarshalSnapshot(snap *Snapshot) ([]byte, error)

MarshalSnapshot serialises snap to JSON.

func RestoreSnapshot

func RestoreSnapshot(snap *Snapshot) (afero.Fs, string, error)

RestoreSnapshot creates a new in-memory filesystem from snap and returns it together with the saved working directory.

Types

type BuiltinFunc

type BuiltinFunc func(ctx context.Context, args []string) error

BuiltinFunc is a native Go command implementation. ctx carries the interp.HandlerContext — use interp.HandlerCtx(ctx) for per-command I/O. For virtual FS access use shell.ShellCtx(ctx).

type CompleteResult

type CompleteResult struct {
	Completions []string `json:"completions"`
	Prefix      string   `json:"prefix"` // input text before the completing token
	Token       string   `json:"token"`  // the partial token being completed
}

CompleteResult holds the tab-completion results for a given input.

func Complete

func Complete(input string, cursor int, fs afero.Fs, cwd string, commands []string) CompleteResult

Complete computes tab completions for input at the given cursor position. commands is the full list of known command names used for command-position completion.

type Option

type Option func(*Shell)

Option is a configuration function for the Shell.

func WithAliases

func WithAliases(aliases map[string]string) Option

WithAliases pre-seeds the alias table (e.g. loaded from a config file).

func WithAllowExternalCommands

func WithAllowExternalCommands(allow bool) Option

WithAllowExternalCommands permits falling back to real OS executables when a command is not found among builtins or plugins. By default this is false, which keeps all execution inside the virtual sandbox.

func WithBuiltin

func WithBuiltin(name string, fn BuiltinFunc) Option

WithBuiltin registers a raw function as a native shell command. Prefer WithPlugin when you want to attach metadata (description, usage).

func WithCwd

func WithCwd(cwd string) Option

WithCwd sets the initial working directory.

func WithDisabledPlugins

func WithDisabledPlugins(names ...string) Option

WithDisabledPlugins removes the named plugins from the shell. Works for both native (builtin) and WASM plugins. Applied after defaults, so it can suppress defaultNativePlugins entries.

func WithEnv

func WithEnv(env map[string]string) Option

WithEnv sets initial environment variables.

func WithFS

func WithFS(fs afero.Fs) Option

WithFS sets the afero Filesystem to use.

func WithInheritEnv

func WithInheritEnv(inherit bool) Option

WithInheritEnv controls whether the shell inherits the parent process's environment variables. When false, only explicitly set variables (via WithEnv) are available. Defaults to true for CLI use; should be false in server mode to prevent leaking host secrets to remote users.

func WithPlugin

func WithPlugin(p plugins.Plugin) Option

WithPlugin registers a Plugin as a native shell command. Native plugins take priority over WASM plugins with the same name.

func WithPluginBytes

func WithPluginBytes(name string, wasm []byte) Option

WithPluginBytes registers a WASM plugin directly from bytes, without needing a file in /memsh/plugins/. The plugin must export command_name() and run().

func WithPluginFilter

func WithPluginFilter(names []string) Option

WithPluginFilter sets an allowlist of WASM plugin names to load during discovery (/memsh/plugins/ and ~/.memsh/plugins/). When the list is non-empty, only plugins whose names appear in it are loaded. Plugins registered explicitly via WithPlugin or WithPluginBytes are unaffected.

func WithStdIO

func WithStdIO(in io.Reader, out, err io.Writer) Option

WithStdIO sets the standard input, output, and error streams.

func WithWASMEnabled

func WithWASMEnabled(enabled bool) Option

WithWASMEnabled controls whether the wazero WASM plugin runtime is started. Pass false to skip all WASM plugin loading (faster startup, no wazero overhead).

type Shell

type Shell struct {
	// contains filtered or unexported fields
}

func New

func New(opts ...Option) (*Shell, error)

func (*Shell) Close

func (s *Shell) Close() error

func (*Shell) Commands

func (s *Shell) Commands() []string

Commands returns all command names known to this shell instance: the static builtin list, registered native plugins, and loaded WASM plugins.

func (*Shell) Cwd

func (s *Shell) Cwd() string

func (*Shell) FS

func (s *Shell) FS() afero.Fs

func (*Shell) ListDir

func (s *Shell) ListDir(path string) ([]string, error)

func (*Shell) LoadMemshrc

func (s *Shell) LoadMemshrc(ctx context.Context) error

LoadMemshrc sources /.memshrc from the virtual filesystem if it exists. Errors are non-fatal — a missing file is silently ignored.

func (*Shell) Register

func (s *Shell) Register(p plugins.Plugin)

func (*Shell) RegisteredPlugins

func (s *Shell) RegisteredPlugins() []string

func (*Shell) Run

func (s *Shell) Run(ctx context.Context, script string) error

type Snapshot

type Snapshot struct {
	Version int            `json:"version"`
	Cwd     string         `json:"cwd"`
	Files   []SnapshotFile `json:"files"`
}

Snapshot is a portable, JSON-serialisable representation of a memsh session: the complete virtual filesystem plus the working directory. File content is stored as raw bytes; the standard encoding/json package encodes []byte as base64 automatically.

func TakeSnapshot

func TakeSnapshot(fs afero.Fs, cwd string) (*Snapshot, error)

TakeSnapshot walks fs and returns a Snapshot containing every file and directory, together with cwd. It never follows symbolic links.

func UnmarshalSnapshot

func UnmarshalSnapshot(data []byte) (*Snapshot, error)

UnmarshalSnapshot parses JSON produced by MarshalSnapshot.

type SnapshotFile

type SnapshotFile struct {
	Path    string      `json:"path"`
	Mode    os.FileMode `json:"mode"`
	IsDir   bool        `json:"is_dir,omitempty"`
	Content []byte      `json:"content,omitempty"` // nil/absent for directories
}

SnapshotFile represents one node (regular file or directory) in the snapshot.

Directories

Path Synopsis
Package cron provides cron expression parsing, matching, and crontab file parsing for the memsh virtual shell scheduler, backed by robfig/cron/v3.
Package cron provides cron expression parsing, matching, and crontab file parsing for the memsh virtual shell scheduler, backed by robfig/cron/v3.
Package plugins defines the Plugin interface and shell context helpers shared by the shell runtime and all native plugin implementations.
Package plugins defines the Plugin interface and shell context helpers shared by the shell runtime and all native plugin implementations.
native
Package native contains the built-in native Go plugins shipped with memsh.
Package native contains the built-in native Go plugins shipped with memsh.

Jump to

Keyboard shortcuts

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