wasmshell

package
v0.24.0 Latest Latest
Warning

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

Go to latest
Published: Oct 8, 2026 License: Apache-2.0 Imports: 26 Imported by: 0

Documentation

Index

Constants

View Source
const ExitCommandNotFound = 127

ExitCommandNotFound is the exit code for a command (or subcommand/flag) the browser shell can't run. Callers treat it as "needs a real environment" — the signal the escalation path acts on.

Variables

View Source
var CmdRegistry = map[string]CommandFunc{}

CmdRegistry maps command names to their implementations.

View Source
var GhSubcommands = map[string]bool{
	"repo clone":  true,
	"repo view":   true,
	"pr list":     true,
	"pr view":     true,
	"pr checkout": true,
	"pr create":   true,
	"pr diff":     true,
	"pr status":   true,
	"auth status": true,
}

GhSubcommands is the set of `gh` subcommands (as space-joined paths, e.g. "pr checkout") the WASM shell answers in-browser. Anything else stays a 127 so the transactional escalation path can take it to a container.

View Source
var GitSubcommands = map[string]bool{

	"status":       true,
	"diff":         true,
	"log":          true,
	"show":         true,
	"branch":       true,
	"remote":       true,
	"ls-files":     true,
	"rev-list":     true,
	"rev-parse":    true,
	"blame":        true,
	"describe":     true,
	"shortlog":     true,
	"tag":          true,
	"symbolic-ref": true,
	"config":       true,
	"cat-file":     true,

	"add":      true,
	"commit":   true,
	"checkout": true,
	"switch":   true,
	"fetch":    true,
	"push":     true,
	"pull":     true,
	"init":     true,
	"clone":    true,
	"rm":       true,
	"mv":       true,
}

GitSubcommands is the set of git subcommands the WASM shell answers in-browser via the registered GitExecutor (isomorphic-git). It covers the read-only commands plus the local/remote write commands a user runs in the browser IDE (add, commit, checkout, branch, fetch, push, pull, init, clone, rm, mv). Subcommands not listed here (rebase, merge, reset, stash, …) stay a 127 so the transactional escalation path can take them to a container.

View Source
var ReadOnlyGitSubcommands = map[string]bool{
	"status":       true,
	"diff":         true,
	"log":          true,
	"show":         true,
	"branch":       true,
	"remote":       true,
	"ls-files":     true,
	"rev-list":     true,
	"rev-parse":    true,
	"blame":        true,
	"describe":     true,
	"shortlog":     true,
	"tag":          true,
	"symbolic-ref": true,
	"config":       true,
	"cat-file":     true,
}

ReadOnlyGitSubcommands is the read-only subset of GitSubcommands. Kept for callers (and tests) that need the narrower set.

Functions

func AutoCompleteJSON

func AutoCompleteJSON(input string) string

AutoCompleteJSON returns completions as a JSON string.

func DeleteFilePath

func DeleteFilePath(path string) error

DeleteFilePath deletes a file.

func ExpandGlobs

func ExpandGlobs(args []string) []string

ExpandGlobs expands glob patterns in arguments.

func GlobCompletion

func GlobCompletion(prefix string) []string

GlobCompletion returns files matching a glob prefix for tab completion.

func HistorySearch

func HistorySearch(prefix string) []string

HistorySearch searches command history for a prefix.

func JSONResult

func JSONResult(r CmdResult) string

JSONResult marshals a CmdResult to JSON string.

func ListDirEntryJSON

func ListDirEntryJSON(path string) (string, error)

ListDirEntryJSON returns the JSON representation of directory entries.

func ParseRedirects

func ParseRedirects(line string) (string, []string, string, string, string, bool, bool)

ParseRedirects extracts command name, args, and redirect operators from a line. Returns: name, args, stdinFile, stdoutFile, stderrFile, appendStdout, appendStderr

func ReadDirCompat added in v0.16.25

func ReadDirCompat(dir string) ([]os.DirEntry, error)

func ReadFileContent

func ReadFileContent(path string) (string, error)

ReadFileContent reads a file and returns its content as a string.

func RecursiveSync

func RecursiveSync(dir string)

RecursiveSync removes deleted files from the store by walking the filesystem. This is a best-effort reconciliation after operations like rm -rf.

func RegisterGhExecutor added in v0.22.3

func RegisterGhExecutor(fn GhExecutor)

RegisterGhExecutor installs the executor backing the shell's "gh" command. Passing nil uninstalls it.

func RegisterGitExecutor added in v0.17.20

func RegisterGitExecutor(fn GitExecutor)

RegisterGitExecutor installs the executor backing the shell's "git" command. Passing nil uninstalls it.

func ResetHistory

func ResetHistory()

ResetHistory clears the command history (useful for testing).

func ResolvePath

