Skip to content

Commit

Permalink
fix: polish up handler function
Browse files Browse the repository at this point in the history
Signed-off-by: Lenin Mehedy <[email protected]>
  • Loading branch information
leninmehedy committed Feb 22, 2024
1 parent 4abb500 commit 19a7407
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 76 deletions.
18 changes: 6 additions & 12 deletions src/commands/account.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Check warning on line 330 in src/commands/account.mjs

View check run for this annotation

Codecov / codecov/patch

src/commands/account.mjs#L329-L330

Added lines #L329 - L330 were not covered by tests
)
})
.command({
Expand All @@ -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)

Check warning on line 343 in src/commands/account.mjs

View check run for this annotation

Codecov / codecov/patch

src/commands/account.mjs#L342-L343

Added lines #L342 - L343 were not covered by tests
)
})
.command({
Expand All @@ -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)

Check warning on line 354 in src/commands/account.mjs

View check run for this annotation

Codecov / codecov/patch

src/commands/account.mjs#L353-L354

Added lines #L353 - L354 were not covered by tests
)
})
.demandCommand(1, 'Select an account command')
Expand Down
28 changes: 18 additions & 10 deletions src/commands/base.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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<boolean>} true if the execution succeeded
*/
async handleCommand (argv, handleFunc) {

Check warning on line 62 in src/commands/base.mjs

View check run for this annotation

Codecov / codecov/patch

src/commands/base.mjs#L62

Added line #L62 was not covered by tests
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)

Check warning on line 70 in src/commands/base.mjs

View check run for this annotation

Codecov / codecov/patch

src/commands/base.mjs#L66-L70

Added lines #L66 - L70 were not covered by tests
} catch (e) {
error = new FullstackTestingError(`Error occurred: ${e.message}`, e)

Check warning on line 72 in src/commands/base.mjs

View check run for this annotation

Codecov / codecov/patch

src/commands/base.mjs#L72

Added line #L72 was not covered by tests
} finally {
await ConfigManager.releaseProcessLock(logger)
logger.debug(`==== End: '${argv._.join(' ')}' ===`)
await ConfigManager.releaseProcessLock(this.logger)
this.logger.debug(`==== End: '${argv._.join(' ')}' ===`)

Check warning on line 75 in src/commands/base.mjs

View check run for this annotation

Codecov / codecov/patch

src/commands/base.mjs#L74-L75

Added lines #L74 - L75 were not covered by tests
}

if (error) {
logger.showUserError(error)
this.logger.showUserError(error)

Check warning on line 79 in src/commands/base.mjs

View check run for this annotation

Codecov / codecov/patch

src/commands/base.mjs#L79

Added line #L79 was not covered by tests

// do not exit immediately so that logger can flush properly
setTimeout(() => {
process.exit(1)

Check warning on line 83 in src/commands/base.mjs

View check run for this annotation

Codecov / codecov/patch

src/commands/base.mjs#L82-L83

Added lines #L82 - L83 were not covered by tests
}, 1)

return false
return false // we return false here, but process will exit with error code eventually.

Check warning on line 86 in src/commands/base.mjs

View check run for this annotation

Codecov / codecov/patch

src/commands/base.mjs#L86

Added line #L86 was not covered by tests
}

return true

Check warning on line 89 in src/commands/base.mjs

View check run for this annotation

Codecov / codecov/patch

src/commands/base.mjs#L89

Added line #L89 was not covered by tests
Expand Down
24 changes: 8 additions & 16 deletions src/commands/cluster.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Check warning on line 228 in src/commands/cluster.mjs

View check run for this annotation

Codecov / codecov/patch

src/commands/cluster.mjs#L227-L228

Added lines #L227 - L228 were not covered by tests
)
})
.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)

Check warning on line 235 in src/commands/cluster.mjs

View check run for this annotation

Codecov / codecov/patch

src/commands/cluster.mjs#L234-L235

