Skip to content

Commit b11d39b

Browse files
committed
feat: changed parsers to allow using vault name without secret path
1 parent 71973d0 commit b11d39b

8 files changed

+22
-25
lines changed

src/secrets/CommandCat.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ class CommandGet extends CommandPolykey {
8585
for (const [vaultName, secretPath] of secretPaths) {
8686
await writer.write({
8787
nameOrId: vaultName,
88-
secretName: secretPath,
88+
secretName: secretPath ?? '/',
8989
metadata: first
9090
? { ...auth, options: { continueOnError: true } }
9191
: undefined,

src/secrets/CommandEdit.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class CommandEdit extends CommandPolykey {
1717
this.argument(
1818
'<secretPath>',
1919
'Path to the secret to be edited, specified as <vaultName>:<directoryPath>',
20-
binParsers.parseSecretPathValue,
20+
binParsers.parseSecretPath,
2121
);
2222
this.addOption(binOptions.nodeId);
2323
this.addOption(binOptions.clientHost);
@@ -68,7 +68,7 @@ class CommandEdit extends CommandPolykey {
6868
const writer = res.writable.getWriter();
6969
await writer.write({
7070
nameOrId: secretPath[0],
71-
secretName: secretPath[1],
71+
secretName: secretPath[1] ?? '/',
7272
metadata: auth,
7373
});
7474
await writer.close();

src/secrets/CommandList.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class CommandList extends CommandPolykey {
1414
this.argument(
1515
'<directoryPath>',
1616
'Directory to list files from, specified as <vaultName>[:<path>]',
17-
binParsers.parseSecretPathOptional,
17+
binParsers.parseSecretPath,
1818
);
1919
this.addOption(binOptions.nodeId);
2020
this.addOption(binOptions.clientHost);

src/secrets/CommandMkdir.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class CommandMkdir extends CommandPolykey {
1313
this.argument(
1414
'<secretPath>',
1515
'Path to where the directory to be created, specified as <vaultName>:<directoryPath>',
16-
binParsers.parseSecretPathValue,
16+
binParsers.parseSecretPath,
1717
);
1818
this.option('-r, --recursive', 'Create the directory recursively');
1919
this.addOption(binOptions.nodeId);

src/secrets/CommandRemove.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class CommandRemove extends CommandPolykey {
2121
this.addOption(binOptions.recursive);
2222
this.action(async (secretPaths, options) => {
2323
secretPaths = secretPaths.map((path: string) =>
24-
binParsers.parseSecretPathValue(path),
24+
binParsers.parseSecretPath(path),
2525
);
2626
const { default: PolykeyClient } = await import(
2727
'polykey/dist/PolykeyClient'

src/secrets/CommandStat.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class CommandStat extends CommandPolykey {
1313
this.argument(
1414
'<secretPath>',
1515
'Path to where the secret, specified as <vaultName>:<directoryPath>',
16-
binParsers.parseSecretPathValue,
16+
binParsers.parseSecretPath,
1717
);
1818
this.addOption(binOptions.nodeId);
1919
this.addOption(binOptions.clientHost);

src/secrets/CommandWrite.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class CommandWrite extends CommandPolykey {
1313
this.argument(
1414
'<secretPath>',
1515
'Path to the secret, specified as <vaultName>:<directoryPath>',
16-
binParsers.parseSecretPathValue,
16+
binParsers.parseSecretPath,
1717
);
1818
this.addOption(binOptions.nodeId);
1919
this.addOption(binOptions.clientHost);

src/utils/parsers.ts

+14-17
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,12 @@ function parseCoreCount(v: string): number | undefined {
6565
}
6666
}
6767

68-
function parseSecretPathOptional(
69-
secretPath: string,
70-
): [string, string?, string?] {
68+
function parseSecretPath(secretPath: string): [string, string?, string?] {
7169
// E.g. If 'vault1:a/b/c', ['vault1', 'a/b/c'] is returned
7270
// If 'vault1', ['vault1, undefined] is returned
73-
// splits out everything after an `=` separator
71+
// If 'vault1:', an error is thrown
72+
// If 'a/b/c', an error is thrown
73+
// Splits out everything after an `=` separator
7474
const lastEqualIndex = secretPath.lastIndexOf('=');
7575
const splitSecretPath =
7676
lastEqualIndex === -1
@@ -89,25 +89,18 @@ function parseSecretPathOptional(
8989
return [vaultName, directoryPath, value];
9090
}
9191

92-
function parseSecretPath(secretPath: string): [string, string, string?] {
93-
// E.g. If 'vault1:a/b/c', ['vault1', 'a/b/c'] is returned
94-
// If 'vault1', an error is thrown
95-
const [vaultName, secretName, value] = parseSecretPathOptional(secretPath);
96-
if (secretName === undefined) {
97-
throw new commander.InvalidArgumentError(
98-
`${secretPath} is not of the format <vaultName>:<directoryPath>[=<value>]`,
99-
);
100-
}
101-
return [vaultName, secretName, value];
102-
}
103-
10492
function parseSecretPathValue(secretPath: string): [string, string, string?] {
10593
const [vaultName, directoryPath, value] = parseSecretPath(secretPath);
10694
if (value != null && !secretPathValueRegex.test(value)) {
10795
throw new commander.InvalidArgumentError(
10896
`${value} is not a valid value name`,
10997
);
11098
}
99+
if (directoryPath == null) {
100+
throw new commander.InvalidArgumentError(
101+
`${secretPath} is not of the format <vaultName>:<directoryPath>[=<value>]`,
102+
);
103+
}
111104
return [vaultName, directoryPath, value];
112105
}
113106

@@ -118,6 +111,11 @@ function parseSecretPathEnv(secretPath: string): [string, string, string?] {
118111
`${value} is not a valid environment variable name`,
119112
);
120113
}
114+
if (directoryPath == null) {
115+
throw new commander.InvalidArgumentError(
116+
`${secretPath} is not of the format <vaultName>:<directoryPath>[=<value>]`,
117+
);
118+
}
121119
return [vaultName, directoryPath, value];
122120
}
123121

@@ -221,7 +219,6 @@ export {
221219
validateParserToArgParser,
222220
validateParserToArgListParser,
223221
parseCoreCount,
224-
parseSecretPathOptional,
225222
parseSecretPath,
226223
parseSecretPathValue,
227224
parseSecretPathEnv,

0 commit comments

Comments
 (0)