$ show mvdan/sh
mvdan/sh is a comprehensive Go library and CLI toolchain for parsing, formatting, and interpreting shell scripts. It supports POSIX Shell, Bash, mksh, Zsh (experimental), and Bats dialects. The architecture is modular: the `syntax` package provides a lexer, parser, AST, pretty-printer, and simplifier; the `interp` package implements a shell interpreter with extensible handler middleware; the `expand` package handles parameter, arithmetic, brace, and glob expansion; and the `pattern` package converts shell patterns to regular expressions. Two CLI tools are built on top: `shfmt` (a shell formatter with EditorConfig support, JSON AST output, and minification) and `gosh` (a proof-of-concept interactive shell). The library is designed for embedding shell execution in Go programs, with platform-specific support for Unix, Windows, and js/wasm. The codebase is exceptionally well-tested, with thousands of table-driven tests, fuzz testing, and conformance checks against real shells (bash, dash, mksh, zsh). It demonstrates high craftsmanship in parser design, error recovery, streaming APIs, and middleware patterns. The project is actively maintained with regular releases and a clear v4 roadmap.
The LLM identified 4 files worth highlighting for reuse:
The core parser for shell syntax, supporting multiple dialects (Bash, POSIX, mksh, Zsh, Bats). Implements a recursive-descent parser with error recovery, streaming APIs via iter.Seq2, and functional options for configuration. Handles complex constructs like heredocs, arithmetic, parameter expansions, and case clauses. The parser is self-contained within the syntax package and can be reused in any Go program needing shell parsing.
Defines the handler middleware pattern for the interpreter, including ExecHandler, OpenHandler, StatHandler, ReadDirHandler, AccessHandler, and ProcSubstHandler. Each handler type is a function that can be chained via ExecHandlers (a slice of handlers). Default implementations are provided, and users can inject custom logic for command execution, file operations, and process substitution. This pattern is cleanly separated and could be extracted for use in other extensible execution engines.
Converts shell glob patterns (including extended glob operators like @(...), !(...)) to regular expressions. Supports modes for filenames, entire string matching, and extended operators. The implementation is small, self-contained, and well-tested. It can be reused independently for any application needing shell-style pattern matching in Go.
Implements a sparse array data structure for indexed arrays with holes, used by the interpreter for shell array variables. Provides SetIndexedElem, DeleteIndexedElem, and CanonicalIndexes operations. The implementation is efficient and self-contained, making it reusable for any Go project needing sparse array semantics.