@@ -24,7 +24,7 @@ class CommandEnv extends CommandPolykey {
2424 this . addOption ( binOptions . envDuplicate ) ;
2525 this . addOption ( binOptions . preserveNewline ) ;
2626 this . argument (
27- '< args...> ' ,
27+ '[ args...] ' ,
2828 'command and arguments formatted as [envPaths...][-- cmd [cmdArgs...]]' ,
2929 binParsers . parseEnvArgs ,
3030 ) ;
@@ -34,7 +34,17 @@ class CommandEnv extends CommandPolykey {
3434 args : [ Array < [ string , string ?, string ?] > , Array < string > ] ,
3535 options ,
3636 ) => {
37- args [ 1 ] . shift ( ) ;
37+ // The parser sets the default value for args. If no arguments are
38+ // provided, then args is an empty array []. This is different from the
39+ // expected structure. This block ensure that the structure is preserved.
40+ args = args [ 0 ] == null && args [ 1 ] == null ? [ [ ] , [ ] ] : args ;
41+
42+ // There needs to be at least one argument or preserved argument provided
43+ if ( args [ 0 ] . length === 0 && options . preserveNewline . length === 0 ) {
44+ this . help ( ) ;
45+ }
46+ // Remove the -- from the command arguments
47+ args [ 1 ] ?. shift ( ) ;
3848 const { default : PolykeyClient } = await import (
3949 'polykey/dist/PolykeyClient'
4050 ) ;
@@ -55,6 +65,10 @@ class CommandEnv extends CommandPolykey {
5565 // b. output the env variables in the desired format
5666
5767 const [ envVariables , [ cmd , ...argv ] ] = args ;
68+ // Append the secret paths which we want to preserve the newlines of
69+ if ( options . preserveNewline ) {
70+ envVariables . push ( ...options . preserveNewline ) ;
71+ }
5872 const clientOptions = await binProcessors . processClientOptions (
5973 options . nodePath ,
6074 options . nodeId ,
@@ -161,8 +175,20 @@ class CommandEnv extends CommandPolykey {
161175 utils . never ( ) ;
162176 }
163177 }
178+
179+ // Find if we need to preserve the newline for this secret
180+ let preserveNewline = false ;
181+ if ( options . preserveNewline ) {
182+ for ( const pair of options . preserveNewline ) {
183+ if ( pair [ 1 ] === secretName ) {
184+ preserveNewline = true ;
185+ break ;
186+ }
187+ }
188+ }
189+
164190 // Trim the single trailing newline if it exists
165- if ( secretContent . endsWith ( '\n' ) ) {
191+ if ( ! preserveNewline && secretContent . endsWith ( '\n' ) ) {
166192 envp [ newName ] = secretContent . slice ( 0 , - 1 ) ;
167193 } else {
168194 envp [ newName ] = secretContent ;
0 commit comments