Hooks (Pre/Post)
Hooks let you run code before or after any command without modifying the command handler itself.
Before Hook
@stack.commands.up.before
def pre_up() -> None:
print("Checking prerequisites...")
check_env_vars()
After Hook
@stack.commands.up.after
def post_up() -> None:
print("Deployment complete — notifying Slack")
notify_slack()
Full Example
from orche import Stack
stack = Stack(compose_files=["docker-compose.yml"])
@stack.commands.up.before
def pre_up() -> None:
print("Running pre-flight checks...")
@stack.commands.up
def up() -> None:
stack.build().up()
@stack.commands.up.after
def post_up() -> None:
print("Services are live.")
orche up
# Running pre-flight checks...
# [build / up output]
# Services are live.
Hooks on Custom Commands
For commands registered via register(), save the handle and decorate from it:
deploy = stack.commands.register("deploy")
@deploy.before
def check_env() -> None: ...
@deploy
def deploy_handler() -> None: ...
See Custom Commands for a full example.
Multiple Hooks
You can register multiple before/after hooks. They run in registration order.
@stack.commands.up.before
def check_docker() -> None: ...
@stack.commands.up.before
def check_env() -> None: ...
Hooks and Command Options
Hooks receive the options of the command they are attached to, but only the ones they declare as parameters. Values are the same resolved ones the handler gets, defaults included.
@stack.commands.register("deploy")
def deploy(env: str = "dev") -> None:
...
@stack.commands.register("deploy").before
def announce(env: str) -> None:
print(f"Deploying to {env}")
See Command Options for how options are parsed.