Documentation
¶
Overview ¶
Package run executes a document's blocks in their envs.
Index ¶
- Variables
- func Lines(data []byte, from, to int) ([]byte, error)
- func LookupAt(lookup doc.Lookup, d *doc.Document, results []*Result, pos int) doc.Lookup
- func NewLookup(d *doc.Document, results []*Result, environ []string) doc.Lookup
- func OutKind(b *doc.Block) string
- func Runnable(b *doc.Block) bool
- func Shell(d *doc.Document) string
- type Answer
- type Event
- type EventKind
- type Options
- type Result
Constants ¶
This section is empty.
Variables ¶
var ErrNoConfirm = errors.New("block needs confirmation")
ErrNoConfirm is returned when a block needs confirming and the run has no way to ask.
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.
Functions ¶
func Lines ¶
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 ¶
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 ¶
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.
Types ¶
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
}
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 ¶
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 ¶
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 ¶
ExitStatus is the block's exit status, or 1 when mdsh failed to run it (r.Err) and there is no status.