Skip to content

Commit

Permalink
feat(kadena-cli): improve config show command to include all command …
Browse files Browse the repository at this point in the history
…paths and counts of wallets, accounts, networks, templates (#2167)

* feat(kadena-cli): improve config show command to include all command paths and number of files

* test: add tests for config show command

* test: cleanup verbose option from tests

* chore: review feedback

* chore: prettier format

* test: update tests after transfer create template changes
  • Loading branch information
realdreamer committed May 29, 2024
1 parent 381a766 commit e1c80f6
Show file tree
Hide file tree
Showing 10 changed files with 184 additions and 50 deletions.
6 changes: 6 additions & 0 deletions .changeset/early-apples-jog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@kadena/kadena-cli": minor
---

Rename config path command to config show command
Config show command to include all commands directory paths and counts of different resources like wallets, accounts, networks etc.
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ describe('account details', () => {
'Enter a ChainId (0-19) (comma or hyphen separated e.g 0,1,2 or 1-5 or all):':
'0',
},
verbose: true,
});
const res = await runCommandJson('account details');
expect(res).toEqual(accountDetailsSuccessData.result.data);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ describe('account fund', () => {
'1',
'Enter an amount:': '5',
},
verbose: true,
});

const res = await runCommand('account fund');
Expand Down
27 changes: 0 additions & 27 deletions packages/tools/kadena-cli/src/config/commands/configPath.ts

This file was deleted.

104 changes: 104 additions & 0 deletions packages/tools/kadena-cli/src/config/commands/configShow.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import type { Table } from 'cli-table3';
import type { Command } from 'commander';
import path from 'node:path';
import {
ACCOUNT_DIR,
NETWORKS_DIR,
TX_TEMPLATE_FOLDER,
WALLET_DIR,
} from '../../constants/config.js';
import { services } from '../../services/index.js';
import { KadenaError } from '../../services/service-error.js';
import { createCommand } from '../../utils/createCommand.js';
import { getDefaultNetworkName } from '../../utils/helpers.js';
import { log } from '../../utils/logger.js';
import { createTable } from '../../utils/table.js';

const getConfigPaths = (kadenaDir: string): Record<string, string> => ({
configDirectory: kadenaDir,
walletDirectory: path.join(kadenaDir, WALLET_DIR),
defaultTemplateDirectory: path.join(kadenaDir, TX_TEMPLATE_FOLDER),
networkDirectory: path.join(kadenaDir, NETWORKS_DIR),
accountDirectory: path.join(kadenaDir, ACCOUNT_DIR),
});

const getNumberOfFiles = async (directory: string): Promise<number> => {
return (await services.filesystem.readDir(directory).catch(() => [])).length;
};

const calculateDirectoryFileCounts = async (
config: Record<string, string>,
): Promise<Record<string, number>> => {
const [
numberOfWallets,
numberOfTemplates,
numberOfNetworks,
numberOfAccounts,
] = await Promise.all([
getNumberOfFiles(config.walletDirectory),
getNumberOfFiles(config.defaultTemplateDirectory),
getNumberOfFiles(config.networkDirectory),
getNumberOfFiles(config.accountDirectory),
]);

return {
numberOfWallets,
numberOfTemplates,
numberOfNetworks,
numberOfAccounts,
};
};

const generateTabularData = async (
config: Record<string, string | number>,
): Promise<Table> => {
const table = createTable({});
table.push(
{ [log.color.green('Config path')]: config.configDirectory },
{ [log.color.green('Wallet path')]: config.walletDirectory },
{ [log.color.green('Number of wallets')]: config.numberOfWallets },
{
[log.color.green('Default template path')]:
config.defaultTemplateDirectory,
},
{ [log.color.green('Number of templates')]: config.numberOfTemplates },
{ [log.color.green('Network path')]: config.networkDirectory },
{ [log.color.green('Number of networks')]: config.numberOfNetworks },
{ [log.color.green('Default network')]: config.defaultNetwork },
{ [log.color.green('Account path')]: config.accountDirectory },
{ [log.color.green('Number of accounts')]: config.numberOfAccounts },
);

return table;
};

export const createConfigShowCommand: (
program: Command,
version: string,
) => void = createCommand(
'show',
'Displays the current config location and counts of different resources like wallets, templates, networks, accounts and etc.)',
[],
async () => {
log.debug('config show');

const kadenaDir = services.config.getDirectory();
if (kadenaDir === null) {
throw new KadenaError('no_kadena_directory');
}

log.info(log.color.green('Currently using the following config:'));
const configPaths = getConfigPaths(kadenaDir);
const directoryFileCounts = await calculateDirectoryFileCounts(configPaths);
const defaultNetwork = (await getDefaultNetworkName()) ?? 'N/A';

const config = {
...configPaths,
...directoryFileCounts,
defaultNetwork,
};
const table = await generateTabularData(config);

log.output(table.toString(), config);
},
);
4 changes: 2 additions & 2 deletions packages/tools/kadena-cli/src/config/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createConfigInitCommand } from './commands/configInit.js';

