Documentation
¶
Overview ¶
Package hooks installs and removes the caprock-hook shim in Claude Code's user-level settings.json, non-destructively. See .ai/03-contracts.md § Hook shim.
Index ¶
- Constants
- Variables
- func DefaultSettingsPath() (string, error)
- func Install(settingsPath, shimPath string) (backup string, err error)
- func InstallStatusline(settingsPath, cmdPath string) (backup string, err error)
- func ListBackups(settingsPath string) []string
- func MarshalIndent(v any) ([]byte, error)
- func ParseOrdered(data []byte) (any, error)
- func Restore(settingsPath, backupPath string) error
- func ShellCommand(p string) string
- func StatuslineInstalled(settingsPath, cmdPath string) (ours bool, present bool, err error)
- func Uninstall(settingsPath, shimPath string) (removed bool, err error)
- func UninstallStatusline(settingsPath, cmdPath string) (removed bool, err error)
- type Object
- type Status
Constants ¶
const ShimTimeoutSeconds = 5
ShimTimeoutSeconds is the per-hook timeout written into settings.json. The shim itself exits within ~1s; 5s is headroom for a slow disk, and it bounds the Phase 2 Stop request-response (5s) too.
Variables ¶
var Events = []string{"SessionStart", "UserPromptSubmit", "PreToolUse", "PostToolUse", "Stop", "SubagentStop", "SessionEnd", "PreCompact", "StopFailure"}
Events the shim registers for (Phase 0). Order is the order written.
Functions ¶
func DefaultSettingsPath ¶
DefaultSettingsPath is ~/.claude/settings.json.
func Install ¶
Install merges our entries into settings.json (creating the file if absent), backing the original up once before the first write. Returns the backup path ("" when nothing was written or no original existed).
func InstallStatusline ¶ added in v0.5.0
InstallStatusline sets settings.json's statusLine to our command, backing the file up once. It refuses to clobber a statusLine the user set to something else — callers should check StatuslineInstalled and only offer to install when nothing (or our own) is present. Returns the backup path (empty if no change).
func ListBackups ¶ added in v0.18.0
ListBackups returns the Caprock backups of settings.json, oldest first.
func MarshalIndent ¶
MarshalIndent renders an ordered value with 2-space indentation, matching how Claude Code writes settings.json.
func ParseOrdered ¶
ParseOrdered decodes JSON preserving object key order.
func Restore ¶ added in v0.18.0
Restore copies a backup back over settings.json. It takes the backup path explicitly rather than picking one, because choosing which snapshot a user wants is a decision only they can make. Before overwriting it snapshots the current file, so a restore is itself undoable.
func ShellCommand ¶ added in v0.27.4
func StatuslineInstalled ¶ added in v0.5.0
StatuslineInstalled reports whether settings.json already has a statusLine command that points at our binary (`… statusline`). A different statusLine command (the user's own) is left alone and reported as not-ours.
func Uninstall ¶
Uninstall removes only entries whose command points at our shim, and drops empty containers it leaves behind. Other hooks are untouched. Returns whether anything was removed.
func UninstallStatusline ¶ added in v0.5.0
UninstallStatusline removes the statusLine only if it is ours, leaving a user-set statusLine untouched. Returns whether anything was removed.
Types ¶
type Object ¶
type Object struct {
Keys []string
Values map[string]any // values are: *Object, []any, string, float64, bool, nil
}
Object is a JSON object with stable key order.
func (*Object) MarshalJSON ¶
MarshalJSON writes keys in insertion order.
type Status ¶
type Status struct {
SettingsPath string `json:"settings_path"`
ShimPath string `json:"shim_path"`
Installed []string `json:"installed"` // events with our entry present
Missing []string `json:"missing"` // events from Events without our entry
ShimExists bool `json:"shim_exists"`
}
Status describes what is registered.