Skip to content

Commit

Permalink
chore: automatically removing -- from parsed command list
Browse files Browse the repository at this point in the history
  • Loading branch information
aryanjassal committed Nov 15, 2024
1 parent af570ed commit 9e656fc
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 24 deletions.
8 changes: 2 additions & 6 deletions src/secrets/CommandEnv.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type PolykeyClient from 'polykey/dist/PolykeyClient';
import type { ParsedSecretPathValue } from '../types';
import path from 'path';
import os from 'os';
import * as utils from 'polykey/dist/utils';
Expand Down Expand Up @@ -29,12 +30,7 @@ class CommandEnv extends CommandPolykey {
binParsers.parseEnvArgs,
);
this.action(
async (
args: [Array<[string, string?, string?]>, Array<string>],
options,
) => {
// Remove the -- from the command arguments
args[1].shift();
async (args: [Array<ParsedSecretPathValue>, Array<string>], options) => {
const { default: PolykeyClient } = await import(
'polykey/dist/PolykeyClient'
);
Expand Down
3 changes: 3 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ type PromiseDeconstructed<T> = {
rejectP: (reason?: any) => void;
};

type ParsedSecretPathValue = [string, string?, string?];

export type {
TableRow,
TableOptions,
Expand All @@ -69,4 +71,5 @@ export type {
AgentChildProcessInput,
AgentChildProcessOutput,
PromiseDeconstructed,
ParsedSecretPathValue,
};
30 changes: 15 additions & 15 deletions src/utils/parsers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { Host, Hostname, Port } from 'polykey/dist/network/types';
import type { SeedNodes } from 'polykey/dist/nodes/types';
import type { ParsedSecretPathValue } from '../types';
import commander from 'commander';
import * as validationUtils from 'polykey/dist/validation/utils';
import * as validationErrors from 'polykey/dist/validation/errors';
Expand Down Expand Up @@ -81,7 +82,7 @@ function parseVaultName(vaultName: string): string {
// If 'vault1:', an error is thrown
// If 'a/b/c', an error is thrown
// Splits out everything after an `=` separator
function parseSecretPath(inputPath: string): [string, string?, string?] {
function parseSecretPath(inputPath: string): ParsedSecretPathValue {
// The colon character `:` is prohibited in vaultName, so it's first occurence
// means that this is the delimiter between vaultName and secretPath.
const colonIndex = inputPath.indexOf(':');
Expand Down Expand Up @@ -116,7 +117,7 @@ function parseSecretPath(inputPath: string): [string, string?, string?] {
return [vaultName, secretPath, value];
}

function parseSecretPathValue(secretPath: string): [string, string?, string?] {
function parseSecretPathValue(secretPath: string): ParsedSecretPathValue {
const [vaultName, directoryPath, value] = parseSecretPath(secretPath);
if (value != null && !secretPathValueRegex.test(value)) {
throw new commander.InvalidArgumentError(
Expand All @@ -126,7 +127,7 @@ function parseSecretPathValue(secretPath: string): [string, string?, string?] {
return [vaultName, directoryPath, value];
}

function parseSecretPathEnv(secretPath: string): [string, string?, string?] {
function parseSecretPathEnv(secretPath: string): ParsedSecretPathValue {
const [vaultName, directoryPath, value] = parseSecretPath(secretPath);
if (value != null && !environmentVariableRegex.test(value)) {
throw new commander.InvalidArgumentError(
Expand Down Expand Up @@ -202,30 +203,29 @@ const parseSeedNodes: (data: string) => [SeedNodes, boolean] =
*/
function parseEnvArgs(
value: string,
prev: [Array<[string, string?, string?]>, Array<string>] | undefined,
): [Array<[string, string?, string?]>, Array<string>] {
const current: [Array<[string, string?, string?]>, Array<string>] = prev ?? [
[],
[],
];
if (current[1].length === 0) {
prev: [Array<ParsedSecretPathValue>, Array<string>, boolean] | undefined,
): [Array<ParsedSecretPathValue>, Array<string>, boolean] {
const current: [Array<ParsedSecretPathValue>, Array<string>, boolean] =
prev ?? [[], [], false];
const [secretsList, commandList, parsingCommandCurrent] = current;
let parsingCommand = parsingCommandCurrent;
if (!parsingCommand) {
// Parse a secret path
if (value !== '--') {
current[0].push(parseSecretPathEnv(value));
secretsList.push(parseSecretPathEnv(value));
} else {
current[1].push(value);
return current;
parsingCommand = true;
}
} else {
// Otherwise we just have the cmd args
current[1].push(value);
commandList.push(value);
}
if (current[0].length === 0 && current[1].length > 0) {
throw new commander.InvalidArgumentError(
'You must provide at least 1 secret path',
);
}
return current;
return [secretsList, commandList, parsingCommand];
}

export {
Expand Down
9 changes: 6 additions & 3 deletions tests/secrets/env.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { VaultName } from 'polykey/dist/vaults/types';
import type { ParsedSecretPathValue } from '@/types';
import path from 'path';
import fs from 'fs';
import fc from 'fast-check';
Expand Down Expand Up @@ -774,16 +775,18 @@ describe('commandEnv', () => {
await polykeyAgent.vaultManager.destroyVault(vaultId);
},
);
test.prop([
test.only.prop([
testUtils.secretPathEnvArrayArb,
fc.string().noShrink(),
testUtils.cmdArgsArrayArb,
])(
'parse secrets env arguments',
async (secretPathEnvArray, cmd, cmdArgsArray) => {
let output:
| [Array<[string, string?, string?]>, Array<string>]
| [Array<ParsedSecretPathValue>, Array<string>, boolean]
| undefined = undefined;
// By running the parser directly, we are bypassing commander, so it works
// with a single --
const args: Array<string> = [
...secretPathEnvArray,
'--',
Expand All @@ -798,7 +801,7 @@ describe('commandEnv', () => {
return binParsers.parseSecretPath(v);
});
expect(parsedEnvs).toMatchObject(expectedSecretPathArray);
expect(parsedArgs).toMatchObject(['--', cmd, ...cmdArgsArray]);
expect(parsedArgs).toMatchObject([cmd, ...cmdArgsArray]);
},
);
test('handles no arguments', async () => {
Expand Down

0 comments on commit 9e656fc

Please sign in to comment.