Skip to content

Add directory-local config and wildcard TERMINUS_USER matching#2827

Open
georgestephanis wants to merge 2 commits into
pantheon-systems:4.xfrom
georgestephanis:add/multiple-session-support
Open

Add directory-local config and wildcard TERMINUS_USER matching#2827
georgestephanis wants to merge 2 commits into
pantheon-systems:4.xfrom
georgestephanis:add/multiple-session-support

Conversation

@georgestephanis
Copy link
Copy Markdown

Summary

This PR adds two small quality-of-life features for users who work with multiple Pantheon accounts (e.g. agency developers juggling client accounts).

1. Directory-local .terminus config files

A new DirectoryConfig class walks up from the current working directory to the filesystem root, looking for a .terminus YAML file. If one is found, it is loaded into the config chain between ~/.terminus/config.yml and the existing .env / environment-variable layers.

This lets you drop a .terminus file in any project root:

TERMINUS_USER: george@client-account.com
TERMINUS_ORG: client-org-name

Any terminus command run from that directory or any of its subdirectories will automatically pick up those settings — no flags or environment variables required.

Config precedence (lowest → highest):
constants.yml~/.terminus/config.yml.terminus (walk-up) → .env → environment variables

2. Wildcard/partial matching for TERMINUS_USER

Authorizer::ensureLogin() now supports fnmatch-style patterns in TERMINUS_USER, so a shared .terminus file can target the right account without hardcoding a specific email:

# Match any saved token whose email is at this domain
TERMINUS_USER: "@client-domain.com"

Supported forms:

Value Behaviour
george@example.com Exact match (existing behaviour, unchanged)
@example.com Matches any saved token at that domain (normalized to *@example.com)
*@example.com Same, explicit form
george@* Matches any domain for that username

If the pattern matches zero tokens, a clear error directs the user to auth:login. If it matches more than one, an error asks them to be more specific — useful when a team member has tokens for multiple accounts at the same domain.

The TERMINUS_USER check is now evaluated before the single-token auto-select, so an explicit pattern always takes priority.

Changed files

  • src/Config/DirectoryConfig.php (new) — walks up directory tree to find and load .terminus
  • src/Terminus.php — inserts DirectoryConfig into the factory() config chain
  • src/Hooks/Authorizer.php — adds isEmailPattern() and matchTokenByPattern() helpers

Testing

All existing unit tests pass. Because both features interact with the filesystem and the saved-token store rather than the Pantheon API, they are best exercised manually or via future functional tests:

# Verify directory config is picked up
mkdir /tmp/terminus-test && echo "TERMINUS_USER: you@example.com" > /tmp/terminus-test/.terminus
cd /tmp/terminus-test && terminus auth:whoami

# Verify walk-up from a subdirectory
mkdir /tmp/terminus-test/sub && cd /tmp/terminus-test/sub && terminus auth:whoami

# Verify wildcard matching (with two tokens saved at the same domain)
echo "TERMINUS_USER: '@example.com'" > /tmp/terminus-test/.terminus
cd /tmp/terminus-test && terminus auth:whoami

AI assistance disclosure: The design and implementation of this PR were driven by George Stephanis. Code was written with the assistance of Claude Code (Anthropic), using the Claude Sonnet 4.6 model (claude-sonnet-4-6). The iterative design conversation — including a deliberate simplification pass that removed an over-engineered "named profiles" system in favour of the lighter approach here — was conducted interactively with a human in the loop at every decision point.

Introduce DirectoryConfig which walks up from a starting directory (typically getcwd()) to locate a .terminus YAML file, parses it, and merges its values into the Terminus configuration (setting the config source name). Wire DirectoryConfig into Terminus so the runtime will extend its Defaults/Yaml/user-home config chain with any per-project .terminus settings before loading DotEnv/Env configs.
Allow TERMINUS_USER to be a wildcard or shorthand pattern (e.g. "*@domain.com" or "@domain.com") when selecting a saved token. The token-selection logic now checks the configured user first, matches patterns via fnmatch (with '@domain.com' normalized to '*@domain.com'), and falls back to the single saved token if no user is configured. Added helper methods isEmailPattern() and matchTokenByPattern() and improved error messages for zero or multiple matches.
@georgestephanis georgestephanis requested a review from a team as a code owner April 24, 2026 19:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant