Skip to content

Command execution

When you run a command, Ahoy:

  1. Uses the file given with --file (or -f), if there is one
  2. Otherwise looks for .ahoy.yml in the current directory, then in each parent directory
  3. Changes to the directory that file is in
  4. Runs your command from there

This means you can run ahoy from any subdirectory in your project and it always behaves consistently.

Arguments are passed to commands as bash positional parameters:

commands:
greet:
cmd: echo "Hello, $1!"
copy:
cmd: cp $1 $2
run-in-container:
cmd: docker-compose exec app "$@"
Terminal window
ahoy greet World # Hello, World!
ahoy copy src.txt dest.txt
ahoy run-in-container npm test

$@ passes all arguments through, which is handy for wrapper commands. Use $# to check the argument count, and $0 for the command name.

Everything after the command name reaches your command unchanged, flags included, so wrapping a tool with its own options just works:

commands:
phpunit:
cmd: docker compose exec app vendor/bin/phpunit "$@"
Terminal window
ahoy phpunit --filter=MyTest --stop-on-failure

Ahoy’s own flags, such as --verbose and --file, only count when they come before the command name, so they can’t clash with your command’s options.

Ahoy removes a -- only when it comes straight after the command name. Any other -- is passed to your command, which matters for tools that use their own separator:

Terminal window
ahoy k exec mypod -- ls /app # kubectl gets its --
ahoy run npm test -- --watch # npm passes --watch to the test script

Commands use the same stdin, stdout and stderr as the terminal or script that ran Ahoy. Ahoy doesn’t read, buffer or copy them, so a command behaves exactly as if you’d typed it yourself:

commands:
shell:
usage: Open a shell in the app container
cmd: docker-compose exec app bash
mysql:
usage: Open a MySQL shell
cmd: mysql -u$DB_USER -p$DB_PASS $DB_NAME

From a terminal you get a real prompt, line editing and working pipes. From a script or CI job, the command sees the same pipes or files it would see without Ahoy.

Full-screen and menu tools need to ask the terminal how big it is. Because Ahoy hands over the real terminal, they get the right answer and draw properly:

commands:
pick-env:
usage: Choose an environment to deploy to
cmd: gum choose staging production
logs:
usage: Browse container logs interactively
cmd: docker compose logs --tail=1000 | fzf --tac

This covers tools built on Bubble Tea such as gum and glow, which draw on stderr, along with progress bars and spinners.

Ahoy doesn’t buffer output either - a long-running command’s output appears as the command writes it, not in a burst when Ahoy exits. Some programs buffer their own output when it goes to a pipe or file, and Ahoy can’t change that.

You can call other Ahoy commands from within a command:

commands:
ci:
usage: Run the full CI pipeline locally
cmd: |
ahoy install
ahoy lint
ahoy test
install:
cmd: npm install
lint:
cmd: npm run lint
test:
cmd: npm test

By default Ahoy runs each command with bash -c '{{cmd}}'. Set entrypoint at the top of the file to use a different interpreter:

ahoyapi: v2
entrypoint:
- node
- "-e"
- "{{cmd}}"
commands:
hello:
cmd: console.log("Hello from Node.js!")
ahoyapi: v2
entrypoint:
- python3
- "-c"
- "{{cmd}}"
commands:
hello:
cmd: print("Hello from Python!")

The entrypoint can use these placeholders:

  • {{cmd}}: the command body
  • {{name}}: the command name (also available as $0)

Ahoy exits with 0 when your command succeeds and 1 when it fails, whatever code the command returned. That’s enough to chain commands in the shell:

Terminal window
ahoy test && ahoy deploy

Inside a command, the usual bash operators decide what runs next:

  • && runs the next command only if the previous one succeeded
  • || runs the next command only if the previous one failed
  • ; always runs the next command
commands:
only-run-1-2-and-4:
cmd: |
echo "1 - If this passes" &&
echo "2 - Then do this" ||
echo "3 - Or do this if 1 or 2 fails" ;
echo "4 - Do this no matter what"
  • Run with --verbose (before the command name) to see the exact command Ahoy ran.
  • Commands run in a subshell. They get a copy of your environment, but can’t change your current directory or set variables in your shell.
  • Quotes can get lost when one command passes arguments to another. Check --verbose first, then try swapping single and double quotes. Using cmd: | for anything complicated keeps YAML quoting out of the way.
  • If Ahoy can’t read your file, run ahoy config validate. It reports YAML mistakes and fields Ahoy doesn’t support.