-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(kadena-cli): improve config show command to include all command …
…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
1 parent
381a766
commit e1c80f6
Showing
10 changed files
with
184 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 0 additions & 27 deletions
27
packages/tools/kadena-cli/src/config/commands/configPath.ts
This file was deleted.
Oops, something went wrong.
104 changes: 104 additions & 0 deletions
104
packages/tools/kadena-cli/src/config/commands/configShow.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 0 additions & 16 deletions
16
packages/tools/kadena-cli/src/config/tests/configPath.test.ts
This file was deleted.
Oops, something went wrong.
72 changes: 72 additions & 0 deletions
72
packages/tools/kadena-cli/src/config/tests/configShow.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters