Skip to content

Commit 50c4393

Browse files
committed
wip: almost added new trimming
[ci skip]
1 parent 4d8705b commit 50c4393

File tree

7 files changed

+43
-40
lines changed

7 files changed

+43
-40
lines changed

src/errors.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,11 @@ class ErrorPolykeyCLIUnexpectedError<T> extends ErrorPolykeyCLI<T> {
8484
exitCode = sysexits.SOFTWARE;
8585
}
8686

87+
class ErrorPolykeyCLISubprocessFailure<T> extends ErrorPolykeyCLI<T> {
88+
static description = 'A subprocess failed to exit gracefully';
89+
exitCode = sysexits.UNKNOWN;
90+
}
91+
8792
class ErrorPolykeyCLINodePath<T> extends ErrorPolykeyCLI<T> {
8893
static description = 'Cannot derive default node path from unknown platform';
8994
exitCode = sysexits.USAGE;
@@ -191,6 +196,7 @@ export {
191196
ErrorPolykeyCLIUncaughtException,
192197
ErrorPolykeyCLIUnhandledRejection,
193198
ErrorPolykeyCLIUnexpectedError,
199+
ErrorPolykeyCLISubprocessFailure,
194200
ErrorPolykeyCLIAsynchronousDeadlock,
195201
ErrorPolykeyCLINodePath,
196202
ErrorPolykeyCLIClientOptions,

src/secrets/CommandEdit.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,16 +120,16 @@ class CommandEdit extends CommandPolykey {
120120
};
121121
const onError = (e: Error) => {
122122
cleanup();
123-
const error = new errors.ErrorPolykeyCLIEditSecret(
124-
`Failed to run command ${process.env.EDITOR}`,
123+
const error = new errors.ErrorPolykeyCLISubprocessFailure(
124+
`Failed to run command '${process.env.EDITOR}'`,
125125
{ cause: e },
126126
);
127127
reject(error);
128128
};
129129
const onClose = (code: number | null) => {
130130
cleanup();
131131
if (code !== 0) {
132-
const error = new errors.ErrorPolykeyCLIEditSecret(
132+
const error = new errors.ErrorPolykeyCLISubprocessFailure(
133133
`Editor exited with code ${code}`,
134134
);
135135
reject(error);

src/secrets/CommandEnv.ts

Lines changed: 20 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import type PolykeyClient from 'polykey/dist/PolykeyClient';
22
import path from 'path';
33
import os from 'os';
4-
import commander from 'commander';
54
import * as utils from 'polykey/dist/utils';
65
import CommandPolykey from '../CommandPolykey';
76
import * 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: {

src/utils/options.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ const preserveNewline = new commander.Option(
327327
out.push(binParsers.parseSecretPathEnv(value));
328328
return out;
329329
})
330-
.default(undefined);
330+
.default([]);
331331

332332
export {
333333
nodePath,

src/utils/parsers.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,8 @@ function parseEnvArgs(
208208
[],
209209
[],
210210
];
211-
console.error('val', value)
212-
console.error('cur', current)
211+
console.error('value', value)
212+
console.error('curent', current)
213213
if (current[1].length === 0) {
214214
// Parse a secret path
215215
if (value !== '--') {
@@ -222,6 +222,11 @@ function parseEnvArgs(
222222
// Otherwise we just have the cmd args
223223
current[1].push(value);
224224
}
225+
if (current[0].length === 0 && current[1].length > 0) {
226+
throw new commander.InvalidArgumentError(
227+
'You must provide at least 1 secret path',
228+
);
229+
}
225230
return current;
226231
}
227232

src/utils/utils.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,11 @@ function outputFormatterError(err: any): string {
448448
}
449449
output += `${indent}timestamp\t${err.timestamp}\n`;
450450
} else {
451-
output += '\n';
451+
if (err.data && !utils.isEmptyObject(err.data)) {
452+
output += `\n${indent}data: ${JSON.stringify(err.data)}\n`
453+
} else {
454+
output += '\n';
455+
}
452456
}
453457
output += `${indent}cause: `;
454458
err = err.cause;

tests/secrets/env.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -741,6 +741,7 @@ describe('commandEnv', () => {
741741
'unix',
742742
'--preserve-newline',
743743
`${vaultName}:${secretName}`,
744+
`${vaultName}:${secretName}`,
744745
'--',
745746
'node',
746747
'-e',

0 commit comments

Comments
 (0)