Skip to content

Commit 9e656fc

Browse files
committed
chore: automatically removing -- from parsed command list
1 parent af570ed commit 9e656fc

File tree

4 files changed

+26
-24
lines changed

4 files changed

+26
-24
lines changed

src/secrets/CommandEnv.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type PolykeyClient from 'polykey/dist/PolykeyClient';
2+
import type { ParsedSecretPathValue } from '../types';
23
import path from 'path';
34
import os from 'os';
45
import * as utils from 'polykey/dist/utils';
@@ -29,12 +30,7 @@ class CommandEnv extends CommandPolykey {
2930
binParsers.parseEnvArgs,
3031
);
3132
this.action(
32-
async (
33-
args: [Array<[string, string?, string?]>, Array<string>],
34-
options,
35-
) => {
36-
// Remove the -- from the command arguments
37-
args[1].shift();
33+
async (args: [Array<ParsedSecretPathValue>, Array<string>], options) => {
3834
const { default: PolykeyClient } = await import(
3935
'polykey/dist/PolykeyClient'
4036
);

src/types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ type PromiseDeconstructed<T> = {
6161
rejectP: (reason?: any) => void;
6262
};
6363

64+
type ParsedSecretPathValue = [string, string?, string?];
65+
6466
export type {
6567
TableRow,
6668
TableOptions,
@@ -69,4 +71,5 @@ export type {
6971
AgentChildProcessInput,
7072
AgentChildProcessOutput,
7173
PromiseDeconstructed,
74+
ParsedSecretPathValue,
7275
};

src/utils/parsers.ts

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { Host, Hostname, Port } from 'polykey/dist/network/types';
22
import type { SeedNodes } from 'polykey/dist/nodes/types';
3+
import type { ParsedSecretPathValue } from '../types';
34
import commander from 'commander';
45
import * as validationUtils from 'polykey/dist/validation/utils';
56
import * as validationErrors from 'polykey/dist/validation/errors';
@@ -81,7 +82,7 @@ function parseVaultName(vaultName: string): string {
8182
// If 'vault1:', an error is thrown
8283
// If 'a/b/c', an error is thrown
8384
// Splits out everything after an `=` separator
84-
function parseSecretPath(inputPath: string): [string, string?, string?] {
85+
function parseSecretPath(inputPath: string): ParsedSecretPathValue {
8586
// The colon character `:` is prohibited in vaultName, so it's first occurence
8687
// means that this is the delimiter between vaultName and secretPath.
8788
const colonIndex = inputPath.indexOf(':');
@@ -116,7 +117,7 @@ function parseSecretPath(inputPath: string): [string, string?, string?] {
116117
return [vaultName, secretPath, value];
117118
}
118119

119-
function parseSecretPathValue(secretPath: string): [string, string?, string?] {
120+
function parseSecretPathValue(secretPath: string): ParsedSecretPathValue {
120121
const [vaultName, directoryPath, value] = parseSecretPath(secretPath);
121122
if (value != null && !secretPathValueRegex.test(value)) {
122123
throw new commander.InvalidArgumentError(
@@ -126,7 +127,7 @@ function parseSecretPathValue(secretPath: string): [string, string?, string?] {
126127
return [vaultName, directoryPath, value];
127128
}
128129

129-
function parseSecretPathEnv(secretPath: string): [string, string?, string?] {
130+
function parseSecretPathEnv(secretPath: string): ParsedSecretPathValue {
130131
const [vaultName, directoryPath, value] = parseSecretPath(secretPath);
131132
if (value != null && !environmentVariableRegex.test(value)) {
132133
throw new commander.InvalidArgumentError(
@@ -202,30 +203,29 @@ const parseSeedNodes: (data: string) => [SeedNodes, boolean] =
202203
*/
203204
function parseEnvArgs(
204205
value: string,
205-
prev: [Array<[string, string?, string?]>, Array<string>] | undefined,
206-
): [Array<[string, string?, string?]>, Array<string>] {
207-
const current: [Array<[string, string?, string?]>, Array<string>] = prev ?? [
208-
[],
209-
[],
210-
];
211-
if (current[1].length === 0) {
206+
prev: [Array<ParsedSecretPathValue>, Array<string>, boolean] | undefined,
207+
): [Array<ParsedSecretPathValue>, Array<string>, boolean] {
208+
const current: [Array<ParsedSecretPathValue>, Array<string>, boolean] =
209+
prev ?? [[], [], false];
210+
const [secretsList, commandList, parsingCommandCurrent] = current;
211+
let parsingCommand = parsingCommandCurrent;
212+
if (!parsingCommand) {
212213
// Parse a secret path
213214
if (value !== '--') {
214-
current[0].push(parseSecretPathEnv(value));
215+
secretsList.push(parseSecretPathEnv(value));
215216
} else {
216-
current[1].push(value);
217-
return current;
217+
parsingCommand = true;
218218
}
219219
} else {
220220
// Otherwise we just have the cmd args
221-
current[1].push(value);
221+
commandList.push(value);
222222
}
223223
if (current[0].length === 0 && current[1].length > 0) {
224224
throw new commander.InvalidArgumentError(
225225
'You must provide at least 1 secret path',
226226
);
227227
}
228-
return current;
228+
return [secretsList, commandList, parsingCommand];
229229
}
230230

231231
export {

tests/secrets/env.test.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { VaultName } from 'polykey/dist/vaults/types';
2+
import type { ParsedSecretPathValue } from '@/types';
23
import path from 'path';
34
import fs from 'fs';
45
import fc from 'fast-check';
@@ -774,16 +775,18 @@ describe('commandEnv', () => {
774775
await polykeyAgent.vaultManager.destroyVault(vaultId);
775776
},
776777
);
777-
test.prop([
778+
test.only.prop([
778779
testUtils.secretPathEnvArrayArb,
779780
fc.string().noShrink(),
780781
testUtils.cmdArgsArrayArb,
781782
])(
782783
'parse secrets env arguments',
783784
async (secretPathEnvArray, cmd, cmdArgsArray) => {
784785
let output:
785-
| [Array<[string, string?, string?]>, Array<string>]
786+
| [Array<ParsedSecretPathValue>, Array<string>, boolean]
786787
| undefined = undefined;
788+
// By running the parser directly, we are bypassing commander, so it works
789+
// with a single --
787790
const args: Array<string> = [
788791
...secretPathEnvArray,
789792
'--',
@@ -798,7 +801,7 @@ describe('commandEnv', () => {
798801
return binParsers.parseSecretPath(v);
799802
});
800803
expect(parsedEnvs).toMatchObject(expectedSecretPathArray);
801-
expect(parsedArgs).toMatchObject(['--', cmd, ...cmdArgsArray]);
804+
expect(parsedArgs).toMatchObject([cmd, ...cmdArgsArray]);
802805
},
803806
);
804807
test('handles no arguments', async () => {

0 commit comments

Comments
 (0)