Skip to content

Commit 5305223

Browse files
author
Troy Jones
committed
Add option to specify max number of messages to move: resolves joblocal#19
1 parent f73a8c6 commit 5305223

File tree

4 files changed

+38
-7
lines changed

4 files changed

+38
-7
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ $ aws-move-queue-messages
3636
With all optional CLI arguments:
3737

3838
```sh
39-
$ aws-move-queue-messages <from-queue-url> <to-queue-url> -r [AWS-REGION] -y
39+
$ aws-move-queue-messages <from-queue-url> <to-queue-url> -r [AWS-REGION] -m 100 -y
4040
```
4141

4242
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".

src/helper.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,16 @@ const validateUrl = (input) => {
66
return valid;
77
};
88

9+
const validateMaxMessages = (input) => {
10+
let valid = true;
11+
if (!input.match(/^[1-9][0-9]*$/)) {
12+
valid = 'Please enter a valid max number of messages greater than 0!'
13+
}
14+
15+
return valid;
16+
}
17+
918
module.exports = {
1019
validateUrl,
20+
validateMaxMessages
1121
};

src/index.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
const aws = require('aws-sdk');
44
const program = require('commander');
55
const { prompt } = require('inquirer');
6-
const { validateUrl } = require('./helper');
6+
const { validateMaxMessages, validateUrl } = require('./helper');
77
const { handle } = require('./main');
88
const { createClient } = require('./sqs');
99

@@ -28,13 +28,24 @@ const toQuestion = {
2828
validate: validateUrl,
2929
};
3030

31+
const maxQuestion = {
32+
type: 'input',
33+
name: 'maxMessages',
34+
message: 'Enter the max number of messages:',
35+
validate: validateMaxMessages,
36+
}
37+
3138
const handleAction = (from, to, options) => {
3239
const questions = [];
3340

3441
if (!options.region) {
3542
questions.push(regionQuestion);
3643
}
3744

45+
if (!options.maxMessages) {
46+
questions.push(maxQuestion)
47+
}
48+
3849
if (!from) {
3950
questions.push(fromQuestion);
4051
}
@@ -45,6 +56,7 @@ const handleAction = (from, to, options) => {
4556

4657
prompt(questions).then(async ({
4758
awsRegion = options.region,
59+
maxMessages = options.maxMessages,
4860
sourceQueueUrl = from,
4961
targetQueueUrl = to,
5062
}) => {
@@ -55,6 +67,7 @@ const handleAction = (from, to, options) => {
5567

5668
try {
5769
count = await handle({
70+
maxMessages,
5871
sourceQueueUrl,
5972
targetQueueUrl,
6073
sqs,
@@ -73,6 +86,7 @@ const handleAction = (from, to, options) => {
7386
program
7487
.arguments('[from] [to]')
7588
.option('-r, --region [value]', 'The AWS region')
89+
.option('-m, --maxMessages [value]', 'Max number of messages')
7690
.option('-y, --yes', 'Non interactive message moving')
7791
.action(handleAction)
7892
.parse(process.argv);

src/main.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const { Spinner } = require('clui');
22

33
const handle = async ({
4+
maxMessages,
45
sourceQueueUrl,
56
targetQueueUrl,
67
sqs,
@@ -10,16 +11,22 @@ const handle = async ({
1011
const count = await sqs.getCount(sourceQueueUrl);
1112
await sqs.getCount(targetQueueUrl);
1213

13-
if (count === 0) {
14+
if (parseInt(count) === 0) {
1415
throw new Error(`The queue ${sourceQueueUrl} is empty!`);
1516
}
1617

18+
maxMessages = parseInt(maxMessages);
19+
moveCount = count
20+
if(count > maxMessages) {
21+
moveCount = maxMessages
22+
}
23+
1724
if (!skipPrompt) {
1825
const { move } = await prompt([
1926
{
2027
type: 'confirm',
2128
name: 'move',
22-
message: `Do you want to move ${count} messages?`,
29+
message: `Do you want to move ${moveCount} of ${count} messages?`,
2330
default: false,
2431
},
2532
]);
@@ -29,12 +36,12 @@ const handle = async ({
2936
}
3037
}
3138

32-
const spinner = new Spinner(`Moving ${count} messages...`);
39+
const spinner = new Spinner(`Moving ${moveCount} messages...`);
3340
spinner.start();
3441

3542
const promises = [];
3643

37-
for (let i = 0; i < count; i += 1) {
44+
for (let i = 0; i < moveCount; i += 1) {
3845
promises.push(sqs.moveMessage(sourceQueueUrl, targetQueueUrl));
3946
}
4047

@@ -45,7 +52,7 @@ const handle = async ({
4552
throw new Error(e.message);
4653
});
4754

48-
return count;
55+
return moveCount;
4956
};
5057

5158
module.exports = {

0 commit comments

Comments
 (0)