Skip to content

Commit

Permalink
fix(kadena-cli): Improved account list display formatting in account …
Browse files Browse the repository at this point in the history
…list command (#2160)

* fix(kadena-cli): Improved account list display formatting in accountList command

* test: fix typo in test case
  • Loading branch information
realdreamer committed May 28, 2024
1 parent 4e4fc7f commit e26cc1f
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 49 deletions.
5 changes: 5 additions & 0 deletions .changeset/brown-toys-divide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@kadena/kadena-cli": minor
---

Updated table generation and json output format to improve account information display
48 changes: 26 additions & 22 deletions packages/tools/kadena-cli/src/account/commands/accountList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@ import type { Command } from 'commander';
import { parse } from 'node:path';
import { NO_ACCOUNTS_FOUND_ERROR_MESSAGE } from '../../constants/account.js';
import { createCommand } from '../../utils/createCommand.js';
import {
isNotEmptyString,
maskStringPreservingStartAndEnd,
truncateText,
} from '../../utils/globalHelpers.js';
import { isNotEmptyString } from '../../utils/globalHelpers.js';
import { log } from '../../utils/logger.js';
import { createTable } from '../../utils/table.js';
import { accountOptions } from '../accountOptions.js';
Expand All @@ -19,21 +15,28 @@ import {
} from '../utils/accountHelpers.js';

function generateTabularData(accounts: IAliasAccountData[]): Table {
const table = createTable({
head: ['Alias', 'Name', 'Public Key(s)', 'Predicate', 'Fungible'],
});

const data = accounts.map((account) => [
truncateText(parse(account.alias).name, 32),
maskStringPreservingStartAndEnd(account.name, 32),
account.publicKeys
.map((key) => maskStringPreservingStartAndEnd(key, 24))
.join('\n'),
account.predicate,
account.fungible,
]);
const table = createTable({});
const header: Record<string, string> = {
publicKeys: 'Public Keys',
name: 'Account Name',
fungible: 'Fungible',
predicate: 'Predicate',
};

table.push(...data);
accounts.map((account) => {
table.push([
{ colSpan: 2, content: `Account Alias: ${parse(account.alias).name}` },
]);
Object.entries(account).forEach(([key, val]) => {
if (key === 'alias') return;
const value = key === 'publicKeys' ? val.join('\n') : val;
const headerKey = header[key] || key;
table.push({
[log.color.green(headerKey)]: value,
});
});
table.push([{ colSpan: 2, content: '' }]);
});

return table;
}
Expand Down Expand Up @@ -81,8 +84,9 @@ export const createAccountListCommand: (
return log.error(`Selected account alias "${accountAlias}" not found.`);
}

const tabularData = generateTabularData(accountsDetails);

log.output(tabularData.toString(), accountsDetails);
const data = generateTabularData(accountsDetails);
const accountsListJSONOutput =
accountsDetails.length === 1 ? accountsDetails[0] : accountsDetails;
log.output(data.toString(), accountsListJSONOutput);
},
);
50 changes: 23 additions & 27 deletions packages/tools/kadena-cli/src/account/tests/accountList.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ describe('account list', () => {
);
});

it('should display only selected account information', async () => {
it('should display information for the specified account only', async () => {
mockPrompts({
select: {
'Select an account (alias - account name):': 'account-one',
Expand All @@ -58,41 +58,37 @@ describe('account list', () => {

const res = await runCommandJson('account list');
expect(res).toEqual(
expect.arrayContaining([
expect.objectContaining({
alias: 'account-one.yaml',
fungible: 'coin',
name: 'k:55e10019549e047e68efaa18489ed785eca271642e2d0ce41d56ced2a30ccb84',
predicate: 'keys-all',
publicKeys: [
'55e10019549e047e68efaa18489ed785eca271642e2d0ce41d56ced2a30ccb84',
],
}),
]),
expect.objectContaining({
alias: 'account-one.yaml',
fungible: 'coin',
name: 'k:55e10019549e047e68efaa18489ed785eca271642e2d0ce41d56ced2a30ccb84',
predicate: 'keys-all',
publicKeys: [
'55e10019549e047e68efaa18489ed785eca271642e2d0ce41d56ced2a30ccb84',
],
}),
);
});

it('should display only passed account information through cli', async () => {
it('should display only the specified account information via CLI', async () => {
const res = await runCommandJson(
'account list --account-alias=account-two',
'account list --account-alias=account-two --quiet',
);
expect(res).toEqual(
expect.arrayContaining([
expect.objectContaining({
alias: 'account-two.yaml',
fungible: 'coin',
name: 'w:yCvUbeS6RqdKsY3WBDB3cgK-6q790xkj4Hb-ABpu3gg:keys-all',
predicate: 'keys-all',
publicKeys: [
'39710afef15243ba36007ae7aa210ab0e09682b2d963928be350e3424b5a420b',
'0f745a7773cbaffedcc7303b0638ffb34516aa3af98605f39dda3aeb730318c9',
],
}),
]),
expect.objectContaining({
alias: 'account-two.yaml',
fungible: 'coin',
name: 'w:yCvUbeS6RqdKsY3WBDB3cgK-6q790xkj4Hb-ABpu3gg:keys-all',
predicate: 'keys-all',
publicKeys: [
'39710afef15243ba36007ae7aa210ab0e09682b2d963928be350e3424b5a420b',
'0f745a7773cbaffedcc7303b0638ffb34516aa3af98605f39dda3aeb730318c9',
],
}),
);
});

it('should return account alias not found when user passes any random account alias', async () => {
it('should return "account alias not found" when a random account alias is provided', async () => {
const res = await runCommand(
'account list --account-alias=some-random-account-alias',
);
Expand Down

0 comments on commit e26cc1f

Please sign in to comment.