11import type { Host , Hostname , Port } from 'polykey/dist/network/types' ;
22import type { SeedNodes } from 'polykey/dist/nodes/types' ;
3+ import type { ParsedSecretPathValue } from '../types' ;
34import commander from 'commander' ;
45import * as validationUtils from 'polykey/dist/validation/utils' ;
56import * 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 */
203204function 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
231231export {
0 commit comments