diff --git a/README.md b/README.md index 82e1dc2..3a7d08a 100644 --- a/README.md +++ b/README.md @@ -36,7 +36,7 @@ $ aws-move-queue-messages With all optional CLI arguments: ```sh -$ aws-move-queue-messages -r [AWS-REGION] -y +$ aws-move-queue-messages -r [AWS-REGION] -m 100 -y ``` Any CLI argument or option you do not specify will fallback to a CLI prompt. The `-y` CLI option will answer the confirmation prompt automatically with "yes". diff --git a/src/helper.js b/src/helper.js index dc9ff35..eef8dd7 100644 --- a/src/helper.js +++ b/src/helper.js @@ -6,6 +6,16 @@ const validateUrl = (input) => { return valid; }; +const validateMaxMessages = (input) => { + let valid = true; + if (!input.match(/^[1-9][0-9]*$/)) { + valid = 'Please enter a valid max number of messages greater than 0!' + } + + return valid; +} + module.exports = { validateUrl, + validateMaxMessages }; diff --git a/src/index.js b/src/index.js index 9d6d313..2349903 100644 --- a/src/index.js +++ b/src/index.js @@ -3,7 +3,7 @@ const aws = require('aws-sdk'); const program = require('commander'); const { prompt } = require('inquirer'); -const { validateUrl } = require('./helper'); +const { validateMaxMessages, validateUrl } = require('./helper'); const { handle } = require('./main'); const { createClient } = require('./sqs'); @@ -28,6 +28,13 @@ const toQuestion = { validate: validateUrl, }; +const maxQuestion = { + type: 'input', + name: 'maxMessages', + message: 'Enter the max number of messages:', + validate: validateMaxMessages, +} + const handleAction = (from, to, options) => { const questions = []; @@ -35,6 +42,10 @@ const handleAction = (from, to, options) => { questions.push(regionQuestion); } + if (!options.maxMessages) { + questions.push(maxQuestion) + } + if (!from) { questions.push(fromQuestion); } @@ -45,6 +56,7 @@ const handleAction = (from, to, options) => { prompt(questions).then(async ({ awsRegion = options.region, + maxMessages = options.maxMessages, sourceQueueUrl = from, targetQueueUrl = to, }) => { @@ -55,6 +67,7 @@ const handleAction = (from, to, options) => { try { count = await handle({ + maxMessages, sourceQueueUrl, targetQueueUrl, sqs, @@ -73,6 +86,7 @@ const handleAction = (from, to, options) => { program .arguments('[from] [to]') .option('-r, --region [value]', 'The AWS region') + .option('-m, --maxMessages [value]', 'Max number of messages') .option('-y, --yes', 'Non interactive message moving') .action(handleAction) .parse(process.argv); diff --git a/src/main.js b/src/main.js index 9e84174..b01d782 100644 --- a/src/main.js +++ b/src/main.js @@ -1,6 +1,7 @@ const { Spinner } = require('clui'); const handle = async ({ + maxMessages, sourceQueueUrl, targetQueueUrl, sqs, @@ -10,16 +11,22 @@ const handle = async ({ const count = await sqs.getCount(sourceQueueUrl); await sqs.getCount(targetQueueUrl); - if (count === 0) { + if (parseInt(count) === 0) { throw new Error(`The queue ${sourceQueueUrl} is empty!`); } + maxMessages = parseInt(maxMessages); + moveCount = count + if(count > maxMessages) { + moveCount = maxMessages + } + if (!skipPrompt) { const { move } = await prompt([ { type: 'confirm', name: 'move', - message: `Do you want to move ${count} messages?`, + message: `Do you want to move ${moveCount} of ${count} messages?`, default: false, }, ]); @@ -29,12 +36,12 @@ const handle = async ({ } } - const spinner = new Spinner(`Moving ${count} messages...`); + const spinner = new Spinner(`Moving ${moveCount} messages...`); spinner.start(); const promises = []; - for (let i = 0; i < count; i += 1) { + for (let i = 0; i < moveCount; i += 1) { promises.push(sqs.moveMessage(sourceQueueUrl, targetQueueUrl)); } @@ -45,7 +52,7 @@ const handle = async ({ throw new Error(e.message); }); - return count; + return moveCount; }; module.exports = {