forked from signalfx/tracing-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.js
executable file
·81 lines (67 loc) · 2.35 KB
/
client.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/usr/bin/env node
const tracer = require('./contactKeeper/tracer');
const yargs = require('yargs');
const client = require('./contactKeeper/client');
function printRes(response) {
console.log('++++++++++++++++++++++++++++++++++');
console.log('ContactKeeper: \n');
if (response.message) {
console.log(response.message);
} else if (response.err) {
console.error('ERROR:', response.err);
}
console.log('\n++++++++++++++++++++++++++++++++++\n');
}
function printList(response) {
if (response.message) {
return printRes(response);
}
console.log('+++++++++++++++++++++++++++++++++++');
console.log(' Contact(s)');
console.log('----++---------++++---------++-----\n');
const properties = ['id', 'firstname', 'lastname', 'email'];
for (const [key, value] of Object.entries(response)) {
properties.forEach((prop) => {
console.log(`${prop}: ${value[prop]}`);
});
console.log('\n-----++---------++++---------++----');
}
console.log('\n+++++++++++++++++++++++++++++++++++\n');
}
yargs
.usage('Usage: $0 <command> [options]')
.scriptName('contactKeeper')
.command('add <firstName> <lastName> <email>',
'Add a new contact to your address book.',
{}, (argv) => {
client.addContact(argv.firstName, argv.lastName, argv.email)
.then(printRes);
})
.command('delete <fName> <lName>', 'Delete a contact.', {}, (argv) => {
client.deleteContact(argv.fName, argv.lName)
.then(printRes);
})
.command('deleteByID <id>', 'Delete a contact by ID.', {}, (argv) => {
client.deleteByID(argv.id)
.then(printRes);
})
.command('get <fName> [lName]', 'Get contact from your ContactKeeper.',
{}, (argv) => {
client.getContact(argv.fName, argv.lName)
.then(printList);
})
.command('list', 'Show list.', {}, (argv) => {
client.listContacts()
.then(printList);
})
.command('update <fName> <lName> <email>', 'Update a contact\'s email.',
{}, (argv) => {
client.updateEmail(argv.fName, argv.lName, argv.email)
.then(printRes);
})
.command('updateByID <id> <email>', 'Update a contact\'s email by ID.',
{}, (argv) => {
client.updateByID(argv.id, argv.email)
.then(printRes);
})
.help().argv;