-
Notifications
You must be signed in to change notification settings - Fork 0
/
gv-contacts
executable file
·123 lines (96 loc) · 2.77 KB
/
gv-contacts
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!/usr/bin/env node
var program = require('commander'),
init = require('./lib/init'),
login = require('./lib/login'),
exec = require('child_process').exec;
program.version('0.0.1')
.option('-j, --json', 'Output in JSON format')
.option('-s, --sync', 'Sync with your ppl address book')
.parse(process.argv);
init(program, function(config) {
var func = program.sync ? syncAll : print;
var client = login(config);
client.contacts('get', wrapped(func));
});
function wrapped(callback) {
return function(err) {
return err
? console.error(err)
: callback.apply(null, Array.prototype.slice.call(arguments, 1));
};
}
function syncAll(resp, data) {
if(!data || !data.contacts || !Object.keys(data.contacts).length) {
process.exit();
}
var key = Object.keys(data.contacts)[0]
var contact = data.contacts[key];
sync(contact, function() {
console.log('Synced: ' + contact.name);
delete data.contacts[key];
syncAll(resp, data);
});
}
function sync(contact, callback) {
var key = contact.name.replace(' ', '').toLowerCase();
ppl('name', key, function(stdout) {
var commands = {
'name': [ quoted(contact.name) ],
'phone': [ contact.phoneNumber.replace('+', '') ]
};
if(contact.emails && contact.emails.length) {
commands.email = [ quoted(contact.emails[0]) ];
}
(function roll() {
if(Object.keys(commands).length == 0) {
callback(); return;
}
var cmd = Object.keys(commands)[0];
var arr = commands[cmd];
arr.unshift(key);
arr.unshift(cmd);
arr.push(function() {
delete commands[cmd];
roll();
}, process.exit.bind(process));
ppl.apply(null, arr);
})();
}, function() {
ppl('add', key, quoted(contact.name), sync.bind(null, contact, callback), process.exit.bind(process));
});
}
function quoted(str) {
return '\"' + str + '\"';
}
function ppl(command) {
var args = Array.prototype.slice.call(arguments, 1),
callback = args[args.length - 2],
errback = args[args.length - 1],
pplArgs = args.slice(0, -2),
cmd = 'ppl ' + command + ' ' + pplArgs.join(' ');
exec(cmd, function(err, stdout, stderr) {
return err ? errback() : callback(stdout);
});
}
function print(resp, data) {
if(!data || !data.contacts) {
return;
}
if(program.json) {
console.log(JSON.stringify(data.contacts));
return;
}
var contacts = Object.keys(data.contacts).map(function(key) {
return data.contacts[key];
})
.sort(function(a, b) {
var c = a.name[0] || 'z',
d = b.name[0] || 'z';
return c > d;
});
contacts.forEach(function(c) {
var name = c.name,
phone = c.phoneNumber.replace('+', '');
console.log(name + '\t' + phone);
});
}