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
- type Session
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.
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 ¶
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
// 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 ¶
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.
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
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) Results ¶ added in v1.1.0
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
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.