Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions astro.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -24,31 +24,32 @@ export default defineConfig({
],
sidebar: [
{
label: "Getting Started",
label: "Getting started",
items: [
{ label: "Installation & Setup", slug: "guides/getting-started" },
{ label: "Next Steps", slug: "guides/next-steps" },
{ label: "Installation & setup", slug: "guides/getting-started" },
{ label: "Next steps", slug: "guides/next-steps" },
{
label: "Shell Autocompletion",
label: "Shell autocompletion",
slug: "guides/shell-autocompletion",
},
],
},
{
label: "Guides",
items: [
{ label: "Writing Commands", slug: "guides/writing-commands" },
{ label: "Command Execution", slug: "guides/command-execution" },
{ label: "Importing & Overriding", slug: "guides/importing" },
{ label: "Environment", slug: "guides/environment" },
{ label: "Writing commands", slug: "guides/writing-commands" },
{ label: "Command execution", slug: "guides/command-execution" },
{ label: "Importing & overriding", slug: "guides/importing" },
{ label: "Environment variables", slug: "guides/environment" },
],
},
{
label: "Reference",
items: [
{ label: "CLI Reference", slug: "reference/cli" },
{ label: "Environment Reference", slug: "reference/environment" },
{ label: "YAML Schema", slug: "reference/yaml-schema" },
{ label: "CLI reference", slug: "reference/cli" },
{ label: "Environment reference", slug: "reference/environment" },
{ label: "YAML schema", slug: "reference/yaml-schema" },
{ label: "Security", slug: "security" },
],
},
],
Expand Down
109 changes: 38 additions & 71 deletions src/content/docs/guides/command-execution.mdx
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
---
title: Command Execution
title: Command execution
description: How Ahoy finds and runs your commands
---

import { Aside } from "@astrojs/starlight/components";

## How Ahoy finds your config
## Finding your config

When you run an Ahoy command, it:
When you run a command, Ahoy:

1. Checks for a `--file` (or `-f`) flag to use a specific config file
2. Otherwise, searches upward from the current directory for `.ahoy.yml`
3. Changes to the directory containing that file
4. Executes your command relative to that directory
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.

Expand Down Expand Up @@ -40,8 +40,7 @@ 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 verbatim, flags
included, so wrapping a tool that takes its own options needs nothing special:
Everything after the command name reaches your command unchanged, flags included, so wrapping a tool with its own options just works:

```yaml
commands:
Expand All @@ -53,14 +52,18 @@ commands:
ahoy phpunit --filter=MyTest --stop-on-failure
```

Ahoy's own flags - `--verbose`, `--file` and friends - are only recognised
before the command name, so they never collide with your command's options.
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:

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

## Interactive commands

All three standard streams - stdin, stdout and stderr - are inherited from the
process that invoked Ahoy. Ahoy does not read, buffer or copy them, so a command
runs exactly as it would if you had typed it yourself:
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:

```yaml
commands:
Expand All @@ -73,18 +76,11 @@ commands:
cmd: mysql -u$DB_USER -p$DB_PASS $DB_NAME
```

When you run these from a terminal, the inherited streams are attached to a
TTY, so they run fully interactively - you get a real prompt, readline support,
and piping all work as expected. Invoked from a script or CI job, where the
streams are pipes or files instead, the command sees exactly what it would see
there without Ahoy.
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 TUI commands
### Full-screen tools and TUI commands

Because the streams are the inherited descriptors rather than a copy of them,
commands can ask about them directly - whether they are a TTY at all, and if so
the terminal size - and get a true answer. When Ahoy is run from a terminal that
is what full-screen and menu-driven tools need in order to draw:
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:

```yaml
commands:
Expand All @@ -97,47 +93,20 @@ commands:
cmd: docker compose logs --tail=1000 | fzf --tac
```

Tools built on [Bubble Tea](https://github.com/charmbracelet/bubbletea) - `gum`,
`glow` and similar - draw on stderr specifically, as do progress bars, spinners
and anything that repaints in place.
This covers tools built on [Bubble Tea](https://github.com/charmbracelet/bubbletea) such as `gum` and `glow`, which draw on stderr, along with progress bars and spinners.

<Aside type="caution" title="Fixed in v3.0.1">
Ahoy v3.0.0 routed stderr through an internal pipe. Bytes still reached your
terminal, but the terminal itself was no longer on the other end, so these
tools measured a 0x0 screen and drew nothing - or, with `gum` on Bubble Tea
v2, crashed outright. If you are on v3.0.0 and an interactive command renders
blank, upgrade. See [issue #180](https://github.com/ahoy-cli/ahoy/issues/180).
Ahoy v3.0.0 sent stderr through an internal pipe. These tools then saw a 0x0 screen and drew
nothing, and `gum` on Bubble Tea v2 crashed. If an interactive command shows a blank screen on
v3.0.0, upgrade. See [issue #180](https://github.com/ahoy-cli/ahoy/issues/180).
</Aside>

Output is not buffered either - a long-running command's output appears as it

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

keep this in

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Restored in fa4f313 as its own paragraph after the v3.0.1 note, and removed the shorter copy I'd merged into the paragraph above.

is produced, not in a burst when the command exits.

## Chaining commands

You can call other Ahoy commands from within a command:

```yaml
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
```

## Custom entrypoints

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

```yaml
ahoyapi: v2
Expand Down Expand Up @@ -165,26 +134,24 @@ commands:
cmd: print("Hello from Python!")
```

Entrypoint template variables:
The entrypoint can use these placeholders:

- `{{cmd}}` - replaced with the command body
- `{{name}}` - replaced with the command name (also available as `$0`)
- `{{cmd}}`: the command body
- `{{name}}`: the command name (also available as `$0`)

## Exit codes

Ahoy passes exit codes through unchanged. If a command exits non-zero, Ahoy exits non-zero. This works naturally with shell pipelines:
Ahoy exits with your command's exit code, so you can chain commands in the shell:

```bash
ahoy test && ahoy deploy
```

## Controlling execution within a command

Because the command body is bash, the usual operators control what runs next:
Inside a command, the usual bash operators decide what runs next:

- `&&` - run the next command only if the previous one succeeded (exit `0`)
- `||` - run the next command only if the previous one failed (non-zero exit)
- `;` - always run the next command
- `&&` 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

```yaml
commands:
Expand All @@ -196,9 +163,9 @@ commands:
echo "4 - Do this no matter what"
```

## Debugging and gotchas
## Troubleshooting

- **Use `--verbose` to see what actually ran.** It prints the resolved command before executing it, which is the fastest way to diagnose an unexpected result.
- **Commands run in a subshell.** Environment variables are copied in, but nothing a command does can change your parent shell - it cannot change your current directory or set variables for your session.
- **Quotes can be tricky.** When passing a command through to a subcommand, quotes can be lost along the way. Check `--verbose` first, then experiment with single versus double quotes. Prefer `cmd: |` for anything complex so YAML quoting stays out of the way.
- **Check your YAML if Ahoy complains.** Indentation and structure errors surface as parse failures; `ahoy config validate` reports them along with unsupported fields.
- 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.
77 changes: 30 additions & 47 deletions src/content/docs/guides/environment.mdx
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
---
title: Environment Variables
description: Loading environment files and managing configuration in Ahoy
title: Environment variables
description: Load environment files into your Ahoy commands
---

import { Aside, Badge } from "@astrojs/starlight/components";

Ahoy can load environment variables from files before running your commands, keeping configuration and secrets out of your YAML.
Ahoy can load variables from `.env` files before it runs a command, so settings and secrets stay out of your `.ahoy.yml`.

## A simple example <Badge text="v2.3.0+" variant="tip" />

Create a `.env` file alongside your `.ahoy.yml`:
Create a `.env` file next to your `.ahoy.yml`:

```bash
DB_USER=root
DB_PASS=secret
DB_NAME=myapp
```

Reference it in your config:
Point to it in your config:

```yaml
ahoyapi: v2
Expand All @@ -30,11 +30,11 @@ commands:
cmd: mysql -u$DB_USER -p$DB_PASS $DB_NAME
```

The path is relative to the `.ahoy.yml` file, not your working directory.
The path is relative to `.ahoy.yml`, not to the directory you're in.

## Global and per-command files
## For every command, or just one

The top-level `env` applies to every command. Individual commands can also specify their own `env`, which layers on top:
An `env` at the top of the file applies to every command. A command can have its own `env` as well, which is loaded after the top-level one:

```yaml
ahoyapi: v2
Expand All @@ -44,13 +44,13 @@ env: .env # Loaded for every command
commands:
deploy:
usage: Deploy the application
env: .env.deploy # Also loaded for this command only
env: .env.deploy # Also loaded, for this command only
cmd: ./deploy.sh
```

## Multiple files <Badge text="v2.5.0+" variant="note" />

Both global and per-command `env` fields accept an array. Files are loaded in order - later files override earlier ones:
Either `env` can be a list. Files load in order, and later files override earlier ones:

```yaml
ahoyapi: v2
Expand All @@ -67,70 +67,53 @@ commands:
cmd: ./deploy.sh staging
```

A file like `.env.local` is often absent, for example when you start a project
from a checked-out repo with an `.ahoy.yml` file in it. From v3.0.1 Ahoy
reports this, rather than passing over it in silence, so please start marking such entries
as optional - see below.
## Files that might not exist <Badge text="v3.0.1+" variant="success" />

## Files that may be absent <Badge text="v3.0.1+" variant="success" />

A missing env file never stops a command - it is skipped, the variables it
would have set stay empty, and the command runs. What changed in v3.0.1 is
that Ahoy now shows a warning:
If an env file is missing, Ahoy still runs the command. From v3.0.1 it also prints a warning, so a typo in a filename doesn't leave you wondering why a variable is empty:

```
[warn] environment file '.env.local' not found, continuing without it. Add 'optional: true' to that entry if it is meant to be absent.
```

Every entry is required by default, so a typo in a filename surfaces instead of
leaving you to work out why a variable is empty. For the files that are meant
to be missing - a personal `.env.local` that only some developers have - write
the entry as a mapping and mark it `optional: true`:
Some files are meant to be missing, such as a personal `.env.local` that only some people have. Mark those `optional: true` and Ahoy won't warn about them:

```yaml
env:
- .env.base # Required: warns when absent
- .env.base # Warns if missing
- path: .env.local
optional: true # Silent when absent
optional: true # Quiet if missing
```

This is the same `optional` you may already use on
[imports](/guides/importing/): simply meaning that the file is not expected to
always present.

The warning goes to stderr, so it never contaminates a command's stdout when
you pipe it. `ahoy config validate` distinguishes the two as well, listing a
missing required file with `❌` and a missing optional one with `➖ ... -
missing, marked optional`.
This works like `optional` on [imports](/guides/importing/). The warning goes to stderr, so it won't end up in piped output. `ahoy config validate` lists a missing required file with `❌` and a missing optional one with `➖`.

<Aside type="caution" title="Requires v3.0.1">
The mapping form is not understood by earlier versions - Ahoy v2.5.0 fails to
parse it with `cannot unmarshal !!seq into string`. Keep the plain string form
if your `.ahoy.yml` is shared with anyone still on v2 or v3.0.0.
Older versions can't read the `path`/`optional` form. Ahoy v2.5.0 fails with
`cannot unmarshal !!seq into string`. Stick to plain filenames if anyone using your `.ahoy.yml` is
on v2 or v3.0.0.
</Aside>

## Precedence
## Which value wins

From lowest to highest:
If the same variable is set in more than one place, the later one wins:

1. Your shell's existing environment
2. Global env files (in order)
3. Command-specific env files (in order)
1. Your shell's environment
2. Top-level env files, in order
3. The command's own env files, in order

## Runtime variables <Badge text="v3" variant="success" />
## Variables Ahoy sets <Badge text="v3" variant="success" />

Ahoy automatically injects two variables into every command:
Ahoy adds two variables to every command:

| Variable | Value |
| ------------------- | --------------------------------- |
| `AHOY_COMMAND_NAME` | The name of the command being run |
| `AHOY_CMD` | Path to the ahoy binary |

These are useful for scripts that call other ahoy commands, or need to know their own name.
Use them in scripts that call other Ahoy commands or need to know their own name.

## Keep secrets out of version control
## Keep secrets out of git

Add your real `.env` files to `.gitignore` and commit a `.env.example` with placeholder values:
Add your real `.env` files to `.gitignore`, and commit a `.env.example` with placeholder values instead:

```
# .gitignore
Expand All @@ -146,4 +129,4 @@ DB_PASS=your_password
DB_NAME=your_database
```

For the complete format specification and precedence details, see the [Environment Reference](/reference/environment).
The [Environment reference](/reference/environment) has the full file format.
Loading