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:
| Path | Scope | Trust behavior |
|---|---|---|
~/.opscode/hooks.json | Global (all sessions) | Always trusted |
.opscode/hooks.json | Project-level | Prompts 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 key | Triggered by | Available ${input.*} fields |
|---|---|---|
Bash | Shell command execution (execute) | command, timeout |
Write | File creation or overwrite (write_file) | file_path, content |
Edit | File modification (edit_file) | file_path, old_string, new_string, replace_all |
Read | File read (read_file) | file_path, offset, limit |
Glob | File path pattern search (glob) | pattern, path |
Grep | Content search (grep) | pattern, path, glob, output_mode |
LS | Directory listing (ls) | path |
mcp__<server>__<tool> | Any MCP tool call | Tool-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"
}
}
}