11import type PolykeyClient from 'polykey/dist/PolykeyClient' ;
22import path from 'path' ;
33import os from 'os' ;
4- import commander from 'commander' ;
54import * as utils from 'polykey/dist/utils' ;
65import CommandPolykey from '../CommandPolykey' ;
76import * as binProcessors from '../utils/processors' ;
@@ -25,33 +24,18 @@ class CommandEnv extends CommandPolykey {
2524 this . addOption ( binOptions . envDuplicate ) ;
2625 this . addOption ( binOptions . preserveNewline ) ;
2726 this . argument (
28- '[ args...] ' ,
27+ '< args...> ' ,
2928 'command and arguments formatted as [envPaths...][-- cmd [cmdArgs...]]' ,
3029 binParsers . parseEnvArgs ,
3130 ) ;
32- this . passThroughOptions ( ) ; // Let -- pass through as-is to parse as delimiter for cmd
31+ this . passThroughOptions ( ) ; // Let '--' pass through as-is
3332 this . action (
3433 async (
3534 args : [ Array < [ string , string ?, string ?] > , Array < string > ] ,
3635 options ,
3736 ) => {
38- // The parser sets the default value for args. If no arguments are
39- // provided, then args is an empty array []. This is different from the
40- // expected structure. This block ensure that the structure is preserved.
41- // Do the same for options.preserveNewline.
42- args = args [ 0 ] == null && args [ 1 ] == null ? [ [ ] , [ ] ] : args ;
43- options . preserveNewline = options . preserveNewline ?? [ ] ;
44-
45- // There needs to be at least one argument or preserved argument
46- // provided. Otherwise, print the help text and error out.
47- if ( args [ 0 ] . length === 0 && options . preserveNewline . length === 0 ) {
48- this . outputHelp ( )
49- throw new commander . InvalidArgumentError (
50- 'You must provide at least one secret path.' ,
51- ) ;
52- }
5337 // Remove the -- from the command arguments
54- args [ 1 ] ? .shift ( ) ;
38+ args [ 1 ] . shift ( ) ;
5539 const { default : PolykeyClient } = await import (
5640 'polykey/dist/PolykeyClient'
5741 ) ;
@@ -72,10 +56,6 @@ class CommandEnv extends CommandPolykey {
7256 // b. output the env variables in the desired format
7357
7458 const [ envVariables , [ cmd , ...argv ] ] = args ;
75- // Append the secret paths which we want to preserve the newlines of
76- if ( options . preserveNewline ) {
77- envVariables . push ( ...options . preserveNewline ) ;
78- }
7959 const clientOptions = await binProcessors . processClientOptions (
8060 options . nodePath ,
8161 options . nodeId ,
@@ -187,13 +167,10 @@ class CommandEnv extends CommandPolykey {
187167 let preserveNewline = false ;
188168 if ( options . preserveNewline ) {
189169 for ( const pair of options . preserveNewline ) {
190- // All the secrets from a vault are being exported together
191- if ( pair [ 1 ] == null && pair [ 0 ] === nameOrId ) {
192- preserveNewline = true ;
193- break ;
194- }
195- // Individual files are being exported from a vault
196- if ( pair [ 1 ] === secretName ) {
170+ if (
171+ pair [ 0 ] === nameOrId &&
172+ ( pair [ 1 ] === newName || pair [ 1 ] == null )
173+ ) {
197174 preserveNewline = true ;
198175 break ;
199176 }
@@ -223,11 +200,21 @@ class CommandEnv extends CommandPolykey {
223200 // If a cmd is provided then we default to exec it
224201 switch ( platform ) {
225202 case 'linux' :
226- // Fallthrough
227203 case 'darwin' :
228- {
229- const { exec } = await import ( '@matrixai/exec' ) ;
204+ const { exec } = await import ( '@matrixai/exec' ) ;
205+ try {
230206 exec . execvp ( cmd , argv , envp ) ;
207+ } catch ( e ) {
208+ if ( 'code' in e && e . code === 'GenericFailure' ) {
209+ throw new binErrors . ErrorPolykeyCLISubprocessFailure (
210+ `Command failed with error ${ e } ` ,
211+ {
212+ cause : e ,
213+ data : { command : [ cmd , ...argv ] } ,
214+ } ,
215+ ) ;
216+ }
217+ throw e ;
231218 }
232219 break ;
233220 default : {
0 commit comments