@@ -8,8 +8,7 @@ import * as gestaltsUtils from 'polykey/dist/gestalts/utils';
88import * as networkUtils from 'polykey/dist/network/utils' ;
99import * as nodesUtils from 'polykey/dist/nodes/utils' ;
1010
11- const vaultNameRegex = / ^ [ \w . - ] + $ / ;
12- const secretPathNameRegex = / ^ ( [ \w - ] + ) (?: : ( [ ^ \0 \\ = ] + ) ) ? $ / ;
11+ const secretPathRegex = / ^ ( [ \w - ] + ) (?: : ( [ ^ \0 \\ = ] + ) ) ? $ / ;
1312const secretPathValueRegex = / ^ ( [ a - z A - Z _ ] [ \w ] + ) ? $ / ;
1413const environmentVariableRegex = / ^ ( [ a - z A - Z _ ] + [ a - z A - Z 0 - 9 _ ] * ) ? $ / ;
1514
@@ -66,47 +65,44 @@ function parseCoreCount(v: string): number | undefined {
6665 }
6766}
6867
69- function parseVaultName ( vaultName : string ) : string {
70- // E.g. If 'vault1, 'vault1' is returned
71- // If 'vault1:a/b/c', an error is thrown
72- if ( ! vaultNameRegex . test ( vaultName ) ) {
73- throw new commander . InvalidArgumentError (
74- `${ vaultName } is not of the format <vaultName>` ,
75- ) ;
76- }
77- // Returns match[1], or the parsed vaultName
78- return vaultName . match ( secretPathNameRegex ) ! [ 1 ] ;
79- }
80-
81- function parseSecretName ( secretPath : string ) : [ string , string ?] {
68+ function parseSecretPathOptional (
69+ secretPath : string ,
70+ ) : [ string , string ?, string ?] {
8271 // E.g. If 'vault1:a/b/c', ['vault1', 'a/b/c'] is returned
8372 // If 'vault1', ['vault1, undefined] is returned
84- if ( ! secretPathNameRegex . test ( secretPath ) ) {
73+ // splits out everything after an `=` separator
74+ const lastEqualIndex = secretPath . lastIndexOf ( '=' ) ;
75+ const splitSecretPath =
76+ lastEqualIndex === - 1
77+ ? secretPath
78+ : secretPath . substring ( 0 , lastEqualIndex ) ;
79+ const value =
80+ lastEqualIndex === - 1
81+ ? undefined
82+ : secretPath . substring ( lastEqualIndex + 1 ) ;
83+ if ( ! secretPathRegex . test ( splitSecretPath ) ) {
8584 throw new commander . InvalidArgumentError (
86- `${ secretPath } is not of the format <vaultName>[:<directoryPath>]` ,
85+ `${ secretPath } is not of the format <vaultName>[:<directoryPath>][=<value>] ` ,
8786 ) ;
8887 }
89- // Returns [vaultName, secretName?]
90- const match = secretPath . match ( secretPathNameRegex ) ! ;
91- return [ match [ 1 ] , match [ 2 ] || undefined ] ;
88+ const [ , vaultName , directoryPath ] = splitSecretPath . match ( secretPathRegex ) ! ;
89+ return [ vaultName , directoryPath , value ] ;
9290}
9391
9492function parseSecretPath ( secretPath : string ) : [ string , string , string ?] {
9593 // E.g. If 'vault1:a/b/c', ['vault1', 'a/b/c'] is returned
9694 // If 'vault1', an error is thrown
97- const [ vaultName , secretName ] = parseSecretName ( secretPath ) ;
95+ const [ vaultName , secretName , value ] = parseSecretPathOptional ( secretPath ) ;
9896 if ( secretName === undefined ) {
9997 throw new commander . InvalidArgumentError (
100- `${ secretPath } is not of the format <vaultName>:<directoryPath>` ,
98+ `${ secretPath } is not of the format <vaultName>:<directoryPath>[=<value>] ` ,
10199 ) ;
102100 }
103- return [ vaultName , secretName ] ;
101+ return [ vaultName , secretName , value ] ;
104102}
105103
106104function parseSecretPathValue ( secretPath : string ) : [ string , string , string ?] {
107- const [ vaultName , directoryPath ] = parseSecretPath ( secretPath ) ;
108- const lastEqualIndex = secretPath . lastIndexOf ( '=' ) ;
109- const value = lastEqualIndex === - 1 ? '' : secretPath . substring ( lastEqualIndex + 1 ) ;
105+ const [ vaultName , directoryPath , value ] = parseSecretPath ( secretPath ) ;
110106 if ( value != null && ! secretPathValueRegex . test ( value ) ) {
111107 throw new commander . InvalidArgumentError (
112108 `${ value } is not a valid value name` ,
@@ -219,13 +215,13 @@ function parseEnvArgs(
219215}
220216
221217export {
218+ secretPathRegex ,
222219 secretPathValueRegex ,
223220 environmentVariableRegex ,
224221 validateParserToArgParser ,
225222 validateParserToArgListParser ,
226223 parseCoreCount ,
227- parseVaultName ,
228- parseSecretName ,
224+ parseSecretPathOptional ,
229225 parseSecretPath ,
230226 parseSecretPathValue ,
231227 parseSecretPathEnv ,
0 commit comments