Added lines #L234 - L235 were not covered by tests
)
})
.command({
Expand All @@ -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)

Check warning on line 252 in src/commands/cluster.mjs

View check run for this annotation

Codecov / codecov/patch

src/commands/cluster.mjs#L251-L252

Added lines #L251 - L252 were not covered by tests
)
})
.command({
Expand All @@ -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)

Check warning on line 263 in src/commands/cluster.mjs

View check run for this annotation

Codecov / codecov/patch

src/commands/cluster.mjs#L262-L263

Added lines #L262 - L263 were not covered by tests
)
})
.demandCommand(1, 'Select a cluster command')
Expand Down
4 changes: 2 additions & 2 deletions src/commands/init.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,9 @@ export class InitCommand extends BaseCommand {
flags.keyFormat
)
},
handler: argv => BaseCommand.handleCommand(
handler: argv => initCmd.handleCommand(

Check warning on line 165 in src/commands/init.mjs

View check run for this annotation

Codecov / codecov/patch

src/commands/init.mjs#L165

Added line #L165 was not covered by tests
argv,
async () => await initCmd.init(argv),
async (args) => await initCmd.init(args),

Check warning on line 167 in src/commands/init.mjs

View check run for this annotation

Codecov / codecov/patch

src/commands/init.mjs#L167

Added line #L167 was not covered by tests
initCmd.logger
)
}
Expand Down
18 changes: 6 additions & 12 deletions src/commands/network.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Check warning on line 370 in src/commands/network.mjs

View check run for this annotation

Codecov / codecov/patch

src/commands/network.mjs#L369-L370

Added lines #L369 - L370 were not covered by tests
)
})
.command({
Expand All @@ -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)

Check warning on line 382 in src/commands/network.mjs

View check run for this annotation

Codecov / codecov/patch

src/commands/network.mjs#L381-L382

Added lines #L381 - L382 were not covered by tests
)
})
.command({
Expand All @@ -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)

Check warning on line 401 in src/commands/network.mjs

View check run for this annotation

Codecov / codecov/patch

src/commands/network.mjs#L400-L401

Added lines #L400 - L401 were not covered by tests
)
})
.demandCommand(1, 'Select a chart command')
Expand Down
24 changes: 8 additions & 16 deletions src/commands/node.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Check warning on line 733 in src/commands/node.mjs

View check run for this annotation

Codecov / codecov/patch

src/commands/node.mjs#L732-L733

Added lines #L732 - L733 were not covered by tests
)
})
.command({
Expand All @@ -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)

Check warning on line 745 in src/commands/node.mjs

View check run for this annotation

Codecov / codecov/patch

src/commands/node.mjs#L744-L745

Added lines #L744 - L745 were not covered by tests
)
})
.command({
Expand All @@ -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)

Check warning on line 756 in src/commands/node.mjs

View check run for this annotation

Codecov / codecov/patch

src/commands/node.mjs#L755-L756

Added lines #L755 - L756 were not covered by tests
)
})
.command({
Expand All @@ -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)

Check warning on line 770 in src/commands/node.mjs

View check run for this annotation

Codecov / codecov/patch

src/commands/node.mjs#L769-L770

Added lines #L769 - L770 were not covered by tests
)
})
.demandCommand(1, 'Select a node command')
Expand Down
12 changes: 4 additions & 8 deletions src/commands/relay.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Check warning on line 240 in src/commands/relay.mjs

View check run for this annotation

Codecov / codecov/patch

src/commands/relay.mjs#L239-L240

Added lines #L239 - L240 were not covered by tests
)
})
.command({
Expand All @@ -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)

Check warning on line 251 in src/commands/relay.mjs

View check run for this annotation

Codecov / codecov/patch

src/commands/relay.mjs#L250-L251

Added lines #L250 - L251 were not covered by tests
)
})
.demandCommand(1, 'Select a relay command')
Expand Down

0 comments on commit 19a7407

Please sign in to comment.