run

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2026 License: MIT Imports: 19 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package run executes a document's blocks in their envs.

Index

Constants

This section is empty.

Variables

View Source
var ErrNoConfirm = errors.New("block needs confirmation")

ErrNoConfirm is returned when a block needs confirming and the run has no way to ask.

View Source
var ErrNoTTY = errors.New("block needs a terminal (tty) and there is none")

ErrNoTTY is a tty block's error when the run has no terminal to give it.

View Source
var ErrRestart = errors.New("the first tty block needs mdsh to start again")

ErrRestart is Sync's answer when the document gained the first tty block after the session's shells started without terminals of their own.

Functions

func Lines

func Lines(data []byte, from, to int) ([]byte, error)

Lines returns lines from through to of data, numbered from 1 and inclusive. from 0 is the first line and to 0 the last.

func LookupAt

func LookupAt(lookup doc.Lookup, d *doc.Document, results []*Result, pos int) doc.Lookup

LookupAt wraps lookup for refs whose meaning depends on where they are used. {{$?}} is the exit status of the last block before pos that ran; {{$ran?}} is whether the last runnable block before pos ran. For a block's run= that is the block before it; for show= and the other display conditions, pos is past the block, so it is the block itself. With no such block they keep a stored value or read as unknown.

func NewLookup

func NewLookup(d *doc.Document, results []*Result, environ []string) doc.Lookup

NewLookup resolves refs for conditions and placeholders: {{$VAR}} from environ (unless environ is nil), {{#block/env(VAR)}}, {{#block/?}} and {{#block/ran?}} from the block's result, and otherwise from a value a placeholder stored in the file. It reads results as they are when it is called, so it sees blocks that finish after it is made. {{$?}} and {{$ran?}} depend on where they are used; see LookupAt.

func OutKind

func OutKind(b *doc.Block) string

OutKind returns the block's output kind: text, markdown, image, screenshot, or recording for a tty block.

func Runnable

func Runnable(b *doc.Block) bool

Runnable reports whether mdsh knows how to run b.

func Shell

func Shell(d *doc.Document) string

Shell is the shell d's blocks run in: the front matter's shell, else zsh if any block is a zsh block, else "" for the default /bin/sh.

Types

type Answer

type Answer int

Answer is a reply to Confirm.

const (
	Stop Answer = iota
	Go
	Skip
)

type Event

type Event struct {
	Kind   EventKind
	Block  *doc.Block
	Data   []byte
	Result *Result
}

Event reports progress. Data is set for Stdout and Stderr, Result for Done.

type EventKind

type EventKind int

EventKind says what an Event reports.

const (
	Started EventKind = iota
	Stdout
	Stderr
	Done
)

type Options

type Options struct {
	// File is the document's path, used for MDSH_FILE and image paths.
	// Empty when the document came from stdin.
	File string
	// Dir is the working directory for every env. Defaults to File's
	// directory, or the current directory.
	Dir string
	// LinkDir is where image links are relative to. Defaults to Dir; set
	// it when the rendered markdown is saved somewhere else.
	LinkDir string
	// Args are the positional parameters, $1 and on.
	Args []string
	// Environ is the caller's environment. Defaults to os.Environ().
	Environ []string
	// Shell overrides the front matter's shell.
	Shell string
	// StopOnError stops at the first failing block, whatever the front
	// matter says.
	StopOnError bool
	// Only runs the named block and the earlier blocks in its env chain.
	Only string
	// OnEvent, if set, is called as blocks start, write and finish.
	OnEvent func(Event)
	// Step asks Confirm before every block that would run, not just
	// those with the confirm attribute.
	Step bool
	// Confirm is asked before each block that has the confirm attribute,
	// or every block in step mode. Its answer runs the block, skips it
	// (keeping any output saved in the file) or stops the run there.
	// If Confirm is nil, the run stops at the first such block with
	// ErrNoConfirm, since nobody can answer.
	Confirm func(*doc.Block) Answer
	// TTY is the terminal that blocks marked tty run on. If it is nil,
	// such blocks fail with ErrNoTTY.
	TTY *os.File
	// TakeTTY, if set, is called before a tty block uses TTY, so a front
	// end that draws on the terminal can step aside. The block runs once
	// it returns, and calls the function it returned when done with TTY.
	TakeTTY func(*doc.Block) (giveBack func())
}

Options control a run.

type Result

type Result struct {
	Block   *doc.Block
	Ran     bool // false if skipped or not reached
	Skipped bool // skip, run= false, unknown language, not selected by Only, or skipped at the prompt
	Stdout  []byte
	Stderr  []byte
	Status  int
	// Exited is true when the block ended its shell (exit, or a timeout).
	Exited bool
	Err    error
	// Image is the markdown image target when the block has out=image:
	// a path relative to the document, or a data: URI.
	Image string
	// Vars holds the variables placeholders ask for, as the block left
	// them. Unset variables are empty.
	Vars map[string]string
	// Declined is true when the block asked for confirmation and the run
	// stopped there instead.
	Declined bool
}

Result is what one block did.

func Includes

func Includes(d *doc.Document, opts Options) []*Result

Includes reads the files d's include blocks name. It returns one result per block, like Run, with only the include blocks filled in: Stdout holds the selected lines. A relative path is relative to opts.Dir, or opts.File's directory.

func Run

func Run(ctx context.Context, d *doc.Document, opts Options) ([]*Result, error)

Run executes d's blocks in order and returns one result per block. It stops early when a block ends its shell, or when a block fails and on_error is stop. Blocks after the stop are returned with Ran false.

func (*Result) ExitStatus

func (r *Result) ExitStatus() int

ExitStatus is the block's exit status, or 1 when mdsh failed to run it (r.Err) and there is no status.

func (*Result) Failed

func (r *Result) Failed() bool

Failed reports whether the block ran and did not succeed.

type Session added in v1.1.0

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

Session runs a document's blocks, keeping each env's shell alive from one run to the next. Run uses one to run the whole document; an interactive front end uses one to run blocks as they are picked.

A Session runs one thing at a time: don't call RunFrom or RunBlock while another call is running.

func NewSession added in v1.1.0

func NewSession(d *doc.Document, opts Options) (*Session, error)

NewSession reads d's include blocks and readies its envs. No shell is started until a block runs in it. Close the session when done.

func (*Session) Close added in v1.1.0

func (s *Session) Close()

Close ends the session's shells.

func (*Session) Results added in v1.1.0

func (s *Session) Results() []*Result

Results returns one result per block, in order. A block run again gets a new result, so hold on to the slice only while nothing runs.

func (*Session) RunBlock added in v1.1.0

func (s *Session) RunBlock(ctx context.Context, i int) (*Result, error)

RunBlock runs block i alone and returns its new result. It runs the block even if it is marked skip, its run= condition is false or Only leaves it out, since running it is what was asked for. A confirm block is still confirmed first; if the answer is not Go, the old result stays and RunBlock returns it. An include block reads its file again. It returns an error if the block can't run at all.

func (*Session) RunFrom added in v1.1.0

func (s *Session) RunFrom(ctx context.Context, from int) error

RunFrom runs the blocks from index from to the end, the way Run does: it honors skip, run=, Only, confirm and step mode, and stops when a block ends its shell or fails with on_error stop. Blocks it runs again get new results.

func (*Session) Sync added in v1.1.0

func (s *Session) Sync() error

Sync catches the session up with changes to the document since it started: blocks appended, as by doc.AppendBlock, and blocks edited in place, as by doc.Document.SetBlock. Call it while nothing runs.

Jump to

Keyboard shortcuts

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