Skip to content

Commit

Permalink
Merge pull request #56 from MatrixAI/feature-stdout-standardization
Browse files Browse the repository at this point in the history
Stdout standardization with output formatter
  • Loading branch information
amydevs authored Mar 22, 2024
2 parents e8cc798 + 18d223c commit da7af41
Show file tree
Hide file tree
Showing 47 changed files with 727 additions and 503 deletions.
3 changes: 2 additions & 1 deletion src/agent/CommandStatus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,14 @@ class CommandStatus extends CommandPolykey {
agentHost: response.agentHost,
agentPort: response.agentPort,
upTime: getUnixtime() - response.startTime,
startTime: response.startTime,
connectionsActive: response.connectionsActive,
nodesTotal: response.nodesTotal,
version: response.version,
sourceVersion: response.sourceVersion,
stateVersion: response.stateVersion,
networkVersion: response.networkVersion,
...response.versionMetadata,
versionMetadata: response.versionMetadata,
},
}),
);
Expand Down
8 changes: 4 additions & 4 deletions src/agent/CommandStop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ class CommandStop extends CommandPolykey {
);
const statusInfo = clientStatus.statusInfo;
if (statusInfo?.status === 'DEAD') {
this.logger.info('Agent is already dead');
process.stderr.write('Agent is already dead\n');
return;
} else if (statusInfo?.status === 'STOPPING') {
this.logger.info('Agent is already stopping');
process.stderr.write('Agent is already stopping\n');
return;
} else if (statusInfo?.status === 'STARTING') {
throw new errors.ErrorPolykeyCLIAgentStatus('agent is starting');
throw new errors.ErrorPolykeyCLIAgentStatus('Agent is starting');
}
const auth = await binProcessors.processAuthentication(
options.passwordFile,
Expand Down Expand Up @@ -62,7 +62,7 @@ class CommandStop extends CommandPolykey {
}),
auth,
);
this.logger.info('Stopping Agent');
process.stderr.write('Stopping Agent\n');
} finally {
if (pkClient! != null) await pkClient.stop();
}
Expand Down
10 changes: 9 additions & 1 deletion src/bootstrap/CommandBootstrap.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import process from 'process';
import CommandPolykey from '../CommandPolykey';
import * as binUtils from '../utils';
import * as binOptions from '../utils/options';
import * as binProcessors from '../utils/processors';

