Skip to content

Commit

Permalink
Provider exec error reporting (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
plumdog authored Jan 25, 2021
1 parent 3ef4487 commit 0c6a8c7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
20 changes: 16 additions & 4 deletions provider/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ const bytesToString = (byteArray: Uint8Array): string => {
};

const execPromise = async (args: Array<string>, input: string): Promise<string> => {
return new Promise((res: (result: string) => void, rej: (error: childProcess.ExecException) => void): void => {
return new Promise((res: (result: string) => void, rej: (error: Error) => void): void => {
const proc = childProcess.spawn('sh', ['-c', 'cat', '-', '|', ...args], { stdio: 'pipe', shell: true });
(proc.stdin as Writable).end(input);

Expand All @@ -137,21 +137,33 @@ const execPromise = async (args: Array<string>, input: string): Promise<string>

proc.on('close', (code: number) => {
if (code > 0) {
rej({
name: `Exited with code ${code}`,
message: stderr,
log(`Exec exited with code ${code}`, {
stdout,
stderr,
});
rej(new Error(`Exec exited with code ${code}`));
} else {
if (stderr) {
log(`Exec exited cleanly, but stderr was not empty`, {
stderr,
});
} else {
log('Exec exited cleanly');
}
res(stdout);
}
});
});
};

const sopsDecode = async (fileContent: string, dataType: string, kmsKeyArn: string | undefined): Promise<unknown> => {
log('Running sops command');
const sopsArgs = ['-d', '--input-type', dataType, '--output-type', 'json', ...(kmsKeyArn ? ['--kms', kmsKeyArn] : []), '/dev/stdin'];
log('Sops command args', { sopsArgs });
const result = await execPromise([path.join(__dirname, 'sops'), ...sopsArgs], fileContent);
log('Sops command result', { result });
const parsed = JSON.parse(result);
log('Sops command result', { parsed });
return Promise.resolve(parsed);
};

Expand Down
2 changes: 1 addition & 1 deletion provider/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module.exports = {
statements: 86,
branches: 65,
functions: 90,
lines: 86,
lines: 85,
},
},
};

0 comments on commit 0c6a8c7

Please sign in to comment.