Skip to content

Commit

Permalink
feat: add option to add overrides, or provide extra variables at call…
Browse files Browse the repository at this point in the history
…-time
  • Loading branch information
nutgaard committed Jun 18, 2024
1 parent ab16bc7 commit f0dde87
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
15 changes: 13 additions & 2 deletions src/commands/inspect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,19 @@ import path from 'path';
import { getFS } from '../utils/ifs';
import { CipherData, Encryption } from '../utils/encryption';
import { getKeyVerifier } from '../utils/persistent-unlock';
import { parseEnvEntry, serializeEnv } from '../utils/env-utils';
import dotenv from 'dotenv';

export const inspectCommand: Command = program
.createCommand('inspect')
.argument('<env_file>', 'File to inspect')
.action(async (envFile) => {
.option(
'-o, --override [keyvalue...]',
'Override or pass extra environment variable, e.g MY_VAR=abba',
parseEnvEntry,
{},
)
.action(async (envFile, options) => {
const fs = getFS();
const dirname = path.dirname(envFile);
const unlockfile = path.join(dirname, '.dktp.unlocked');
Expand All @@ -17,9 +25,12 @@ export const inspectCommand: Command = program
const [key, verifier] = keyVerifier;
const content = await Encryption.decryptWithKey(key, verifier, vaultFile);

const env = dotenv.parse(content);
dotenv.populate(env, options.override, { override: true });

if (updatedValue) {
await fs.write(unlockfile, Buffer.from(updatedValue));
}

console.log(content);
console.log(serializeEnv(env));
});
10 changes: 9 additions & 1 deletion src/commands/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,19 @@ import { getExec } from '../utils/exec';
import { getFS } from '../utils/ifs';
import { CipherData, Encryption } from '../utils/encryption';
import { getKeyVerifier } from '../utils/persistent-unlock';
import { parseEnvEntry } from '../utils/env-utils';

export const runCommand: Command = program
.createCommand('run')
.argument('<env_file>', 'Envfile to use for process')
.argument('<command...>', 'The command to run')
.action(async (envFile, cmds) => {
.option(
'-o, --override [keyvalue...]',
'Override or pass extra environment variable, e.g MY_VAR=abba',
parseEnvEntry,
{},
)
.action(async (envFile, cmds, options) => {
const fs = getFS();
const dirname = path.dirname(envFile);
const unlockfile = path.join(dirname, '.dktp.unlocked');
Expand All @@ -25,6 +32,7 @@ export const runCommand: Command = program
}

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

getExec().spawn(cmds);
Expand Down
16 changes: 16 additions & 0 deletions src/utils/env-utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export function parseEnvEntry(entry: string, previous: Record<string, string>): Record<string, string> {
const delimiterIndex = entry.indexOf('=');
const key = entry.slice(0, delimiterIndex);
const value = entry.slice(delimiterIndex + 1);

return {
...previous,
[key]: value,
};
}

export function serializeEnv(env: Record<string, string>): string {
return Object.entries(env)
.map(([key, value]) => `${key}="${value}"`)
.join('\n');
}

0 comments on commit f0dde87

Please sign in to comment.