func ResolvePath(p string) string

ResolvePath expands ~ and makes relative paths absolute against cwd.

func SetShellEnv

func SetShellEnv(e *Env)

SetShellEnv replaces the global shell environment (useful for testing).

func SetStoreWriter

func SetStoreWriter(s StoreWriter)

SetStoreWriter sets the StoreWriter used for persisting files.

func SplitChains added in v0.17.20

func SplitChains(input string) []chainSegment

SplitChains splits a command line by unquoted && / || / ; separators. Quotes and escapes suppress splitting, and separator characters inside quotes are preserved in the segment text. The first segment carries an empty op; later ones carry the operator that joins them to the previous segment.

func SplitPipeline

func SplitPipeline(input string) []string

SplitPipeline splits a command line by unquoted pipe characters.

func SyncDeleteFile

func SyncDeleteFile(path string) error

SyncDeleteFile removes a file from the filesystem (MEMFS) and syncs to the StoreWriter.

func SyncWriteFile

func SyncWriteFile(path, content string) error

SyncWriteFile writes content to the filesystem (MEMFS) and syncs to the StoreWriter.

func Tokenize

func Tokenize(line string, keepQuotes bool) []string

Tokenize splits a command line into tokens, respecting quotes and escapes.

func WalkCompat added in v0.16.25

func WalkCompat(root string, fn func(path string, info os.FileInfo, err error) error) error

func WriteFileContent

func WriteFileContent(path, content string) error

WriteFileContent writes content to a file.

Types

type AutoCompleteResult

type AutoCompleteResult struct {
	Completions []string `json:"completions"`
}

AutoCompleteResult holds the JSON response for tab completion.

func AutoComplete

func AutoComplete(input string) AutoCompleteResult

AutoComplete performs tab completion for the given input. It handles: - Command name completion (first token) - File/directory path completion (subsequent tokens) - History search (for empty input or ! commands)

type CmdResult

type CmdResult struct {
	Stdout   string   `json:"stdout"`
	Stderr   string   `json:"stderr"`
	ExitCode int      `json:"exitCode"`
	Output   []OutSeg `json:"output"`
}

CmdResult holds the result of a command execution. Output, when set, is Stdout and Stderr interleaved in the order they were written; a result without it wrote its stdout before its stderr.

func ParseAndExecute

func ParseAndExecute(input string) CmdResult

ParseAndExecute runs a shell command line or script: lists, pipelines, redirections, control flow, functions and expansions, over the built-in commands.

type CommandFunc

type CommandFunc func(args []string, stdin string) CmdResult

CommandFunc is the type for all command implementations.

type DirEntry

type DirEntry struct {
	Name string `json:"name"`
	Type string `json:"type"` // "file" or "dir"
	Size int64  `json:"size"`
	Mode uint32 `json:"mode"`
}

DirEntry represents a directory listing entry.

type Env

type Env struct {
	Vars map[string]string
}

Env holds the shell environment variables.

var ShellEnv *Env

ShellEnv holds the global shell environment.

func NewEnv

func NewEnv() *Env

NewEnv creates a new Env with default shell variables.

func (*Env) All

func (e *Env) All() map[string]string

All returns a copy of all environment variables.

func (*Env) Get

func (e *Env) Get(key string) string

Get returns the value of an environment variable, falling back to the OS environment.

func (*Env) Set

func (e *Env) Set(key, value string)

Set sets an environment variable in both the Env map and the OS environment.

type GhExecutor added in v0.22.3

type GhExecutor func(subcommand string, args []string) CmdResult

GhExecutor runs a `gh` subcommand (e.g. "pr list") with the raw args and returns its result. The browser build installs an implementation backed by isomorphic-git (clone/checkout) plus the GitHub REST API over the same egress path git uses; without one, `gh` reports itself unavailable (exit 127) — the same signal the escalation surface treats as "run in container".

type GitExecutor added in v0.17.20

type GitExecutor func(subcommand string, args []string) CmdResult

GitExecutor runs a read-only git subcommand ("status", "diff", "log", …) with the raw subcommand args and returns its result. The browser build installs an implementation backed by isomorphic-git; without one, the shell reports git as unavailable (exit 127) — the same signal a WASM shell gives for commands it cannot run, which the escalation surface treats as "run in container".

type OutSeg added in v0.21.0

type OutSeg struct {
	Err  bool   `json:"err,omitempty"`
	Text string `json:"text"`
}

OutSeg is a run of output written to one stream.

type StoreWriter

type StoreWriter interface {
	SaveFile(path, content string)
	DeleteFile(path string)
}

StoreWriter is the interface for persisting file writes/deletes to external storage. In the WASM build, this is backed by IndexedDB. In tests, a no-op is used.

Jump to

Keyboard shortcuts

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