From 19a74074a7e229e060aef5a074580b2114ff5364 Mon Sep 17 00:00:00 2001 From: Lenin Mehedy Date: Thu, 22 Feb 2024 16:57:13 +1100 Subject: [PATCH] fix: polish up handler function Signed-off-by: Lenin Mehedy --- src/commands/account.mjs | 18 ++++++------------ src/commands/base.mjs | 28 ++++++++++++++++++---------- src/commands/cluster.mjs | 24 ++++++++---------------- src/commands/init.mjs | 4 ++-- src/commands/network.mjs | 18 ++++++------------ src/commands/node.mjs | 24 ++++++++---------------- src/commands/relay.mjs | 12 ++++-------- 7 files changed, 52 insertions(+), 76 deletions(-) diff --git a/src/commands/account.mjs b/src/commands/account.mjs index 6a87fc141..007f824b3 100644 --- a/src/commands/account.mjs +++ b/src/commands/account.mjs @@ -326,10 +326,8 @@ export class AccountCommand extends BaseCommand { flags.privateKey, flags.amount ), - handler: argv => BaseCommand.handleCommand( - argv, - async () => await accountCmd.create(argv), - accountCmd.logger + handler: argv => accountCmd.handleCommand(argv, + async (args) => await accountCmd.create(args) ) }) .command({ @@ -341,10 +339,8 @@ export class AccountCommand extends BaseCommand { flags.privateKey, flags.amount ), - handler: argv => BaseCommand.handleCommand( - argv, - async () => await accountCmd.update(argv), - accountCmd.logger + handler: argv => accountCmd.handleCommand(argv, + async (args) => await accountCmd.update(args) ) }) .command({ @@ -354,10 +350,8 @@ export class AccountCommand extends BaseCommand { flags.namespace, flags.accountId ), - handler: argv => BaseCommand.handleCommand( - argv, - async () => await accountCmd.get(argv), - accountCmd.logger + handler: argv => accountCmd.handleCommand(argv, + async (args) => await accountCmd.get(args) ) }) .demandCommand(1, 'Select an account command') diff --git a/src/commands/base.mjs b/src/commands/base.mjs index 1f4808d74..c6fa118ad 100644 --- a/src/commands/base.mjs +++ b/src/commands/base.mjs @@ -50,32 +50,40 @@ export class BaseCommand extends ShellRunner { this.depManager = opts.depManager } - static async handleCommand (argv, handleCmd, logger) { + /** + * Handle the execution of the command + * + * It ensures process file is locked before the handleFunc is called + * + * @param argv argv of the command + * @param handleFunc async function to be invoked + * @return {Promise} true if the execution succeeded + */ + async handleCommand (argv, handleFunc) { if (!argv) throw new MissingArgumentError('argv is required') - if (!handleCmd) throw new MissingArgumentError('handleCmd is required') - if (!logger) throw new MissingArgumentError('logger is required') + if (!handleFunc) throw new MissingArgumentError('handleFunc is required') let error = null try { - logger.debug(`==== Start: '${argv._.join(' ')}' ===`) - await ConfigManager.acquireProcessLock(logger) - await handleCmd() + this.logger.debug(`==== Start: '${argv._.join(' ')}' ===`) + await ConfigManager.acquireProcessLock(this.logger) + await handleFunc(argv) } catch (e) { error = new FullstackTestingError(`Error occurred: ${e.message}`, e) } finally { - await ConfigManager.releaseProcessLock(logger) - logger.debug(`==== End: '${argv._.join(' ')}' ===`) + await ConfigManager.releaseProcessLock(this.logger) + this.logger.debug(`==== End: '${argv._.join(' ')}' ===`) } if (error) { - logger.showUserError(error) + this.logger.showUserError(error) // do not exit immediately so that logger can flush properly setTimeout(() => { process.exit(1) }, 1) - return false + return false // we return false here, but process will exit with error code eventually. } return true diff --git a/src/commands/cluster.mjs b/src/commands/cluster.mjs index 5d6670dee..d5ffc1df8 100644 --- a/src/commands/cluster.mjs +++ b/src/commands/cluster.mjs @@ -224,19 +224,15 @@ export class ClusterCommand extends BaseCommand { .command({ command: 'list', desc: 'List all available clusters', - handler: argv => BaseCommand.handleCommand( - argv, - async () => await clusterCmd.showClusterList(argv), - clusterCmd.logger + handler: argv => clusterCmd.handleCommand(argv, + async (args) => await clusterCmd.showClusterList(args) ) }) .command({ command: 'info', desc: 'Get cluster info', - handler: argv => BaseCommand.handleCommand( - argv, - async () => await clusterCmd.getClusterInfo(argv), - clusterCmd.logger + handler: argv => clusterCmd.handleCommand(argv, + async (args) => await clusterCmd.getClusterInfo(args) ) }) .command({ @@ -252,10 +248,8 @@ export class ClusterCommand extends BaseCommand { flags.deployCertManagerCrds, flags.fstChartVersion ), - handler: argv => BaseCommand.handleCommand( - argv, - async () => await clusterCmd.setup(argv), - clusterCmd.logger + handler: argv => clusterCmd.handleCommand(argv, + async (args) => await clusterCmd.setup(args) ) }) .command({ @@ -265,10 +259,8 @@ export class ClusterCommand extends BaseCommand { flags.clusterName, flags.clusterSetupNamespace ), - handler: argv => BaseCommand.handleCommand( - argv, - async () => await clusterCmd.reset(argv), - clusterCmd.logger + handler: argv => clusterCmd.handleCommand(argv, + async (args) => await clusterCmd.reset(args) ) }) .demandCommand(1, 'Select a cluster command') diff --git a/src/commands/init.mjs b/src/commands/init.mjs index eb73a7d2c..722f27787 100644 --- a/src/commands/init.mjs +++ b/src/commands/init.mjs @@ -162,9 +162,9 @@ export class InitCommand extends BaseCommand { flags.keyFormat ) }, - handler: argv => BaseCommand.handleCommand( + handler: argv => initCmd.handleCommand( argv, - async () => await initCmd.init(argv), + async (args) => await initCmd.init(args), initCmd.logger ) } diff --git a/src/commands/network.mjs b/src/commands/network.mjs index b53d98bea..0f842df2d 100644 --- a/src/commands/network.mjs +++ b/src/commands/network.mjs @@ -366,10 +366,8 @@ export class NetworkCommand extends BaseCommand { flags.enablePrometheusSvcMonitor, flags.fstChartVersion ), - handler: argv => BaseCommand.handleCommand( - argv, - async () => await networkCmd.deploy(argv), - networkCmd.logger + handler: argv => networkCmd.handleCommand(argv, + async (args) => await networkCmd.deploy(args) ) }) .command({ @@ -380,10 +378,8 @@ export class NetworkCommand extends BaseCommand { flags.force, flags.deletePvcs ), - handler: argv => BaseCommand.handleCommand( - argv, - async () => await networkCmd.destroy(argv), - networkCmd.logger + handler: argv => networkCmd.handleCommand(argv, + async (args) => await networkCmd.destroy(args) ) }) .command({ @@ -401,10 +397,8 @@ export class NetworkCommand extends BaseCommand { flags.hederaExplorerTlsHostName, flags.enablePrometheusSvcMonitor ), - handler: argv => BaseCommand.handleCommand( - argv, - async () => await networkCmd.refresh(argv), - networkCmd.logger + handler: argv => networkCmd.handleCommand(argv, + async (args) => await networkCmd.refresh(args) ) }) .demandCommand(1, 'Select a chart command') diff --git a/src/commands/node.mjs b/src/commands/node.mjs index f01d8a0c8..29b3435f3 100644 --- a/src/commands/node.mjs +++ b/src/commands/node.mjs @@ -729,10 +729,8 @@ export class NodeCommand extends BaseCommand { flags.settingTxt, flags.log4j2Xml ), - handler: argv => BaseCommand.handleCommand( - argv, - async () => await nodeCmd.setup(argv), - nodeCmd.logger + handler: argv => nodeCmd.handleCommand(argv, + async (args) => await nodeCmd.setup(args) ) }) .command({ @@ -743,10 +741,8 @@ export class NodeCommand extends BaseCommand { flags.nodeIDs, flags.updateAccountKeys ), - handler: argv => BaseCommand.handleCommand( - argv, - async () => await nodeCmd.start(argv), - nodeCmd.logger + handler: argv => nodeCmd.handleCommand(argv, + async (args) => await nodeCmd.start(args) ) }) .command({ @@ -756,10 +752,8 @@ export class NodeCommand extends BaseCommand { flags.namespace, flags.nodeIDs ), - handler: argv => BaseCommand.handleCommand( - argv, - async () => await nodeCmd.stop(argv), - nodeCmd.logger + handler: argv => nodeCmd.handleCommand(argv, + async (args) => await nodeCmd.stop(args) ) }) .command({ @@ -772,10 +766,8 @@ export class NodeCommand extends BaseCommand { flags.generateTlsKeys, flags.keyFormat ), - handler: argv => BaseCommand.handleCommand( - argv, - async () => await nodeCmd.keys(argv), - nodeCmd.logger + handler: argv => nodeCmd.handleCommand(argv, + async (args) => await nodeCmd.keys(args) ) }) .demandCommand(1, 'Select a node command') diff --git a/src/commands/relay.mjs b/src/commands/relay.mjs index d060106f3..9d4b2d387 100644 --- a/src/commands/relay.mjs +++ b/src/commands/relay.mjs @@ -236,10 +236,8 @@ export class RelayCommand extends BaseCommand { flags.operatorKey ) }, - handler: argv => BaseCommand.handleCommand( - argv, - async () => await relayCmd.install(argv), - relayCmd.logger + handler: argv => relayCmd.handleCommand(argv, + async (args) => await relayCmd.install(args) ) }) .command({ @@ -249,10 +247,8 @@ export class RelayCommand extends BaseCommand { flags.namespace, flags.nodeIDs ), - handler: argv => BaseCommand.handleCommand( - argv, - async () => await relayCmd.uninstall(argv), - relayCmd.logger + handler: argv => relayCmd.handleCommand(argv, + async (args) => await relayCmd.uninstall(args) ) }) .demandCommand(1, 'Select a relay command')