9 "mvdan.cc/sh/moreinterp/coreutils"
10 "mvdan.cc/sh/v3/expand"
11 "mvdan.cc/sh/v3/interp"
12 "mvdan.cc/sh/v3/syntax"
15// RunOptions configures a single stateless shell execution via [Run].
17// The zero value is not useful; at minimum Command must be set. Stdin,
18// Stdout, and Stderr may be nil (nil readers/writers are treated as
19// empty/discard). BlockFuncs may be nil to disable block-list enforcement —
20// hooks use this to run user-authored commands with the same trust level as
22type RunOptions struct {
23 // Command is the shell source to parse and execute.
25 // Cwd is the working directory for the execution. Required: callers
26 // must supply a non-empty value. Run does not silently fall back to
27 // the Crush process cwd — hooks and the bash tool have different
28 // notions of "default" and each owns that decision.
30 // Env is the full environment visible to the command. The caller is
31 // responsible for inheriting from os.Environ() if that's desired.
33 // Stdin is the command's standard input. nil is equivalent to an empty
36 // Stdout receives the command's standard output. nil discards output.
38 // Stderr receives the command's standard error. nil discards output.
40 // BlockFuncs is an optional list of deny-list matchers applied before
41 // each command reaches the exec layer. nil disables blocking entirely.
42 BlockFuncs []BlockFunc
45// Run parses and executes a shell command using the same mvdan.cc/sh
46// interpreter stack that the stateful [Shell] type uses (builtins,
47// optional block list, optional Go coreutils). It is safe to call
48// concurrently from multiple goroutines: each call builds its own
49// [interp.Runner] and shares no state with other callers or with any
52// Errors returned from the command itself (non-zero exit, context
53// cancellation, parse failures) follow the same conventions as
54// [Shell.Exec]: inspect with [IsInterrupt] and [ExitCode].
55func Run(ctx context.Context, opts RunOptions) (err error) {
57 if r := recover(); r != nil {
58 err = fmt.Errorf("command execution panic: %v", r)
63 return fmt.Errorf("shell.Run: Cwd is required")
75 line, err := syntax.NewParser().Parse(strings.NewReader(opts.Command), "")
77 return fmt.Errorf("could not parse command: %w", err)
80 runner, err := newRunner(opts.Cwd, opts.Env, opts.Stdin, stdout, stderr, opts.BlockFuncs)
82 return fmt.Errorf("could not run command: %w", err)
85 return runner.Run(ctx, line)
88// newRunner constructs an [interp.Runner] configured with the standard
89// Crush handler stack. Shared by the stateless [Run] entrypoint and the
90// stateful [Shell] so the two surfaces cannot drift.
91func newRunner(cwd string, env []string, stdin io.Reader, stdout, stderr io.Writer, blockFuncs []BlockFunc) (*interp.Runner, error) {
93 interp.StdIO(stdin, stdout, stderr),
94 interp.Interactive(false),
95 interp.Env(expand.ListEnviron(env...)),
97 interp.ExecHandlers(standardHandlers(blockFuncs)...),
101// standardHandlers returns the exec-handler middleware chain used by both
102// [Run] and [Shell]. Order matters:
103// 1. builtins first (so Crush's in-process jq wins over any PATH binary);
104// 2. script dispatch (shebang / binary / shell-source for path-prefixed
105// argv[0], no-op for bare commands) — runs before the block list so
106// that deny rules see the already-resolved argv of anything the
107// script exec's rather than the outer path-prefixed wrapper;
109// 4. optional Go coreutils (only when useGoCoreUtils is on).
110func standardHandlers(blockFuncs []BlockFunc) []func(next interp.ExecHandlerFunc) interp.ExecHandlerFunc {
111 handlers := []func(next interp.ExecHandlerFunc) interp.ExecHandlerFunc{
113 scriptDispatchHandler(blockFuncs),
114 blockHandler(blockFuncs),
117 handlers = append(handlers, coreutils.ExecHandler)
122// builtinHandler returns middleware that dispatches recognized Crush
123// builtins to their in-process Go implementations. Currently: jq.
124func builtinHandler() func(next interp.ExecHandlerFunc) interp.ExecHandlerFunc {
125 return func(next interp.ExecHandlerFunc) interp.ExecHandlerFunc {
126 return func(ctx context.Context, args []string) error {
128 return next(ctx, args)
132 hc := interp.HandlerCtx(ctx)
133 return handleJQ(ctx, args, hc.Stdin, hc.Stdout, hc.Stderr)
135 return next(ctx, args)
141// blockHandler returns middleware that rejects commands matched by any of
142// the provided [BlockFunc]s before they reach the underlying exec path.
143// A nil or empty blockFuncs slice is a no-op.
144func blockHandler(blockFuncs []BlockFunc) func(next interp.ExecHandlerFunc) interp.ExecHandlerFunc {
145 return func(next interp.ExecHandlerFunc) interp.ExecHandlerFunc {
146 return func(ctx context.Context, args []string) error {
148 return next(ctx, args)
150 for _, blockFunc := range blockFuncs {
152 return fmt.Errorf("command is not allowed for security reasons: %q", args[0])
155 return next(ctx, args)
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list