Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
addievo committed Oct 31, 2023
1 parent 75905fc commit 32c474a
Show file tree
Hide file tree
Showing 17 changed files with 231 additions and 198 deletions.
38 changes: 18 additions & 20 deletions src/nodes/CommandClaim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,27 +65,25 @@ class CommandClaim extends CommandPolykey {
);
const claimed = response.success;
if (claimed) {
process.stdout.write(
binUtils.outputFormatter({
type: options.format === 'json' ? 'json' : 'list',
data: [
`Successfully generated a cryptolink claim on Keynode with ID ${nodesUtils.encodeNodeId(
nodeId,
)}`,
],
}),
);
const formattedOutput = await 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);
} else {
process.stdout.write(
binUtils.outputFormatter({
type: options.format === 'json' ? 'json' : 'list',
data: [
`Successfully sent Gestalt Invite notification to Keynode with ID ${nodesUtils.encodeNodeId(
nodeId,
)}`,
],
}),
);
const formattedOutput = await 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);
}
} finally {
if (pkClient! != null) await pkClient.stop();
Expand Down
87 changes: 22 additions & 65 deletions src/nodes/CommandConnections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,72 +60,29 @@ class CommandAdd extends CommandPolykey {
return connectionEntries;
}, auth);
if (options.format === 'human') {
const output: Array<string> = [];

// Initialize variables to hold the maximum length for each column
let maxHostStringLength = 'NodeHost'.length;
let maxUsageCountLength = 'UsageCount'.length;
let maxTimeoutLength = 'Timeout'.length;

// Loop through the connections to find the maximum length for each column
for (const connection of connections) {
const hostnameString =
connection.hostname === '' ? '' : `(${connection.hostname})`;
const hostString = `${connection.nodeIdEncoded}@${connection.host}${hostnameString}:${connection.port}`;
const usageCount = connection.usageCount.toString();
const timeout =
connection.timeout === -1 ? 'NA' : `${connection.timeout}`;

if (hostString.length > maxHostStringLength) {
maxHostStringLength = hostString.length;
}
if (usageCount.length > maxUsageCountLength) {
maxUsageCountLength = usageCount.length;
}
if (timeout.length > maxTimeoutLength) {
maxTimeoutLength = timeout.length;
}
}

// Create the header line with proper padding
const headerLine =
'NodeHost'.padEnd(maxHostStringLength) +
'\t' +
'UsageCount'.padEnd(maxUsageCountLength) +
'\t' +
'Timeout'.padEnd(maxTimeoutLength);
output.push(headerLine);

// Create the data lines with proper padding
for (const connection of connections) {
const hostnameString =
connection.hostname === '' ? '' : `(${connection.hostname})`;
const hostString = `${connection.nodeIdEncoded}@${connection.host}${hostnameString}:${connection.port}`;
const usageCount = connection.usageCount.toString();
const timeout =
connection.timeout === -1 ? 'NA' : `${connection.timeout}`;

const outputLine =
hostString.padEnd(maxHostStringLength) +
'\t' +
usageCount.padEnd(maxUsageCountLength) +
'\t' +
timeout.padEnd(maxTimeoutLength);
output.push(outputLine);
}
process.stdout.write(
binUtils.outputFormatter({
type: 'list',
data: output,
}),
);
// Wait for outputFormatter to complete and then write to stdout
const formattedOutput = await binUtils.outputFormatter({
type: 'table',
data: connections,
options: {
headers: [
'host',
'hostname',
'nodeIdEncoded',
'port',
'timeout',
'usageCount',
],
},
});
process.stdout.write(formattedOutput);
} else {
process.stdout.write(
binUtils.outputFormatter({
type: 'json',
data: connections,
}),
);
// Wait for outputFormatter to complete and then write to stdout
const formattedOutput = await binUtils.outputFormatter({
type: 'json',
data: connections,
});
process.stdout.write(formattedOutput);
}
} finally {
if (pkClient! != null) await pkClient.stop();
Expand Down
11 changes: 5 additions & 6 deletions src/nodes/CommandFind.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,11 @@ class CommandFind extends CommandPolykey {
}
let output: any = result;
if (options.format === 'human') output = [result.message];
process.stdout.write(
binUtils.outputFormatter({
type: options.format === 'json' ? 'json' : 'list',
data: output,
}),
);
const formattedOutput = await binUtils.outputFormatter({
type: options.format === 'json' ? 'json' : 'list',
data: output,
});
process.stdout.write(formattedOutput);
// Like ping it should error when failing to find node for automation reasons.
if (!result.success) {
throw new errors.ErrorPolykeyCLINodeFindFailed(result.message);
Expand Down
11 changes: 5 additions & 6 deletions src/nodes/CommandGetAll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,11 @@ class CommandGetAll extends CommandPolykey {
`NodeId ${value.nodeIdEncoded}, Address ${value.host}:${value.port}, bucketIndex ${value.bucketIndex}`,
);
}
process.stdout.write(
binUtils.outputFormatter({
type: options.format === 'json' ? 'json' : 'list',
data: output,
}),
);
const formattedOutput = await binUtils.outputFormatter({
type: options.format === 'json' ? 'json' : 'list',
data: output,
});
process.stdout.write(formattedOutput);
} finally {
if (pkClient! != null) await pkClient.stop();
}
Expand Down
12 changes: 6 additions & 6 deletions src/nodes/CommandPing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,12 @@ class CommandPing extends CommandPolykey {
else status.message = error.message;
const output: any =
options.format === 'json' ? status : [status.message];
process.stdout.write(
binUtils.outputFormatter({
type: options.format === 'json' ? 'json' : 'list',
data: output,
}),
);
const formattedOutput = await binUtils.outputFormatter({
type: options.format === 'json' ? 'json' : 'list',
data: output,
});
process.stdout.write(formattedOutput);

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

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

process.stdout.write(formattedOutput);
} finally {
if (pkClient! != null) await pkClient.stop();
}
Expand Down
14 changes: 7 additions & 7 deletions src/secrets/CommandStat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,13 @@ class CommandStat extends CommandPolykey {
data.push(`${key}: ${value}`);
}

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

process.stdout.write(formattedOutput);
} finally {
if (pkClient! != null) await pkClient.stop();
}
Expand Down
9 changes: 9 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,17 @@ type AgentChildProcessOutput =
error: POJO;
};

type TableRow = Record<string, any>;

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

export type {
AgentStatusLiveData,
AgentChildProcessInput,
AgentChildProcessOutput,
TableRow,
TableOptions,
};
Loading

0 comments on commit 32c474a

Please sign in to comment.