hook

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 30, 2026 License: MIT Imports: 10 Imported by: 0 modules, 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 hook reads a shell command line the agent would run, for terrace's PreToolUse hook in Claude Code (spec 5.6): the raw applies, destroys and state writes of Terragrunt, OpenTofu and Terraform it refuses, the `terrace approve` it passes to Claude Code's prompt only in the one form the ask rule matches, the writes to terrace's and Claude Code's files it asks about, and the lines it can't read, which it refuses. It reads every command the line would run: pipelines, lists, subshells, substitutions, the scripts given to a shell, to eval or on a shell's stdin, and the command a wrapper such as env, command, exec, sudo, timeout, `mise exec` or `find -exec` runs. A command it doesn't know is read from every word that may start one, but for the refusals it tells for certain, from a word that may be no command at all; a command that only reads, as echo or grep, runs nothing among its words. It reads each path from every folder the line may be in, as cd leaves it. What git alone can tell a git command writes, it leaves a question for git.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Guarded

func Guarded(p string) bool

Guarded reports whether p, a path from the top of a work tree, names a guarded file by its name.

Types

type ApproveForm

type ApproveForm struct {
	Code seam.Code
}

ApproveForm is `terrace approve` in a form Claude Code's ask rule doesn't match, or terrace given approve's --question, which only the hook pins (5.6.4, 5.6.5); and the code it names, empty where it names none that reads as one.

type GitKind

type GitKind int

GitKind is what a GitQuestion asks of git.

const (
	// GitDiffers: the files that differ across the revision range Rev,
	// which a move across it writes.
	GitDiffers GitKind = iota
	// GitChanged: the files whose content the work tree changes from the
	// commit Rev, which a move that discards its changes, or a restore from
	// Rev, writes.
	GitChanged
	// GitUnstaged: the files whose content the work tree changes from the
	// index, which a restore from the index writes.
	GitUnstaged
	// GitUntracked: the files git neither tracks nor ignores, which a
	// stash -u takes away.
	GitUntracked
	// GitOthers: the files git doesn't track, ignored ones too, which a
	// stash -a takes away.
	GitOthers
	// GitTree: the files of the commit Rev, as a stash's untracked files,
	// which its apply puts back.
	GitTree
	// GitCheckout: git checkout's lone operand Rev. Where it's a commit,
	// the checkout moves the work tree there from From, as Move gives;
	// where it's neither a commit nor a path git tracks, git may make a
	// branch of a remote's of that name, which writes Files.
	GitCheckout
	// GitApplies: the files the patch file Rev touches that Guarded tells.
	GitApplies
	// GitPull: a pull of the commit Rev onto From, which writes across
	// Move where git's config says the pull rebases, and Merge where it
	// merges.
	GitPull
)

The questions the hook asks of git.

type GitQuestion

type GitQuestion struct {
	Kind GitKind
	Dir  string
	// Rev is what Kind asks about: a revision range, a commit, or a patch
	// file.
	Rev string
	// From is the commit a GitCheckout or GitPull moves the work tree
	// from: HEAD, or the one an earlier git command of the line moved it
	// to, as it stands before the line.
	From string
	// Gate is a commit the question is asked only where it exists, empty
	// for none: where it doesn't, the command fails, writing nothing.
	Gate string
	// Specs are the pathspecs of the guarded files git is asked about, each
	// read from the top of Dir's work tree.
	Specs []string
	// Files are the guarded files the line may write where git can't
	// answer.
	Files []string
}

GitQuestion asks git, run in the folder Dir, which guarded files a git command the line runs writes, as its Kind asks.

func (GitQuestion) Merge

func (q GitQuestion) Merge() string

Merge is the revision range a GitPull that merges writes across: what Rev brings since it parted from From.

func (GitQuestion) Move

func (q GitQuestion) Move() string

Move is the revision range a move from From to Rev writes across: a GitCheckout's checkout of a commit, or a GitPull that rebases.

type Place

type Place struct {
	Cwd, Root, Top string
	Home           string
	Settings       []string
	// Resolve is an absolute path as the file system resolves it: the
	// links of its longest prefix that exists followed. Nil takes a path as
	// it's written.
	Resolve func(string) string
}

Place is where a command line runs: the folder it runs in, terrace's root, empty where it holds no terrace.hcl, and the top of the git work tree it runs in, each a physical path; Home, the person's home folder, which ~ names; and Settings, the other settings files Claude Code reads there, by their full paths. A path the line names is resolved against them, through Resolve where it's given; one the hook can't resolve is told by its name.

type Reason

type Reason string

Reason is why the hook can't read a command.

const (
	// ReasonParse: the line doesn't parse as a shell's.
	ReasonParse Reason = "parse"
	// ReasonComputedCommand: a command's name is computed, globbed or
	// braced, so any binary may run.
	ReasonComputedCommand Reason = "computed_command"
	// ReasonComputedScript: a script given to a shell, eval, source or
	// trap is computed.
	ReasonComputedScript Reason = "computed_script"
	// ReasonPipedScript: a shell reads its script from a pipe.
	ReasonPipedScript Reason = "piped_script"
	// ReasonComputedWords: a guarded tool's or terrace's words are
	// computed or globbed, so its command can't be told.
	ReasonComputedWords Reason = "computed_words"
	// ReasonXargs: a guarded tool, terrace or a shell takes words from
	// xargs or parallel.
	ReasonXargs Reason = "xargs"
	// ReasonGlobQualifier: a word holds an extended glob, which zsh reads
	// as a glob qualifier that may run a command.
	ReasonGlobQualifier Reason = "glob_qualifier"
	// ReasonNested: scripts nest within scripts deeper than the hook
	// reads.
	ReasonNested Reason = "nested"
)

The reasons the hook can't read a command.

type Refusal

type Refusal interface {
	// contains filtered or unexported methods
}

Refusal is one thing a command line runs that the hook refuses: a ToolCommand, an ApproveForm or an Unreadable.

type ToolCommand

type ToolCommand struct {
	Tool, Command string
}

ToolCommand is a raw tool command the hook refuses (5.6.3): the tool, as terraform, tofu or terragrunt, and its command, as apply, destroy, "apply -destroy", "state rm" or "init -migrate-state".

type Unreadable

type Unreadable struct {
	Reason  Reason
	Command string
	Detail  string
}

Unreadable is a command the hook can't read, and why (5.6.10). Command is the command it's about, as the line names it; Detail is the parser's own message for a line that doesn't parse.

type Verdict

type Verdict struct {
	// Approve is the run of a line that's `terrace approve <code>` alone,
	// the one form Claude Code's ask rule matches (5.6.5); empty otherwise.
	Approve seam.Code
	// Refusals are what the line runs that the hook refuses, in the order
	// the line runs them.
	Refusals []Refusal
	// Writes are the guarded files the line may write, each once, as the
	// line names them, or for a glob or a git restore, the files it may
	// reach (5.6.8).
	Writes []string
	// Git are what the line's git commands may write that only git can
	// tell: each a question whose answer, the guarded files git names, the
	// line may write too.
	Git []GitQuestion
}

Verdict is what the hook makes of a command line. Refusals outrank writes, and a line with either approves nothing.

func Read

func Read(line string, at Place) Verdict

Read is what the hook makes of the command line line, run at at.

Jump to

Keyboard shortcuts

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