Expand Down Expand Up @@ -36,7 +37,14 @@ class CommandBootstrap extends CommandPolykey {
logger: this.logger,
});
this.logger.info(`Bootstrapped ${options.nodePath}`);
if (recoveryCodeOut != null) process.stdout.write(recoveryCodeOut + '\n');
process.stdout.write(
binUtils.outputFormatter({
type: options.format === 'json' ? 'json' : 'dict',
data: {
recoveryCode: recoveryCodeOut,
},
}),
);
});
}
}
Expand Down
46 changes: 30 additions & 16 deletions src/identities/CommandAuthenticate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,33 +68,47 @@ class CommandAuthenticate extends CommandPolykey {
);
for await (const message of genReadable) {
if (message.request != null) {
this.logger.info(`Navigate to the URL in order to authenticate`);
this.logger.info(
'Use any additional additional properties to complete authentication',
process.stderr.write(
`Navigate to the URL in order to authenticate\n`,
);
process.stderr.write(
'Use any additional additional properties to complete authentication\n',
);
try {
await identitiesUtils.browser(message.request.url);
} catch (e) {
if (e.code !== 'ENOENT') throw e;
// Otherwise we ignore `ENOENT` as a failure to spawn a browser
}
process.stdout.write(
binUtils.outputFormatter({
type: options.format === 'json' ? 'json' : 'dict',
data: {
url: message.request.url,
...message.request.dataMap,
},
}),
);
if (options.format === 'json') {
process.stdout.write(
binUtils.outputFormatter({
type: 'json',
data: {
url: message.request.url,
dataMap: message.request.dataMap,
},
}),
);
} else {
process.stdout.write(
binUtils.outputFormatter({
type: 'dict',
data: {
url: message.request.url,
...message.request.dataMap,
},
}),
);
}
} else if (message.response != null) {
this.logger.info(
`Authenticated digital identity provider ${providerId}`,
process.stderr.write(
`Authenticated digital identity provider ${providerId}\n`,
);
process.stdout.write(
binUtils.outputFormatter({
type: options.format === 'json' ? 'json' : 'list',
data: [message.response.identityId],
type: options.format === 'json' ? 'json' : 'dict',
data: { identityId: message.response.identityId },
}),
);
} else {
Expand Down
10 changes: 6 additions & 4 deletions src/identities/CommandClaim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,16 @@ class CommandClaim extends CommandPolykey {
}),
auth,
);
const output = [`Claim Id: ${claimMessage.claimId}`];
const data: { claimId: string; url?: string } = {
claimId: claimMessage.claimId,
};
if (claimMessage.url) {
output.push(`Url: ${claimMessage.url}`);
data.url = claimMessage.url;
}
process.stdout.write(
binUtils.outputFormatter({
type: options.format === 'json' ? 'json' : 'list',
data: output,
type: options.format === 'json' ? 'json' : 'dict',
data,
}),
);
} finally {
Expand Down
27 changes: 17 additions & 10 deletions src/identities/CommandGet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,17 @@ class CommandGet extends CommandPolykey {
utils.never();
}
const gestalt = res!.gestalt;
let output: any = gestalt;
if (options.format !== 'json') {
// Creating a list.
output = [];
if (options.format === 'json') {
process.stdout.write(
binUtils.outputFormatter({
type: 'json',
data: {
gestalt,
},
}),
);
} else {
const output: Array<string> = [];
// Listing nodes.
for (const nodeKey of Object.keys(gestalt.nodes)) {
const node = gestalt.nodes[nodeKey];
Expand All @@ -102,13 +109,13 @@ class CommandGet extends CommandPolykey {
const identity = gestalt.identities[identityKey];
output.push(`${identity.providerId}:${identity.identityId}`);
}
process.stdout.write(
binUtils.outputFormatter({
type: 'list',
data: output,
}),
);
}
process.stdout.write(
binUtils.outputFormatter({
type: options.format === 'json' ? 'json' : 'list',
data: output,
}),
);
} finally {
if (pkClient! != null) await pkClient.stop();
}
Expand Down
13 changes: 4 additions & 9 deletions src/identities/CommandInvite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,10 @@ class CommandClaim extends CommandPolykey {
}),
auth,
);
process.stdout.write(
binUtils.outputFormatter({
type: options.format === 'json' ? 'json' : 'list',
data: [
`Successfully sent Gestalt Invite notification to Keynode with ID ${nodesUtils.encodeNodeId(
nodeId,
)}`,
],
}),
process.stderr.write(
`Successfully sent Gestalt Invite notification to Keynode with ID ${nodesUtils.encodeNodeId(
nodeId,
)}\n`,
);
} finally {
if (pkClient! != null) await pkClient.stop();
Expand Down
122 changes: 61 additions & 61 deletions src/identities/CommandList.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type PolykeyClient from 'polykey/dist/PolykeyClient';
import type { GestaltMessage } from 'polykey/dist/client/types';
import CommandPolykey from '../CommandPolykey';
import * as binOptions from '../utils/options';
import * as binUtils from '../utils';
Expand Down Expand Up @@ -42,72 +43,71 @@ class CommandList extends CommandPolykey {
},
logger: this.logger.getChild(PolykeyClient.name),
});
let output: any;
const gestalts = await binUtils.retryAuthentication(async (auth) => {
const gestalts: Array<any> = [];
const stream = await pkClient.rpcClient.methods.gestaltsGestaltList({
metadata: auth,
});
for await (const gestaltMessage of stream) {
const gestalt = gestaltMessage.gestalt;
const newGestalt: any = {
permissions: [],
nodes: [],
identities: [],
};
for (const node of Object.keys(gestalt.nodes)) {
const nodeInfo = gestalt.nodes[node];
newGestalt.nodes.push({ nodeId: nodeInfo.nodeId });
}
for (const identity of Object.keys(gestalt.identities)) {
const identityInfo = gestalt.identities[identity];
newGestalt.identities.push({
providerId: identityInfo.providerId,
identityId: identityInfo.identityId,
const gestaltMessages = await binUtils.retryAuthentication(
async (auth) => {
const gestaltMessages: Array<
GestaltMessage & { gestalt: { actionsList: Array<string> } }
> = [];
const stream = await pkClient.rpcClient.methods.gestaltsGestaltList(
{
metadata: auth,
},
);
for await (const gestaltMessage of stream) {
// Getting the permissions for the gestalt.
const actionsMessage = await binUtils.retryAuthentication(
(auth) =>
pkClient.rpcClient.methods.gestaltsActionsGetByNode({
metadata: auth,
nodeIdEncoded: Object.values(
gestaltMessage.gestalt.nodes,
)[0].nodeId,
}),
auth,
);
const actionsList = actionsMessage.actionsList;
gestaltMessages.push({
gestalt: {
...gestaltMessage.gestalt,
actionsList,
},
});
}
// Getting the permissions for the gestalt.
const actionsMessage = await binUtils.retryAuthentication(
(auth) =>
pkClient.rpcClient.methods.gestaltsActionsGetByNode({
metadata: auth,
nodeIdEncoded: newGestalt.nodes[0].nodeId,
}),
auth,
);
const actionList = actionsMessage.actionsList;
if (actionList.length === 0) newGestalt.permissions = null;
else newGestalt.permissions = actionList;
gestalts.push(newGestalt);
}
return gestalts;
}, auth);
output = gestalts;
if (options.format !== 'json') {
return gestaltMessages;
},
auth,
);
if (options.format === 'json') {
process.stdout.write(
binUtils.outputFormatter({
type: 'json',
data: gestaltMessages,
}),
);
} 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'}`);
// Listing nodes
for (const node of gestalt.nodes) {
output.push(`${node.nodeId}`);
}
// Listing identities
for (const identity of gestalt.identities) {
output.push(`${identity.providerId}:${identity.identityId}`);
}
output.push('');
count++;
for (const gestaltMessage of gestaltMessages) {
const gestalt = gestaltMessage.gestalt;
const nodeIds = Object.values(gestalt.nodes).map(
(node) => node.nodeId as string,
);
const identities = Object.values(gestalt.identities).map(
(identity) => `${identity.providerId}:${identity.identityId}`,
);
process.stdout.write(
binUtils.outputFormatter({
type: 'dict',
data: {
gestalt: {
actionsList: gestalt.actionsList.join(','),
identities,
nodeIds,
},
},
}),
);
}
}
process.stdout.write(
binUtils.outputFormatter({
type: options.format === 'json' ? 'json' : 'list',
data: output,
}),
);
} finally {
if (pkClient! != null) await pkClient.stop();
}
Expand Down
Loading

0 comments on commit da7af41

Please sign in to comment.