Skip to content

Commit f0dde87

Browse files
committed
feat: add option to add overrides, or provide extra variables at call-time
1 parent ab16bc7 commit f0dde87

File tree

3 files changed

+38
-3
lines changed

3 files changed

+38
-3
lines changed

src/commands/inspect.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,19 @@ import path from 'path';
33
import { getFS } from '../utils/ifs';
44
import { CipherData, Encryption } from '../utils/encryption';
55
import { getKeyVerifier } from '../utils/persistent-unlock';
6+
import { parseEnvEntry, serializeEnv } from '../utils/env-utils';
7+
import dotenv from 'dotenv';
68

79
export const inspectCommand: Command = program
810
.createCommand('inspect')
911
.argument('<env_file>', 'File to inspect')
10-
.action(async (envFile) => {
12+
.option(
13+
'-o, --override [keyvalue...]',
14+
'Override or pass extra environment variable, e.g MY_VAR=abba',
15+
parseEnvEntry,
16+
{},
17+
)
18+
.action(async (envFile, options) => {
1119
const fs = getFS();
1220
const dirname = path.dirname(envFile);
1321
const unlockfile = path.join(dirname, '.dktp.unlocked');
@@ -17,9 +25,12 @@ export const inspectCommand: Command = program
1725
const [key, verifier] = keyVerifier;
1826
const content = await Encryption.decryptWithKey(key, verifier, vaultFile);
1927

28+
const env = dotenv.parse(content);
29+
dotenv.populate(env, options.override, { override: true });
30+
2031
if (updatedValue) {
2132
await fs.write(unlockfile, Buffer.from(updatedValue));
2233
}
2334

24-
console.log(content);
35+
console.log(serializeEnv(env));
2536
});

src/commands/run.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,19 @@ import { getExec } from '../utils/exec';
55
import { getFS } from '../utils/ifs';
66
import { CipherData, Encryption } from '../utils/encryption';
77
import { getKeyVerifier } from '../utils/persistent-unlock';
8+
import { parseEnvEntry } from '../utils/env-utils';
89

910
export const runCommand: Command = program
1011
.createCommand('run')
1112
.argument('<env_file>', 'Envfile to use for process')
1213
.argument('<command...>', 'The command to run')
13-
.action(async (envFile, cmds) => {
14+
.option(
15+
'-o, --override [keyvalue...]',
16+
'Override or pass extra environment variable, e.g MY_VAR=abba',
17+
parseEnvEntry,
18+
{},
19+
)
20+
.action(async (envFile, cmds, options) => {
1421
const fs = getFS();
1522
const dirname = path.dirname(envFile);
1623
const unlockfile = path.join(dirname, '.dktp.unlocked');
@@ -25,6 +32,7 @@ export const runCommand: Command = program
2532
}
2633

2734
const env = dotenv.parse(content);
35+
dotenv.populate(env, options.override, { override: true });
2836
dotenv.populate(process.env as dotenv.DotenvPopulateInput, env, { override: true });
2937

3038
getExec().spawn(cmds);

src/utils/env-utils.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
export function parseEnvEntry(entry: string, previous: Record<string, string>): Record<string, string> {
2+
const delimiterIndex = entry.indexOf('=');
3+
const key = entry.slice(0, delimiterIndex);
4+
const value = entry.slice(delimiterIndex + 1);
5+
6+
return {
7+
...previous,
8+
[key]: value,
9+
};
10+
}
11+
12+
export function serializeEnv(env: Record<string, string>): string {
13+
return Object.entries(env)
14+
.map(([key, value]) => `${key}="${value}"`)
15+
.join('\n');
16+
}

0 commit comments

Comments
 (0)