Skip to content

Writing commands

An Ahoy command is a shell command with a name and some help text, written in YAML.

A command needs a name and a cmd. Everything else is optional, but a usage line is worth adding:

ahoyapi: v2
commands:
hello:
usage: Say hello to someone
cmd: echo "Hello, $1!"

The usage line is what people see when they run ahoy, so keep it short and clear.

Arguments arrive as normal bash positional parameters:

commands:
shell:
usage: Open a shell in a container
cmd: docker compose exec $1 bash
copy:
usage: Copy a file between containers
cmd: docker compose cp $1 $2
run:
usage: Run any command in the app container
cmd: docker compose exec app "$@"

$1, $2 for specific positions; $@ to forward everything. The last pattern is great for transparent wrappers:

Terminal window
ahoy run npm test -- --watch

Flags count as arguments. Everything you type after the command name reaches your command as typed, in the same order:

Terminal window
ahoy run npm test --watch
ahoy run chown -R www-data:www-data .

Command execution has the details, including how -- is handled.

For anything longer than one line, use a YAML block (|). It’s normal bash inside:

commands:
setup:
usage: Set up the project from scratch
cmd: |
echo "Installing dependencies..."
npm install
echo "Running migrations..."
./migrate.sh
echo "All done. Run 'ahoy start' to begin."

usage is the one line shown in the list. description is longer help, shown when someone runs ahoy help <command>:

commands:
deploy:
usage: Deploy the application
description: |
Deploys to the given environment, running tests and building
assets first. Needs .env.deploy to be set up.
Usage:
ahoy deploy staging
ahoy deploy production
The production deploy asks for confirmation unless CI=true
is set.
cmd: ./scripts/deploy.sh $1

Aliases are short names for a command. They’re listed next to the main name:

commands:
start-dev:
usage: Start the development environment
aliases: ["up", "start"]
cmd: docker compose up -d
open-shell:
usage: Open a shell in the app container
aliases: ["shell", "sh", "bash"]
cmd: docker compose exec app bash

Give the command a descriptive name, and add short aliases for the ones you type all day.

A command can run other Ahoy commands:

commands:
ci:
usage: Run the full CI pipeline locally
cmd: |
ahoy lint
ahoy test
ahoy build
fresh:
usage: Tear down and rebuild everything
cmd: |
ahoy down
docker compose build --no-cache
ahoy up
ahoy install

For commands that can’t be undone, add a confirm helper that asks first. This one also works in scripts and CI, where you answer in advance with AHOY_CONFIRM_RESPONSE=y:

commands:
db:reset:
usage: Reset the database to a clean state
cmd: |
ahoy confirm "This will destroy all database data. Continue?" || exit 0
docker compose exec db mysql -u"$DB_USER" -p"$DB_PASS" -e \
"DROP DATABASE IF EXISTS \`$DB_NAME\`; CREATE DATABASE \`$DB_NAME\`;"
echo "Database reset."
confirm:
hide: true
cmd: |
if [ -z "${AHOY_CONFIRM_RESPONSE}" ]; then
read -r -p ">> $1 [y/N] " response
else
response="${AHOY_CONFIRM_RESPONSE}"
echo ">> $1 [y/N] ${response}"
fi
response=$(printf '%s' "$response" | tr '[:upper:]' '[:lower:]')
if [ "$response" != "y" ] && [ "$response" != "yes" ]; then
echo "Cancelled."
exit 1
fi
# Answered in advance, so pause for a chance to press Ctrl+C.
if [ -n "${AHOY_CONFIRM_RESPONSE}" ] && [ "${AHOY_CONFIRM_WAIT_SKIP}" != "1" ]; then
sleep 3
fi

hide: true keeps a helper out of the command list. You can still run it.

Ahoy works well for wrapping long container commands:

commands:
drush:
usage: Run Drush in the CLI container
cmd: docker compose exec cli drush --root=/var/www/html/web "$@"
composer:
usage: Run Composer in the CLI container
cmd: docker compose exec cli composer "$@"
node:
usage: Run Node.js commands in the app container
cmd: docker compose exec app node "$@"

Now the team can run ahoy drush cache:rebuild or ahoy composer require vendor/package. The wrapped tool gets all its own flags, even ones Ahoy also uses:

Terminal window
ahoy drush --help # Drush's help
ahoy composer require --dev vendor/pkg
ahoy drush sql:query --extra=-A "SELECT 1"
ahoy node --version

Because --help goes to the wrapped tool, use ahoy help drush to see Ahoy’s help for the command.

Commands with their own env file v2.3.0+

Section titled “Commands with their own env file ”

Use env to load settings for one command:

commands:
deploy:
usage: Deploy to staging
env: .env.deploy
cmd: |
echo "Deploying to $DEPLOY_HOST..."
rsync -avz ./dist/ $DEPLOY_HOST:$DEPLOY_PATH

The Environment variables guide covers env files in full.

The repository has example files you can copy:

  • examples.ahoy.yml: around 30 commands for Docker, databases, testing, Drupal and deployment. ahoy config init downloads it, and it uses nearly every Ahoy feature, so it’s a good file to read.
  • docker.ahoy.yml: Docker Compose commands to import into your own config.
  • confirmation.ahoy.yml: the confirmation helper on its own, ready to import.

Every field is listed in the YAML schema.