Skip to content

Commit

Permalink
OR-2497 Refactor Contact CLI to use yargs
Browse files Browse the repository at this point in the history
  • Loading branch information
kscc25 committed Jan 20, 2017
1 parent 8139996 commit 8b75611
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 61 deletions.
38 changes: 21 additions & 17 deletions modules/linagora.esn.contact/bin/cli.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,30 @@
'use strict';

var commander = require('commander');
var fs = require('fs-extra');
var q = require('q');
var path = require('path');
const fs = require('fs-extra');
const q = require('q');
const path = require('path');
const yargs = require('yargs');

var commandsPath = path.resolve(__dirname + '/commands');
const commandsPath = path.resolve(path.join(__dirname, 'commands'));
const readdir = q.denodeify(fs.readdir);

var readdir = q.denodeify(fs.readdir);
readdir(commandsPath).then(function(files) {
files.forEach(function(filename) {
var file = commandsPath + '/' + filename;
if (fs.statSync(file).isFile()) {
var commandName = filename.slice(filename.lastIndexOf('/') + 1, filename.lastIndexOf('.'));
var c = require('./commands/' + commandName);
if (c.getCommandParameters && typeof c.getCommandParameters === 'function') {
commandName = commandName + ' ' + c.getCommandParameters();
}
var command = commander.command(commandName);
c.createCommand(command);
const filePath = path.join(commandsPath, filename);

if (fs.statSync(filePath).isFile()) {
const command = require('./commands/' + filename).command;

yargs.command(command);
}
});
commander.parse(process.argv);
});

yargs
.usage('Usage: $0 <command> [options]')
.demand(1, 'You need to specify a command')
.help()
.version()
.epilogue('for more information, go to https://open-paas.org')
.example('$0 populate --help', 'show help of populate command')
.argv;
});
112 changes: 68 additions & 44 deletions modules/linagora.esn.contact/bin/commands/populate.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,48 @@
'use strict';

var q = require('q');
var uuid = require('node-uuid');
var request = require('request');
var ICAL = require('ical.js');
var fs = require('fs');
var path = require('path');
var commons = require('../../../../bin/commons');

var DEFAULT_BASE_URL = 'http://localhost:8080';
var DEFAULT_LOGIN = 'admin@open-paas.org';
var DEFAULT_PASSWORD = 'secret';
const q = require('q');
const uuid = require('node-uuid');
const request = require('request');
const ICAL = require('ical.js');
const fs = require('fs');
const path = require('path');
const commons = require('../../../../bin/commons');

const DEFAULT_BASE_URL = 'http://localhost:8080';
const DEFAULT_LOGIN = 'admin@open-paas.org';
const DEFAULT_PASSWORD = 'secret';

const command = {
command: 'populate <type> <size>',
desc: 'Configure OpenPaaS',
builder: {
login: {
alias: 'l',
describe: 'User login',
default: DEFAULT_LOGIN
},
password: {
alias: 'p',
describe: 'User password',
default: DEFAULT_PASSWORD
},
url: {
alias: 'u',
describe: 'ESN base URL',
default: DEFAULT_BASE_URL
}
},
handler: argv => {
const { url, login, password, size, type } = argv;

commons.logInfo(`Calling with user ${login}/${password} on ${url}`);

exec(url, login, password, size, type)
.then(() => commons.logInfo('Configured'))
.catch(commons.logError)
.finally(commons.exit);
}
};

function exec(base_url, login, password, size, type) {

Expand All @@ -22,10 +54,12 @@ function exec(base_url, login, password, size, type) {
var fileName = size < 1000 ? '100' : '1000';

var filePath = path.join(__dirname, './data/populate/' + fileName + '.json');

fs.readFile(filePath, 'utf-8', function(err, data) {
if (err) {
return defer.reject(err);
}

return defer.resolve(JSON.parse(data).results);
});

Expand All @@ -34,6 +68,7 @@ function exec(base_url, login, password, size, type) {

function getRandomContactsFromWeb() {
var defer = q.defer();

request({
method: 'GET',
json: true,
Expand All @@ -42,8 +77,10 @@ function exec(base_url, login, password, size, type) {
if (err) {
return defer.reject(err);
}

return defer.resolve(body.results || []);
});

return defer.promise;
}

Expand All @@ -54,6 +91,7 @@ function exec(base_url, login, password, size, type) {

function getRandomContacts() {
var generator = contactGenerators[type] || contactGenerators.file;

return generator();
}

Expand All @@ -74,6 +112,7 @@ function exec(base_url, login, password, size, type) {
if (shell.emails) {
shell.emails.forEach(function(data) {
var prop = vcard.addPropertyWithValue('email', 'mailto:' + data.value);

prop.setParameter('type', data.type);
});
}
Expand All @@ -87,12 +126,13 @@ function exec(base_url, login, password, size, type) {

function sendContact(user, shell) {
bookId = bookId || user._id;
var path = '/dav/api/addressbooks/' + bookId + '/contacts/' + shell.id + '.vcf';

var path = '/dav/api/addressbooks/' + bookId + '/contacts/' + shell.id + '.vcf';
var json = shellToVCARD(shell).toJSON();

var defer = q.defer();
console.log('Creating contact on path %s...', path);

commons.logInfo(`Creating contact on path ${path}...`);

request({
uri: base_url + path,
jar: true,
Expand All @@ -106,9 +146,12 @@ function exec(base_url, login, password, size, type) {
if (err) {
return defer.reject(err);
}
console.log('%s created', shell.id);

commons.logInfo(`${shell.id} created`);

return defer.resolve(body);
});

return defer.promise;
}

Expand All @@ -132,6 +175,7 @@ function exec(base_url, login, password, size, type) {
phone: [{type: 'mobile', value: u.cell}, {type: 'work', value: u.phone}],
photo: u.picture.medium
};

return sendContact(user, shell);
}

Expand All @@ -144,47 +188,27 @@ function exec(base_url, login, password, size, type) {
}

commons.loginAsUser(base_url, login, password, function(err, user) {

if (err) {
console.log('Login error');
commons.logError('Login error');

return defer.reject(err);
}

console.log('Logged in as', user);
commons.logInfo('Logged in as', user);

return createContacts(user).then(function(results) {
console.log('Contacts have been created', results.length);
commons.logInfo('Contacts have been created', results.length);
defer.resolve();
}, function(err) {
console.log(err);
commons.logError(err);
defer.reject(err);
});
});
return defer.promise;
}
module.exports.exec = exec;

function getCommandParameters() {
return '<type> <size>';
return defer.promise;
}
module.exports.getCommandParameters = getCommandParameters;

module.exports.createCommand = function(command) {

module.exports = {
exec,
command
.description('Populate <size> random contacts from <type> generator')
.option('-l, --login [login]', 'User login', DEFAULT_LOGIN)
.option('-p, --password [password]', 'User password', DEFAULT_PASSWORD)
.option('-u, --url [url]', 'ESN base URL like http://localhost:8080', DEFAULT_BASE_URL)

.action(function(type, size) {
var url = command.url;
var login = command.login;
var password = command.password;
console.log('Calling with user %s/%s on %s', login, password, url);
exec(url, login, password, size, type).then(function() {
console.log('Populated');
}, function(err) {
console.log('Error', err);
}).finally(commons.exit);
});
};

0 comments on commit 8b75611

Please sign in to comment.