import type { Command } from 'commander';
import { createConfigPathCommand } from './commands/configPath.js';
import { createConfigShowCommand } from './commands/configShow.js';

/**
* Represents the root command for the configuration CLI.
Expand All @@ -26,5 +26,5 @@ export function configCommandFactory(program: Command, version: string): void {
all projects bootstrap comes from this configuration
*/
createConfigInitCommand(configProgram, version);
createConfigPathCommand(configProgram, version);
createConfigShowCommand(configProgram, version);
}
16 changes: 0 additions & 16 deletions packages/tools/kadena-cli/src/config/tests/configPath.test.ts

This file was deleted.

72 changes: 72 additions & 0 deletions packages/tools/kadena-cli/src/config/tests/configShow.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import path from 'node:path';
import { describe, expect, it, vi } from 'vitest';
import {
ACCOUNT_DIR,
NETWORKS_DIR,
TX_TEMPLATE_FOLDER,
WALLET_DIR,
} from '../../constants/config.js';
import { services } from '../../services/index.js';
import { runCommand, runCommandJson } from '../../utils/test.util.js';

describe('config show', () => {
const configDirectory = process.env.KADENA_DIR!;
it('should return the default config paths and values based on test setup', async () => {
// as part of test setup, we have already created the default config
// with 2 templates and 3 networks
const output = await runCommandJson('config show');
expect(output).toEqual({
configDirectory,
walletDirectory: path.join(configDirectory, WALLET_DIR),
defaultTemplateDirectory: path.join(configDirectory, TX_TEMPLATE_FOLDER),
networkDirectory: path.join(configDirectory, NETWORKS_DIR),
accountDirectory: path.join(configDirectory, ACCOUNT_DIR),
numberOfWallets: 0,
numberOfTemplates: 3,
numberOfNetworks: 3,
numberOfAccounts: 0,
defaultNetwork: 'N/A',
});
});

it('should return wallets and accounts count after creating a wallet and account', async () => {
// creating a wallet
await runCommandJson('wallet add -w test --quiet', {
stdin: '12345678',
});
// creating an account
await runCommand(
'account add --from=key --account-alias=account-one --account-name=k:55e10019549e047e68efaa18489ed785eca271642e2d0ce41d56ced2a30ccb84 --fungible=coin --network=testnet --chain-id=1 --public-keys=55e10019549e047e68efaa18489ed785eca271642e2d0ce41d56ced2a30ccb84 --quiet',
);
const output = await runCommandJson('config show');
expect(output).toEqual({
configDirectory,
walletDirectory: path.join(configDirectory, WALLET_DIR),
defaultTemplateDirectory: path.join(configDirectory, TX_TEMPLATE_FOLDER),
networkDirectory: path.join(configDirectory, NETWORKS_DIR),
accountDirectory: path.join(configDirectory, ACCOUNT_DIR),
numberOfWallets: 1,
numberOfTemplates: 3,
numberOfNetworks: 3,
numberOfAccounts: 1,
defaultNetwork: 'N/A',
});
});

it('should return the default network name when it exists', async () => {
// setting the default network
await runCommand('network set-default --network=testnet --confirm --quiet');
const output = await runCommandJson('config show');
expect(output.defaultNetwork).toEqual('testnet');
});

it('should return warning message to run "kadena config init" command when there is no configDirectory', async () => {
const mockGetDirectory = vi.fn().mockReturnValue(null);
services.config.getDirectory = mockGetDirectory;
const { stderr } = await runCommand('config show');
expect(stderr).toContain(
'No kadena directory found. Run the following command to create one:',
);
expect(stderr).toContain('kadena config init');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ describe('network delete command', () => {
'Are you sure you want to delete the configuration for network "devnet"?\n type "yes" to confirm or "no" to cancel and press enter.':
'yes',
},
verbose: true,
});

await runCommand('network delete');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ describe('network update command', () => {
'Enter Kadena network explorer URL (e.g. "https://explorer.chainweb.com/mainnet/tx/"):':
'',
},
verbose: true,
});
await runCommand('network update');
const content = await services.filesystem.readFile(networkFilePath);
Expand Down Expand Up @@ -82,7 +81,6 @@ describe('network update command', () => {
'Enter Kadena network explorer URL (e.g. "https://explorer.chainweb.com/mainnet/tx/"):':
'',
},
verbose: true,
});
await runCommand('network update');
expect(await services.filesystem.fileExists(networkFilePath)).toBe(false);
Expand Down

0 comments on commit e1c80f6

Please sign in to comment.