Documentation
¶
Overview ¶
Package repl implements a generic interactive read-eval-print loop over an HCL expression engine. Each input line is parsed as a single HCL expression (hclsyntax.ParseExpression) and evaluated against an *hcl.EvalContext supplied by a Host; results are echoed HCL-style and numbered into a "_" / "_N" history.
The loop, prompt/result numbering, session bindings, meta-command dispatch, multi-line accumulation, value formatting, and tab-completion are all generic. A Host supplies the language/runtime specifics: the parent eval context for a given input (so a host may inject its own functions, ambient values, or a per-eval "ctx" object and open a trace span), the completion context, and the reserved-name policy. Heavy host concerns — logging, tracing — stay in the Host so this package depends only on readline, HCL, and cty.
functy's own CLI drives this over its baseline context (see NewStaticHost); a richer host (e.g. Vinculum) layers its live configuration on top.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DefaultHistoryPath ¶
DefaultHistoryPath returns a conventional readline history location for an application: $XDG_STATE_HOME/<app>/history when XDG_STATE_HOME is set, else ~/.<app>_history. It returns "" (history disabled) if no location is usable.
Types ¶
type Host ¶
type Host interface {
// EvalContext returns the parent eval context for evaluating src, plus a
// finish func run when that input's evaluation completes (e.g. to end a trace
// span). finish may be nil. The Session layers session bindings (_, _N, and
// named bindings) as a child of parent before evaluating. ctx is tied to the
// session lifetime (cancelled on exit / SIGTERM) and is suitable as a span
// parent.
EvalContext(ctx context.Context, src string) (parent *hcl.EvalContext, finish func(), err error)
// CompletionContext returns the context whose variables and functions are the
// tab-completion candidates. It should be cheap and side-effect-free (no trace
// spans): it is called per keystroke. It typically returns the same
// variables/functions a user can reference, including any per-eval "ctx".
CompletionContext(ctx context.Context) (*hcl.EvalContext, error)
// Reserved reports whether name may not be bound as a session variable. The
// Session already reserves "_" and the numbered "_N"; a host adds its own
// (e.g. "ctx" and built-in top-level namespaces).
Reserved(name string) bool
}
Host supplies the runtime specifics the generic loop depends on. A minimal static host is available via NewStaticHost; richer hosts implement it directly.
func NewStaticHost ¶
func NewStaticHost(ctx *hcl.EvalContext) Host
NewStaticHost returns a Host that evaluates against ctx unchanged. ctx must be non-nil and carry the Functions (and any Variables) available to the REPL.
type MetaCommand ¶
type MetaCommand struct {
Names []string
Summary string // shown in :help
Run func(args []string, errOut io.Writer) (exit bool)
}
MetaCommand is a host-provided extension to the base ":"-command set. Its Names are the accepted spellings (each beginning with ':'); Run receives the whitespace-split arguments that follow the command word and the session's diagnostic writer (for usage/error messages), and returns true to exit the REPL.
type Options ¶
type Options struct {
// Banner is printed once at startup, before a generic usage hint. Empty
// prints only the hint.
Banner string
// HistoryPath is the readline history file; "" disables persistence.
HistoryPath string
// Meta are additional meta-commands (e.g. log control) beyond the built-ins.
Meta []MetaCommand
// OnStart is invoked once after the line editor is live, with the editor's
// redraw-aware writer. A host can point its async log output there so writes
// erase and redraw the prompt cleanly. Nil if unused.
OnStart func(refresh io.Writer)
// OnDetach is invoked once during teardown, before the editor is closed, so a
// host can repoint async output away from the (about-to-close) editor writer.
OnDetach func()
// Out and ErrOut override the result and diagnostic streams (used by tests).
// When nil they default to the editor's stdout and os.Stderr.
Out io.Writer
ErrOut io.Writer
}
Options configures a Session. All fields are optional.