feat(cli): load credentials from .env#60465
Conversation
Read POSTHOG_CLI_HOST, POSTHOG_CLI_API_KEY, and POSTHOG_CLI_PROJECT_ID (plus legacy CLI_TOKEN / CLI_ENV_ID aliases) from ./.env and ./.env.local when not present in the process environment. Process env still wins; .env wins over .env.local. Bumps version to 0.7.14. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
Prompt To Fix All With AIFix the following 2 code review issues. Work through them one at a time, proposing concise fixes.
---
### Issue 1 of 2
cli/src/utils/auth.rs:80
`.env.local` receives the lowest priority here, which inverts the near-universal convention. `.env.local` is intended to be a machine-local override file (committed `.env` supplies defaults, un-committed `.env.local` lets each developer tailor their setup). With the current order, any key already present in `.env` is immovable by `.env.local` — making the file silently ineffective for the most common use-case (overriding a team-shared `.env`).
```suggestion
for source in [None, Some(local), Some(dotenv)] {
```
### Issue 2 of 2
cli/README.md:30
If the priority in `resolve_var` is corrected so that `.env.local` overrides `.env`, this documentation line needs to reflect the updated order.
```suggestion
These variables can also be set in a `.env` or `.env.local` file in the directory you run the CLI from. Precedence is process env → `.env.local` → `.env`.
```
Reviews (1): Last reviewed commit: "feat(cli): load credentials from .env an..." | Re-trigger Greptile |
There was a problem hiding this comment.
Pull request overview
This PR improves the posthog-cli developer experience by allowing credential resolution from local dotenv files when running the CLI from a repo, while preserving existing environment-variable and homedir-based auth flows.
Changes:
- Extend
EnvVarProvider::get_credentialsto resolve credentials from process env, then./.env, then./.env.local(with legacy alias support). - Add
dotenvydependency and bumpposthog-cliversion to0.7.14(includingCargo.lockupdates). - Document dotenv support in the CLI README and add a changelog entry.
Reviewed changes
Copilot reviewed 4 out of 5 changed files in this pull request and generated 6 comments.
Show a summary per file
| File | Description |
|---|---|
cli/src/utils/auth.rs |
Adds dotenv loading and precedence-based credential resolution logic. |
cli/README.md |
Documents .env/.env.local support and precedence for env-based auth. |
cli/CHANGELOG.md |
Adds a 0.7.14 entry describing the new dotenv credential behavior. |
cli/Cargo.toml |
Bumps version to 0.7.14 and adds dotenvy dependency. |
cli/Cargo.lock |
Locks dotenvy and updates the posthog-cli package version/deps. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
PR overviewAll previously flagged issues have been addressed. No open security concerns remain on this pull request. Security reviewNo open security issues remain on this pull request. Fixed/addressed: 2 · PR risk: 0/10 |
Pick credentials from a single source (process env, then .env.local, then .env) based on whether it supplies both POSTHOG_CLI_API_KEY and POSTHOG_CLI_PROJECT_ID (or their legacy aliases). Host is read only from that same source, so a stray POSTHOG_CLI_HOST in one file can no longer redirect a key supplied by another. Also: flip .env / .env.local precedence so .env.local wins (matches the near-universal convention), tidy load_dotenv, include legacy alias names in the not-found error, broaden the success log message, and add unit tests for try_source covering required vs optional fields, legacy aliases, and host isolation. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
Drop the implicit .env / .env.local lookup in the cwd. Add a top-level --env-file <PATH> flag instead, so users opt in to file-based credentials and point at exactly the file they want. Precedence: CLI args → process env → --env-file → credentials.json. Source resolution remains atomic — host is only read from the same source that supplied the api key and project id. Missing or invalid --env-file paths now fail loudly instead of being silently empty. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
Add two tests that exercise EnvVarProvider::get_credentials() against both real process env and a temp file: one asserts the process env wins when both supply credentials, the other asserts the file is consulted when the env vars are absent. Tests serialize on a local mutex so they don't clobber each other when run in parallel. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
|
Greptile encountered an error while reviewing this PR. Please reach out to support@greptile.com for assistance. |
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Co-authored-by: Claude Opus 4.7 <noreply@anthropic.com>
Problem
When running the CLI from a repo, you usually already have a
.env(or.env.local) file with the project's configuration. Today the CLI only readsPOSTHOG_CLI_*from the process environment, so contributors have to either log in interactively orexportthe vars by hand on every shell.Changes
EnvVarProvider::get_credentialsnow resolves each credential across three sources, in order:./.env./.env.localProcess env wins;
.envwins over.env.local. The accepted names are unchanged —POSTHOG_CLI_HOST,POSTHOG_CLI_API_KEY(+ legacyPOSTHOG_CLI_TOKEN),POSTHOG_CLI_PROJECT_ID(+ legacyPOSTHOG_CLI_ENV_ID). TheHomeDirProviderfallback is unchanged, so existing flows (interactive login, CI with exported env) keep working identically.Adds
dotenvyas a dep, bumpsposthog-clito0.7.14, updates the README's env-auth section, and adds aCHANGELOG.mdentry.How did you test this code?
I'm an agent. No manual testing — verified locally with
cargo checkandcargo build(both clean). No automated tests exist aroundEnvVarProvider; I didn't add any in this PR.Publish to changelog?
no
🤖 Agent context
Authored with Claude Code. Initial draft also added unprefixed
POSTHOG_HOST/POSTHOG_API_KEY/POSTHOG_PROJECT_IDvariants — rejected on review to keep the variable namespace strict toPOSTHOG_CLI_*(with the existing legacy aliases). README update added because the env-auth docs are the natural place to mention.envsupport.