Documentation
¶
Overview ¶
Package hooks runs the scripts configured in <vault>/.nota/hooks.toml when core events happen, like git hooks:
[[hook]]
event = "inbox.added" # see events.Types
run = "sh scripts/file.sh" # run with the vault as working directory (sh -c; cmd /C on Windows)
mode = "transform" # "after" (default) or "transform"
timeout = "2s" # default 2s for transform, 30s for after
match = { folder = "Inbox", tag = "x" }
A hook gets the event as JSON on stdin (see Payload) and NOTA_VAULT, NOTA_EVENT, NOTA_NOTE and NOTA_HOOK in its environment.
After hooks run in the background and never block the write that triggered them. Transform hooks run only on note.created and inbox.added, before the write returns; they print a Patch as JSON on stdout, which is applied through the vault like any other edit. A failing, slow or confused hook is logged and leaves the note as it was.
Index ¶
Constants ¶
const ( DefaultTransformTimeout = 2 * time.Second DefaultAfterTimeout = 30 * time.Second )
Default timeouts.
const File = vault.MetaDirName + "/hooks.toml"
File is the vault-relative path of the hooks configuration.
const OriginPrefix = "hook:"
OriginPrefix starts the Event.Origin of changes made by a hook.
Variables ¶
var TransformEvents = []events.Type{events.NoteCreated, events.InboxAdded}
TransformEvents are the events transform hooks may run on.
Functions ¶
Types ¶
type Config ¶
type Config struct {
Hooks []Hook `toml:"hook"`
}
Config is the parsed hooks file.
type Exec ¶
type Exec func(ctx context.Context, h Hook, dir string, env []string, stdin []byte) (stdout []byte, err error)
Exec runs a hook's command with stdin and returns its stdout. The context carries the hook's timeout.
type Hook ¶
type Hook struct {
// ID names the hook in logs and event origins: its name, else "#N"
// for the N-th entry.
ID string `toml:"-" json:"id"`
Name string `toml:"name" json:"name,omitempty"`
Event events.Type `toml:"event" json:"event"`
Run string `toml:"run" json:"run"`
Mode Mode `toml:"mode" json:"mode"`
Timeout time.Duration `toml:"-" json:"-"`
Match Match `toml:"match" json:"match"`
RawTimeout string `toml:"timeout" json:"-"`
}
Hook is one [[hook]] entry.
type Match ¶
type Match struct {
// Folder matches notes in this folder and its subfolders.
Folder string `toml:"folder" json:"folder,omitempty"`
// Tag matches notes with this tag in frontmatter or body, nested tags included.
Tag string `toml:"tag" json:"tag,omitempty"`
}
Match filters the notes a hook runs for. Empty fields match everything.
type Options ¶
type Options struct {
// Dir is the hooks' working directory; the vault root when empty.
Dir string
// Logger gets failures (warn) and runs (debug); slog.Default when nil.
Logger *slog.Logger
// Exec runs commands; ShellExec when nil. Tests replace it.
Exec Exec
}
Options configure a Runner.
type Patch ¶
type Patch struct {
Set map[string]any `json:"set,omitempty"`
Unset []string `json:"unset,omitempty"`
Tags struct {
Add []string `json:"add,omitempty"`
Remove []string `json:"remove,omitempty"`
} `json:"tags,omitzero"`
// Folder moves the note; "/" is the vault root.
Folder string `json:"folder,omitempty"`
}
Patch is what a transform hook prints on stdout. Every field is optional; empty output means "no change".
func ParsePatch ¶
ParsePatch decodes a transform hook's output. Blank output is the empty patch; unknown fields are errors, so typos do not pass silently.
type Payload ¶
type Payload struct {
Event events.Type `json:"event"`
// Vault is the vault directory (also the hook's working directory).
Vault string `json:"vault"`
// Path is the note's vault-relative path (its new path for
// note.moved), empty for sync.completed.
Path string `json:"path,omitempty"`
OldPath string `json:"oldPath,omitempty"`
Trash string `json:"trash,omitempty"`
Folder string `json:"folder"`
Title string `json:"title,omitempty"`
// Tags are the note's frontmatter and inline tags.
Tags []string `json:"tags"`
Frontmatter map[string]any `json:"frontmatter"`
// Body is set for note.created, note.saved and inbox.added.
Body string `json:"body,omitempty"`
Time time.Time `json:"time"`
Origin string `json:"origin,omitempty"`
}
Payload is the JSON a hook reads on stdin.
type Result ¶
type Result struct {
Hook string `json:"hook"`
Mode Mode `json:"mode"`
Duration time.Duration `json:"-"`
Seconds float64 `json:"seconds"`
Output string `json:"output,omitempty"`
Patch *Patch `json:"patch,omitempty"`
Error string `json:"error,omitempty"`
}
Result is the outcome of one hook run.
type Runner ¶
type Runner struct {
// contains filtered or unexported fields
}
Runner runs a vault's hooks on its events.
func (*Runner) Attach ¶
Attach subscribes the runner to bus. Transform hooks run on the emitting goroutine, inside the write that triggered them; after hooks are started in the background. The returned func detaches it.
func (*Runner) Handle ¶
Handle runs the hooks for one event. A change a hook made never runs that hook again, and transform hooks never run on changes made by a transform hook, so hooks cannot loop.