-
Notifications
You must be signed in to change notification settings - Fork 10
feat(cli/compute): --env-file + partial env edits via --env-set/--env-unset #94
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| import { describe, expect, it, beforeAll, afterAll } from 'vitest'; | ||
| import { mkdtempSync, rmSync, writeFileSync } from 'node:fs'; | ||
| import { tmpdir } from 'node:os'; | ||
| import { join } from 'node:path'; | ||
| import { parseEnvFile } from './env-file.js'; | ||
|
|
||
| let dir: string; | ||
|
|
||
| beforeAll(() => { | ||
| dir = mkdtempSync(join(tmpdir(), 'cli-env-file-')); | ||
| }); | ||
|
|
||
| afterAll(() => { | ||
| rmSync(dir, { recursive: true, force: true }); | ||
| }); | ||
|
|
||
| function write(name: string, contents: string): string { | ||
| const p = join(dir, name); | ||
| writeFileSync(p, contents); | ||
| return p; | ||
| } | ||
|
|
||
| describe('parseEnvFile', () => { | ||
| it('parses plain KEY=VALUE pairs', () => { | ||
| const p = write('plain.env', 'FOO=bar\nBAZ=qux\n'); | ||
| expect(parseEnvFile(p)).toEqual({ FOO: 'bar', BAZ: 'qux' }); | ||
| }); | ||
|
|
||
| it('skips blank lines and # comments', () => { | ||
| const p = write('comments.env', '# header comment\n\nFOO=bar\n# inline\nBAZ=qux\n'); | ||
| expect(parseEnvFile(p)).toEqual({ FOO: 'bar', BAZ: 'qux' }); | ||
| }); | ||
|
|
||
| it('strips matching surrounding double quotes from values', () => { | ||
| const p = write('dquotes.env', 'GREETING="hello world"\n'); | ||
| expect(parseEnvFile(p)).toEqual({ GREETING: 'hello world' }); | ||
| }); | ||
|
|
||
| it('strips matching surrounding single quotes from values', () => { | ||
| const p = write('squotes.env', "GREETING='hello world'\n"); | ||
| expect(parseEnvFile(p)).toEqual({ GREETING: 'hello world' }); | ||
| }); | ||
|
|
||
| it('preserves # inside quoted values (not a comment)', () => { | ||
| const p = write('hash.env', 'PASSWORD="abc#123"\n'); | ||
| expect(parseEnvFile(p)).toEqual({ PASSWORD: 'abc#123' }); | ||
| }); | ||
|
|
||
| it('strips trailing inline comment from unquoted values', () => { | ||
| const p = write('inline.env', 'PORT=8080 # default port\n'); | ||
| expect(parseEnvFile(p)).toEqual({ PORT: '8080' }); | ||
| }); | ||
|
|
||
| it('preserves "=" inside values (only first equals splits)', () => { | ||
| const p = write('eq.env', 'JWT=a.b=c.d\n'); | ||
| expect(parseEnvFile(p)).toEqual({ JWT: 'a.b=c.d' }); | ||
| }); | ||
|
|
||
| it('rejects invalid keys (lowercase, hyphen)', () => { | ||
| const p = write('badkey.env', 'lower=ok\n'); | ||
| expect(() => parseEnvFile(p)).toThrow(/invalid env var key/); | ||
| }); | ||
|
|
||
| it('rejects malformed lines (no equals)', () => { | ||
| const p = write('malformed.env', 'NOT_A_PAIR\n'); | ||
| expect(() => parseEnvFile(p)).toThrow(/expected KEY=VALUE/); | ||
| }); | ||
|
|
||
| it('reports the line number on errors', () => { | ||
| const p = write('linenum.env', 'GOOD=ok\n\nBAD\n'); | ||
| expect(() => parseEnvFile(p)).toThrow(/:3:/); | ||
| }); | ||
|
|
||
| it('throws CLIError when file does not exist', () => { | ||
| expect(() => parseEnvFile(join(dir, 'nope.env'))).toThrow(/Could not read --env-file/); | ||
| }); | ||
|
|
||
| it('handles CRLF line endings', () => { | ||
| const p = write('crlf.env', 'FOO=bar\r\nBAZ=qux\r\n'); | ||
| expect(parseEnvFile(p)).toEqual({ FOO: 'bar', BAZ: 'qux' }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| import { readFileSync } from 'node:fs'; | ||
| import { CLIError } from './errors.js'; | ||
|
|
||
| const ENV_KEY_REGEX = /^[A-Z_][A-Z0-9_]*$/; | ||
|
|
||
| // Minimal dotenv parser. Handles: | ||
| // • KEY=VALUE lines | ||
| // • Blank lines and # comment lines | ||
| // • Optional surrounding quotes ("..." or '...') stripped from VALUE | ||
| // • Inline trailing comments after unquoted values: KEY=val # note | ||
| // | ||
| // Keeps escape-sequence handling deliberately out of scope — anything fancy | ||
| // (multiline strings, $VAR expansion) belongs in a real dotenv library; for | ||
| // `compute deploy --env-file` the goal is feature-parity with `--env <json>` | ||
| // for the 95% case, not full dotenv semantics. | ||
| export function parseEnvFile(path: string): Record<string, string> { | ||
| let raw: string; | ||
| try { | ||
| raw = readFileSync(path, 'utf-8'); | ||
| } catch (err) { | ||
| const msg = err instanceof Error ? err.message : String(err); | ||
| throw new CLIError(`Could not read --env-file at ${path}: ${msg}`); | ||
| } | ||
|
|
||
| const result: Record<string, string> = {}; | ||
| const lines = raw.split(/\r?\n/); | ||
| for (let i = 0; i < lines.length; i++) { | ||
| const line = lines[i].trim(); | ||
| if (line === '' || line.startsWith('#')) continue; | ||
|
|
||
| const eq = line.indexOf('='); | ||
| if (eq <= 0) { | ||
| throw new CLIError( | ||
| `${path}:${i + 1}: expected KEY=VALUE, got "${line}"` | ||
| ); | ||
| } | ||
| const key = line.slice(0, eq).trim(); | ||
| if (!ENV_KEY_REGEX.test(key)) { | ||
| throw new CLIError( | ||
| `${path}:${i + 1}: invalid env var key "${key}" (must match [A-Z_][A-Z0-9_]*)` | ||
| ); | ||
| } | ||
|
|
||
| let value = line.slice(eq + 1).trim(); | ||
|
|
||
| // Surrounding quotes (matching pair) — strip them and use the inner | ||
| // string verbatim. Anything inside quotes is preserved including '#'. | ||
| if ( | ||
| (value.startsWith('"') && value.endsWith('"') && value.length >= 2) || | ||
| (value.startsWith("'") && value.endsWith("'") && value.length >= 2) | ||
| ) { | ||
| value = value.slice(1, -1); | ||
| } else { | ||
| // Unquoted value — strip a trailing inline comment (`KEY=val # note`). | ||
| const hash = value.indexOf(' #'); | ||
| if (hash >= 0) value = value.slice(0, hash).trimEnd(); | ||
| } | ||
|
|
||
| result[key] = value; | ||
| } | ||
| return result; | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reject conflicting env patch operations for the same key.
A key can currently be present in both
setandunset, creating an ambiguous patch contract. Fail fast in CLI when overlap exists.Suggested fix
if (hasPatch) { const setMap: Record<string, string> = {}; for (const arg of envSetArgs) { const [k, v] = parseKeyValue(arg); setMap[k] = v; } for (const k of envUnsetArgs) assertValidKey(k); + const overlaps = envUnsetArgs.filter((k) => Object.hasOwn(setMap, k)); + if (overlaps.length > 0) { + throw new CLIError( + `Conflicting env patch: key(s) present in both --env-set and --env-unset: ${overlaps.join(', ')}` + ); + } body.envVarsPatch = { ...(envSetArgs.length > 0 && { set: setMap }), ...(envUnsetArgs.length > 0 && { unset: envUnsetArgs }), }; }📝 Committable suggestion
🤖 Prompt for AI Agents