From ce60f6da3d7b5346bc6bd54544dbe6bb56437610 Mon Sep 17 00:00:00 2001 From: Amy Yan Date: Mon, 4 Mar 2024 15:49:08 +1100 Subject: [PATCH] feat: `CommandList` now uses dict and list formats --- src/identities/CommandList.ts | 44 ++++++++++++++++++++--------------- 1 file changed, 25 insertions(+), 19 deletions(-) diff --git a/src/identities/CommandList.ts b/src/identities/CommandList.ts index 7d7c7e43..7d7407ea 100644 --- a/src/identities/CommandList.ts +++ b/src/identities/CommandList.ts @@ -42,7 +42,6 @@ class CommandList extends CommandPolykey { }, logger: this.logger.getChild(PolykeyClient.name), }); - let output: any; const gestalts = await binUtils.retryAuthentication(async (auth) => { const gestalts: Array = []; const stream = await pkClient.rpcClient.methods.gestaltsGestaltList({ @@ -82,32 +81,39 @@ class CommandList extends CommandPolykey { } return gestalts; }, auth); - output = gestalts; - if (options.format !== 'json') { + if (options.format === 'json') { + process.stdout.write( + binUtils.outputFormatter({ + type: options.format === 'json' ? 'json' : 'list', + data: gestalts, + }), + ); + } else { // Convert to a human-readable list. - output = []; let count = 1; for (const gestalt of gestalts) { - output.push(`gestalt ${count}`); - output.push(`permissions: ${gestalt.permissions ?? 'None'}`); + process.stdout.write( + binUtils.outputFormatter({ + type: 'dict', + data: { + gestalt: count, + permissions: gestalt.permissions, + }, + }), + ); // Listing nodes - for (const node of gestalt.nodes) { - output.push(`${node.nodeId}`); - } + const nodeIds = gestalt.nodes.map((node) => node.nodeId); // Listing identities - for (const identity of gestalt.identities) { - output.push(`${identity.providerId}:${identity.identityId}`); - } - output.push(''); + const identities = gestalt.identities.map( + (identity) => `${identity.providerId}:${identity.identityId}`, + ); + binUtils.outputFormatter({ + type: 'list', + data: nodeIds.concat(identities), + }); count++; } } - process.stdout.write( - binUtils.outputFormatter({ - type: options.format === 'json' ? 'json' : 'list', - data: output, - }), - ); } finally { if (pkClient! != null) await pkClient.stop(); }