Skip to content

feat(cli): load credentials from .env#60465

Merged
ablaszkiewicz merged 8 commits into
masterfrom
cli/load-credentials-from-dotenv
May 29, 2026
Merged

feat(cli): load credentials from .env#60465
ablaszkiewicz merged 8 commits into
masterfrom
cli/load-credentials-from-dotenv

Conversation

@ablaszkiewicz
Copy link
Copy Markdown
Contributor

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 reads POSTHOG_CLI_* from the process environment, so contributors have to either log in interactively or export the vars by hand on every shell.

Changes

EnvVarProvider::get_credentials now resolves each credential across three sources, in order:

  1. process environment
  2. ./.env
  3. ./.env.local

Process env wins; .env wins over .env.local. The accepted names are unchanged — POSTHOG_CLI_HOST, POSTHOG_CLI_API_KEY (+ legacy POSTHOG_CLI_TOKEN), POSTHOG_CLI_PROJECT_ID (+ legacy POSTHOG_CLI_ENV_ID). The HomeDirProvider fallback is unchanged, so existing flows (interactive login, CI with exported env) keep working identically.

Adds dotenvy as a dep, bumps posthog-cli to 0.7.14, updates the README's env-auth section, and adds a CHANGELOG.md entry.

How did you test this code?

I'm an agent. No manual testing — verified locally with cargo check and cargo build (both clean). No automated tests exist around EnvVarProvider; 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_ID variants — rejected on review to keep the variable namespace strict to POSTHOG_CLI_* (with the existing legacy aliases). README update added because the env-auth docs are the natural place to mention .env support.

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>
Copilot AI review requested due to automatic review settings May 28, 2026 16:02
@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps Bot commented May 28, 2026

Prompt To Fix All With AI
Fix 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

Comment thread cli/src/utils/auth.rs Outdated
Comment thread cli/src/utils/auth.rs Outdated
Comment thread cli/README.md Outdated
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

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_credentials to resolve credentials from process env, then ./.env, then ./.env.local (with legacy alias support).
  • Add dotenvy dependency and bump posthog-cli version to 0.7.14 (including Cargo.lock updates).
  • 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.

Comment thread cli/src/utils/auth.rs Outdated
Comment thread cli/src/utils/auth.rs Outdated
Comment thread cli/src/utils/auth.rs Outdated
Comment thread cli/src/utils/auth.rs Outdated
Comment thread cli/CHANGELOG.md
Comment thread cli/src/utils/auth.rs Outdated
Comment thread cli/src/utils/auth.rs Outdated
@veria-ai
Copy link
Copy Markdown

veria-ai Bot commented May 28, 2026

PR overview

All previously flagged issues have been addressed. No open security concerns remain on this pull request.

Security review

No open security issues remain on this pull request.

Fixed/addressed: 2 · PR risk: 0/10

@ablaszkiewicz ablaszkiewicz marked this pull request as draft May 28, 2026 16:19
ablaszkiewicz and others added 3 commits May 28, 2026 18:26
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>
@ablaszkiewicz ablaszkiewicz changed the title feat(cli): load credentials from .env and .env.local feat(cli): load credentials from .env May 28, 2026
@ablaszkiewicz ablaszkiewicz marked this pull request as ready for review May 28, 2026 17:47
@ablaszkiewicz ablaszkiewicz requested review from a team, cat-ph and hpouillot May 28, 2026 17:47
@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps Bot commented May 28, 2026

Greptile encountered an error while reviewing this PR. Please reach out to support@greptile.com for assistance.

Comment thread cli/src/utils/auth.rs Outdated
Comment thread cli/src/utils/auth.rs Outdated
ablaszkiewicz and others added 2 commits May 29, 2026 11:28
@ablaszkiewicz ablaszkiewicz merged commit 02c6291 into master May 29, 2026
147 checks passed
@ablaszkiewicz ablaszkiewicz deleted the cli/load-credentials-from-dotenv branch May 29, 2026 10:28
mayteio pushed a commit that referenced this pull request May 29, 2026
Co-authored-by: Claude Opus 4.7 <noreply@anthropic.com>
@deployment-status-posthog
Copy link
Copy Markdown

deployment-status-posthog Bot commented May 29, 2026

Deploy status

Environment Status Deployed At Workflow
dev ✅ Deployed 2026-05-29 10:52 UTC Run
prod-us ✅ Deployed 2026-05-29 11:15 UTC Run
prod-eu ✅ Deployed 2026-05-29 11:18 UTC Run

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.

4 participants