Skip to content

Commit

Permalink
Merge pull request #50 from MatrixAI/feature-stdout-fix
Browse files Browse the repository at this point in the history
Fixing outputFormatter and standardising STDOUT
  • Loading branch information
amydevs authored Nov 8, 2023
2 parents 485ea68 + 37db593 commit 550c32f
Show file tree
Hide file tree
Showing 29 changed files with 501 additions and 255 deletions.
2 changes: 1 addition & 1 deletion src/identities/CommandPermissions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class CommandPermissions extends CommandPolykey {
logger: this.logger.getChild(PolykeyClient.name),
});
const [type, id] = gestaltId;
let actions: string[] = [];
let actions: Array<string> = [];
switch (type) {
case 'node':
{
Expand Down
2 changes: 1 addition & 1 deletion src/keys/CommandCert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class CommandCert extends CommandPolykey {
};
let output: any = result;
if (options.format === 'human') {
output = [`Root certificate:\t\t${result.cert}`];
output = ['Root certificate:', result.cert];
}
process.stdout.write(
binUtils.outputFormatter({
Expand Down
2 changes: 1 addition & 1 deletion src/keys/CommandCertchain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class CommandsCertchain extends CommandPolykey {
};
let output: any = result;
if (options.format === 'human') {
output = [`Root Certificate Chain:\t\t${result.certchain}`];
output = ['Root Certificate Chain:', ...result.certchain];
}
process.stdout.write(
binUtils.outputFormatter({
Expand Down
2 changes: 1 addition & 1 deletion src/keys/CommandDecrypt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class CommandDecrypt extends CommandPolykey {
};
let output: any = result;
if (options.format === 'human') {
output = [`Decrypted data:\t\t${result.decryptedData}`];
output = [`Decrypted data:${' '.repeat(4)}${result.decryptedData}`];
}
process.stdout.write(
binUtils.outputFormatter({
Expand Down
2 changes: 1 addition & 1 deletion src/keys/CommandEncrypt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ class CommandEncypt extends CommandPolykey {
};
let output: any = result;
if (options.format === 'human') {
output = [`Encrypted data:\t\t${result.encryptedData}`];
output = [`Encrypted data:${' '.repeat(4)}${result.encryptedData}`];
}
process.stdout.write(
binUtils.outputFormatter({
Expand Down
2 changes: 1 addition & 1 deletion src/keys/CommandSign.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class CommandSign extends CommandPolykey {
};
let output: any = result;
if (options.format === 'human') {
output = [`Signature:\t\t${result.signature}`];
output = [`Signature:${' '.repeat(4)}${result.signature}`];
}
process.stdout.write(
binUtils.outputFormatter({
Expand Down
4 changes: 3 additions & 1 deletion src/keys/CommandVerify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,9 @@ class CommandVerify extends CommandPolykey {
};
let output: any = result;
if (options.format === 'human') {
output = [`Signature verified:\t\t${result.signatureVerified}`];
output = [
`Signature verified:${' '.repeat(4)}${result.signatureVerified}`,
];
}
process.stdout.write(
binUtils.outputFormatter({
Expand Down
8 changes: 4 additions & 4 deletions src/nodes/CommandClaim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,25 +65,25 @@ class CommandClaim extends CommandPolykey {
);
const claimed = response.success;
if (claimed) {
const formattedOutput = await binUtils.outputFormatter({
const outputFormatted = binUtils.outputFormatter({
type: options.format === 'json' ? 'json' : 'list',
data: [
`Successfully generated a cryptolink claim on Keynode with ID ${nodesUtils.encodeNodeId(
nodeId,
)}`,
],
});
process.stdout.write(formattedOutput);
process.stdout.write(outputFormatted);
} else {
const formattedOutput = await binUtils.outputFormatter({
const outputFormatted = binUtils.outputFormatter({
type: options.format === 'json' ? 'json' : 'list',
data: [
`Successfully sent Gestalt Invite notification to Keynode with ID ${nodesUtils.encodeNodeId(
nodeId,
)}`,
],
});
process.stdout.write(formattedOutput);
process.stdout.write(outputFormatted);
}
} finally {
if (pkClient! != null) await pkClient.stop();
Expand Down
11 changes: 6 additions & 5 deletions src/nodes/CommandConnections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,28 +59,29 @@ class CommandAdd extends CommandPolykey {
}, auth);
if (options.format === 'human') {
// Wait for outputFormatter to complete and then write to stdout
const formattedOutput = await binUtils.outputFormatter({
const outputFormatted = binUtils.outputFormatter({
type: 'table',
data: connections,
options: {
headers: [
columns: [
'host',
'hostname',
'nodeIdEncoded',
'port',
'timeout',
'usageCount',
],
includeHeaders: true,
},
});
process.stdout.write(formattedOutput);
process.stdout.write(outputFormatted);
} else {
// Wait for outputFormatter to complete and then write to stdout
const formattedOutput = await binUtils.outputFormatter({
const outputFormatted = binUtils.outputFormatter({
type: 'json',
data: connections,
});
process.stdout.write(formattedOutput);
process.stdout.write(outputFormatted);
}
} finally {
if (pkClient! != null) await pkClient.stop();
Expand Down
4 changes: 2 additions & 2 deletions src/nodes/CommandFind.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,11 @@ class CommandFind extends CommandPolykey {
}
let output: any = result;
if (options.format === 'human') output = [result.message];
const formattedOutput = await binUtils.outputFormatter({
const outputFormatted = binUtils.outputFormatter({
type: options.format === 'json' ? 'json' : 'list',
data: output,
});
process.stdout.write(formattedOutput);
process.stdout.write(outputFormatted);
// Like ping it should error when failing to find node for automation reasons.
if (!result.success) {
throw new errors.ErrorPolykeyCLINodeFindFailed(result.message);
Expand Down
4 changes: 2 additions & 2 deletions src/nodes/CommandGetAll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@ class CommandGetAll extends CommandPolykey {
`NodeId ${value.nodeIdEncoded}, Address ${value.host}:${value.port}, bucketIndex ${value.bucketIndex}`,
);
}
const formattedOutput = await binUtils.outputFormatter({
const outputFormatted = binUtils.outputFormatter({
type: options.format === 'json' ? 'json' : 'list',
data: output,
});
process.stdout.write(formattedOutput);
process.stdout.write(outputFormatted);
} finally {
if (pkClient! != null) await pkClient.stop();
}
Expand Down
4 changes: 2 additions & 2 deletions src/nodes/CommandPing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@ class CommandPing extends CommandPolykey {
else status.message = error.message;
const output: any =
options.format === 'json' ? status : [status.message];
const formattedOutput = await binUtils.outputFormatter({
const outputFormatted = binUtils.outputFormatter({
type: options.format === 'json' ? 'json' : 'list',
data: output,
});
process.stdout.write(formattedOutput);
process.stdout.write(outputFormatted);

if (error != null) throw error;
} finally {
Expand Down
4 changes: 2 additions & 2 deletions src/notifications/CommandRead.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,11 @@ class CommandRead extends CommandPolykey {
notifications.push(notification);
}
for (const notification of notifications) {
const formattedOutput = await binUtils.outputFormatter({
const outputFormatted = binUtils.outputFormatter({
type: options.format === 'json' ? 'json' : 'dict',
data: notification,
});
process.stdout.write(formattedOutput);
process.stdout.write(outputFormatted);
}
} finally {
if (pkClient! != null) await pkClient.stop();
Expand Down
10 changes: 6 additions & 4 deletions src/secrets/CommandGet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,14 @@ class CommandGet extends CommandPolykey {
}),
meta,
);
const secretContent = Buffer.from(response.secretContent, 'binary');
const formattedOutput = await binUtils.outputFormatter({
const secretContent = response.secretContent;
const outputFormatted = binUtils.outputFormatter({
type: 'raw',
data: secretContent,
data: binUtils.encodeEscapedWrapped(secretContent)
? binUtils.encodeEscaped(secretContent)
: secretContent,
});
process.stdout.write(formattedOutput);
process.stdout.write(outputFormatted);
} finally {
if (pkClient! != null) await pkClient.stop();
}
Expand Down
4 changes: 2 additions & 2 deletions src/secrets/CommandList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,12 @@ class CommandList extends CommandPolykey {
return data;
}, auth);

const formattedOutput = await binUtils.outputFormatter({
const outputFormatted = binUtils.outputFormatter({
type: options.format === 'json' ? 'json' : 'list',
data: data,
});

process.stdout.write(formattedOutput);
process.stdout.write(outputFormatted);
} finally {
if (pkClient! != null) await pkClient.stop();
}
Expand Down
6 changes: 3 additions & 3 deletions src/secrets/CommandStat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,18 @@ class CommandStat extends CommandPolykey {
meta,
);

const data: string[] = [`Stats for "${secretPath[1]}"`];
const data: Array<string> = [`Stats for "${secretPath[1]}"`];
for (const [key, value] of Object.entries(response.stat)) {
data.push(`${key}: ${value}`);
}

// Assuming the surrounding function is async
const formattedOutput = await binUtils.outputFormatter({
const outputFormatted = binUtils.outputFormatter({
type: options.format === 'json' ? 'json' : 'list',
data,
});

process.stdout.write(formattedOutput);
process.stdout.write(outputFormatted);
} finally {
if (pkClient! != null) await pkClient.stop();
}
Expand Down
19 changes: 10 additions & 9 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ import type { RecoveryCode } from 'polykey/dist/keys/types';
import type { StatusLive } from 'polykey/dist/status/types';
import type { NodeIdEncoded } from 'polykey/dist/ids/types';

type TableRow = Record<string, any>;

interface TableOptions {
columns?: Array<string> | Record<string, number>;
includeHeaders?: boolean;
includeRowCount?: boolean;
}

type AgentStatusLiveData = Omit<StatusLive['data'], 'nodeId'> & {
nodeId: NodeIdEncoded;
};
Expand Down Expand Up @@ -40,17 +48,10 @@ type AgentChildProcessOutput =
error: POJO;
};

type TableRow = Record<string, any>;

interface TableOptions {
headers?: string[];
includeRowCount?: boolean;
}

export type {
TableRow,
TableOptions,
AgentStatusLiveData,
AgentChildProcessInput,
AgentChildProcessOutput,
TableRow,
TableOptions,
};
3 changes: 1 addition & 2 deletions src/utils/parsers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,7 @@ const parseProviderId: (data: string) => ids.ProviderId =

const parseIdentityId: (data: string) => ids.IdentityId =
validateParserToArgParser(ids.parseIdentityId);

const parseProviderIdList: (data: string) => ids.ProviderId[] =
const parseProviderIdList: (data: string) => Array<ids.ProviderId> =
validateParserToArgListParser(ids.parseProviderId);

const parseGestaltAction: (data: string) => 'notify' | 'scan' | 'claim' =
Expand Down
Loading

0 comments on commit 550c32f

Please sign in to comment.