Skip to content

Commit 909f817

Browse files
Merge pull request joblocal#5 from joblocal/cli-arguments
Cli arguments
2 parents 3d40362 + d198466 commit 909f817

File tree

7 files changed

+623
-692
lines changed

7 files changed

+623
-692
lines changed

.editorconfig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
root = true
2+
3+
[*]
4+
charset = utf-8
5+
indent_style = space
6+
indent_size = 2
7+
end_of_line = lf
8+
insert_final_newline = true
9+
trim_trailing_whitespace = true

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@ After installing the package you can use it as follows.
3333
$ aws-move-queue-messages
3434
```
3535

36+
With all optional CLI arguments:
37+
38+
```sh
39+
$ aws-move-queue-messages <from-queue-url> <to-queue-url> -r [AWS-REGION] -y
40+
```
41+
42+
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".
43+
3644
### Parameter
3745
Use the **Source Url** parameter to specify the source queue from which the messages are to be read.
3846
Use the **Target Url** parameter to specify the target queue in which the messages are written.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
},
2222
"dependencies": {
2323
"aws-sdk": "^2.328.0",
24-
"clear": "^0.1.0",
2524
"clui": "^0.3.6",
25+
"commander": "^2.20.0",
2626
"inquirer": "^6.2.0",
2727
"minimist": "^1.2.0"
2828
},

src/index.js

Lines changed: 68 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,78 @@
11
#!/usr/bin/env node
22

33
const aws = require('aws-sdk');
4-
const clear = require('clear');
4+
const program = require('commander');
55
const { prompt } = require('inquirer');
66
const { validateUrl } = require('./helper');
77
const { handle } = require('./main');
88
const { createClient } = require('./sqs');
99

10-
clear();
11-
12-
prompt([
13-
{
14-
type: 'input',
15-
name: 'awsRegion',
16-
message: 'Enter your AWS region:',
17-
default: 'eu-west-1',
18-
},
19-
{
20-
type: 'input',
21-
name: 'sourceQueueUrl',
22-
message: 'Enter your source queue url:',
23-
validate: validateUrl,
24-
},
25-
{
26-
type: 'input',
27-
name: 'targetQueueUrl',
28-
message: 'Enter your target queue url:',
29-
validate: validateUrl,
30-
},
31-
]).then(async ({ awsRegion, sourceQueueUrl, targetQueueUrl }) => {
32-
aws.config.update({ region: awsRegion });
33-
34-
const sqs = createClient(new aws.SQS());
35-
let count;
36-
37-
try {
38-
count = await handle({
39-
sourceQueueUrl,
40-
targetQueueUrl,
41-
sqs,
42-
prompt,
43-
});
44-
} catch (e) {
45-
console.error(`! ${e.message}`);
46-
process.exit(1);
10+
const regionQuestion = {
11+
type: 'input',
12+
name: 'awsRegion',
13+
message: 'Enter your AWS region:',
14+
default: 'eu-west-1',
15+
};
16+
17+
const fromQuestion = {
18+
type: 'input',
19+
name: 'sourceQueueUrl',
20+
message: 'Enter your source queue url:',
21+
validate: validateUrl,
22+
};
23+
24+
const toQuestion = {
25+
type: 'input',
26+
name: 'targetQueueUrl',
27+
message: 'Enter your target queue url:',
28+
validate: validateUrl,
29+
};
30+
31+
const handleAction = (from, to, options) => {
32+
const questions = [];
33+
34+
if (!options.region) {
35+
questions.push(regionQuestion);
36+
}
37+
38+
if (!from) {
39+
questions.push(fromQuestion);
4740
}
4841

49-
console.log(`${count} messages moved from ${sourceQueueUrl} to ${targetQueueUrl}!`);
50-
});
42+
if (!to) {
43+
questions.push(toQuestion);
44+
}
45+
46+
prompt(questions).then(async ({
47+
awsRegion = options.region,
48+
sourceQueueUrl = from,
49+
targetQueueUrl = to,
50+
}) => {
51+
aws.config.update({ region: awsRegion });
52+
53+
const sqs = createClient(new aws.SQS());
54+
let count;
55+
56+
try {
57+
count = await handle({
58+
sourceQueueUrl,
59+
targetQueueUrl,
60+
sqs,
61+
prompt,
62+
skipPrompt: options.yes,
63+
});
64+
} catch (e) {
65+
console.error(`! ${e.message}`);
66+
process.exit(1);
67+
}
68+
69+
console.log(`${count} messages moved from ${sourceQueueUrl} to ${targetQueueUrl}!`);
70+
});
71+
};
72+
73+
program
74+
.arguments('[from] [to]')
75+
.option('-r, --region [value]', 'The AWS region')
76+
.option('-y, --yes', 'Non interactive message moving')
77+
.action(handleAction)
78+
.parse(process.argv);

src/main.js

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const handle = async ({
55
targetQueueUrl,
66
sqs,
77
prompt,
8+
skipPrompt,
89
}) => {
910
const count = await sqs.getCount(sourceQueueUrl);
1011
await sqs.getCount(targetQueueUrl);
@@ -13,17 +14,19 @@ const handle = async ({
1314
throw new Error(`The queue ${sourceQueueUrl} is empty!`);
1415
}
1516

16-
const { move } = await prompt([
17-
{
18-
type: 'confirm',
19-
name: 'move',
20-
message: `Do you want to move ${count} messages?`,
21-
default: false,
22-
},
23-
]);
24-
25-
if (!move) {
26-
process.exit(0);
17+
if (!skipPrompt) {
18+
const { move } = await prompt([
19+
{
20+
type: 'confirm',
21+
name: 'move',
22+
message: `Do you want to move ${count} messages?`,
23+
default: false,
24+
},
25+
]);
26+
27+
if (!move) {
28+
process.exit(0);
29+
}
2730
}
2831

2932
const spinner = new Spinner(`Moving ${count} messages...`);

src/main.spec.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,25 @@ describe('handle', () => {
2323
expect(sqs.getCount).toHaveBeenCalled();
2424
});
2525

26+
test('to move messages without prompt', async () => {
27+
const sqs = {
28+
getCount: jest.fn(() => 3),
29+
moveMessage: jest.fn(),
30+
};
31+
32+
const prompt = jest.fn();
33+
34+
await handle({
35+
sourceQueueUrl: 'https://sqs.region.amazonaws.com/123456789/srcQueue',
36+
targetQueueUrl: 'https://sqs.region.amazonaws.com/123456789/targetQueue',
37+
sqs,
38+
prompt,
39+
skipPrompt: true,
40+
});
41+
42+
expect(sqs.getCount).toHaveBeenCalled();
43+
});
44+
2645
describe('reject promise', () => {
2746
test('on failed count', () => {
2847
const sqs = {

0 commit comments

Comments
 (0)