Skip to main content

Hooks

Hooks let you run external shell commands, compliance loggers, or code formatters automatically whenever OpsCode invokes a tool. You can configure hooks globally for all sessions or commit project-specific hooks to your repository.

Hook configuration scopes​

OpsCode loads hook definitions from two locations and executes both when present:

PathScopeTrust behavior
~/.opscode/hooks.jsonGlobal (all sessions)Always trusted
.opscode/hooks.jsonProject-levelPrompts for approval on first run, or pass --trust-project-hooks in CI/CD

hooks.json format​

Each entry under "hooks" maps a tool name to a shell command. Use ${input.<field>} placeholders to interpolate the tool's runtime arguments:

.opscode/hooks.json
{
"hooks": {
"Bash": {
"command": "echo 'Shell: ${input.command}' >> /tmp/opscode-audit.log"
},
"Write": {
"command": "echo 'File created: ${input.file_path}' >> /tmp/opscode-audit.log"
},
"Edit": {
"command": "echo 'File edited: ${input.file_path}' >> /tmp/opscode-audit.log"
}
}
}

Supported tool events​

Hook keyTriggered byAvailable ${input.*} fields
BashShell command execution (execute)command, timeout
WriteFile creation or overwrite (write_file)file_path, content
EditFile modification (edit_file)file_path, old_string, new_string, replace_all
ReadFile read (read_file)file_path, offset, limit
GlobFile path pattern search (glob)pattern, path
GrepContent search (grep)pattern, path, glob, output_mode
LSDirectory listing (ls)path
mcp__<server>__<tool>Any MCP tool callTool-specific schema arguments

Common recipes​

Auto-format Terraform and OpenTofu files on save​

Automatically run tofu fmt or terraform fmt whenever OpsCode writes or edits a .tf or .tofu file:

.opscode/hooks.json
{
"hooks": {
"Write": {
"command": "if echo '${input.file_path}' | grep -qE '\\.(tf|tofu)$'; then tofu fmt '${input.file_path}' 2>/dev/null || terraform fmt '${input.file_path}' 2>/dev/null || true; fi"
},
"Edit": {
"command": "if echo '${input.file_path}' | grep -qE '\\.(tf|tofu)$'; then tofu fmt '${input.file_path}' 2>/dev/null || terraform fmt '${input.file_path}' 2>/dev/null || true; fi"
}
}
}

Append an immutable audit log​

Record every shell command and file mutation with a UTC timestamp:

~/.opscode/hooks.json
{
"hooks": {
"Bash": {
"command": "echo \"$(date -u +%Y-%m-%dT%H:%M:%SZ) EXEC: ${input.command}\" >> ~/.opscode/audit.log"
},
"Write": {
"command": "echo \"$(date -u +%Y-%m-%dT%H:%M:%SZ) WRITE: ${input.file_path}\" >> ~/.opscode/audit.log"
},
"Edit": {
"command": "echo \"$(date -u +%Y-%m-%dT%H:%M:%SZ) EDIT: ${input.file_path}\" >> ~/.opscode/audit.log"
}
}
}

Send Slack notifications on infrastructure plans​

Notify a Slack channel whenever terraform plan or tofu plan runs:

.opscode/hooks.json
{
"hooks": {
"Bash": {
"command": "if echo '${input.command}' | grep -qE '(terraform|tofu) plan'; then curl -s -X POST -H 'Content-type: application/json' --data '{\"text\":\"OpsCode executed an infrastructure plan\"}' \"$SLACK_WEBHOOK_URL\"; fi"
}
}
}