From b7c137aef0ccd6326aec0972abb2833c8de73c9b Mon Sep 17 00:00:00 2001 From: npty Date: Wed, 17 Jul 2024 10:04:51 +0700 Subject: [PATCH] chore: refactor cli --- sui/cli-utils.js | 9 +++++++++ sui/gas-service.js | 23 ++++++++++------------- 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/sui/cli-utils.js b/sui/cli-utils.js index 1ef5569b7..bb72d5f6f 100644 --- a/sui/cli-utils.js +++ b/sui/cli-utils.js @@ -51,7 +51,16 @@ const addExtendedOptions = (program, options = {}) => { return program; }; +const addBaseOptionsToCommands = (program, options = {}) => { + if (program.commands.length > 0) { + program.commands.forEach((command) => { + addBaseOptions(command, options); + }); + } +}; + module.exports = { addBaseOptions, addExtendedOptions, + addBaseOptionsToCommands, }; diff --git a/sui/gas-service.js b/sui/gas-service.js index 136c7fbd9..cbe980ac4 100644 --- a/sui/gas-service.js +++ b/sui/gas-service.js @@ -10,7 +10,7 @@ const { utils: { arrayify }, } = ethers; -const { addBaseOptions } = require('./cli-utils'); +const { addBaseOptionsToCommands } = require('./cli-utils'); const { getWallet, printWalletInfo, broadcast } = require('./sign-utils'); async function payGas(config, chain, args, options) { @@ -202,7 +202,7 @@ if (require.main === module) { program.name('gas-service').description('Interact with the gas service contract.'); - const payGasProgram = new Command() + const payGasCmd = new Command() .command('pay_gas ') .description('Pay gas for the new contract call.') .requiredOption('--channel ', 'Existing channel ID to initiate a cross-chain message over') @@ -212,7 +212,7 @@ if (require.main === module) { mainProcessor('pay_gas', options, [amount, destinationChain, destinationAddress, payload], processCommand); }); - const addGasProgram = new Command() + const addGasCmd = new Command() .command('add_gas ') .description('Add gas for the existing contract call.') .option('--refund_address ', 'Refund address.') @@ -221,7 +221,7 @@ if (require.main === module) { mainProcessor('add_gas', options, [messageId, amount], processCommand); }); - const collectGasProgram = new Command() + const collectGasCmd = new Command() .command('collect_gas ') .description('Collect gas from the gas service contract.') .option('--receiver ', 'Receiver address. Default is the sender address.') @@ -229,7 +229,7 @@ if (require.main === module) { mainProcessor('collect_gas', options, [amount], processCommand); }); - const refundProgram = new Command() + const refundCmd = new Command() .command('refund ') .description('Refund gas from the gas service contract.') .option('--receiver ', 'Receiver address. Default is the sender address.') @@ -237,15 +237,12 @@ if (require.main === module) { mainProcessor('refund', options, [messageId, amount], processCommand); }); - program.addCommand(payGasProgram); - program.addCommand(addGasProgram); - program.addCommand(collectGasProgram); - program.addCommand(refundProgram); + program.addCommand(payGasCmd); + program.addCommand(addGasCmd); + program.addCommand(collectGasCmd); + program.addCommand(refundCmd); - addBaseOptions(payGasProgram); - addBaseOptions(addGasProgram); - addBaseOptions(collectGasProgram); - addBaseOptions(refundProgram); + addBaseOptionsToCommands(program); program.parse(); }