tender-cli
Version:
Command line interface for Tender
280 lines (235 loc) • 6.8 kB
JavaScript
// Generated by CoffeeScript 1.6.2
var Table, TenderCLI, csv, fs, moment, path, reporters, tender, tenderCLI;
fs = require('fs');
path = require('path');
moment = require('moment');
tender = require('tender');
Table = require('cli-table');
csv = require('csv');
reporters = require('./reporters');
TenderCLI = (function() {
function TenderCLI(program, options) {
this.program = program;
this.options = options;
this.initClient();
}
TenderCLI.prototype.initClient = function() {
var config, configPath;
config = {};
configPath = path.join(process.env.HOME || process.env.HOMEPATH, '.tenderrc');
if (fs.existsSync(configPath)) {
config = JSON.parse(fs.readFileSync(configPath));
}
this.clientOpts = {
username: this.program.username || config.username,
password: this.program.pass || config.password,
subdomain: this.program.subdomain || config.subdomain,
token: this.program.api || config.token
};
return this.client = tender.createClient(this.clientOpts);
};
TenderCLI.prototype.list = function() {
var options,
_this = this;
if (this.options.reporters) {
return this.listReporters("list");
}
options = {};
if (this.options.state) {
options.state = this.options.state;
}
if (this.options.category) {
options.category = this.options.category;
}
if (this.options.title) {
options.pattern = this.options.title;
}
if (this.options.queue) {
options.queue = this.options.queue;
}
if (this.options.max) {
options.max = this.options.max;
}
return this.client.getDiscussions(options, function(err, results) {
if (err) {
return console.log(err);
}
return _this.report(results, "list");
});
};
TenderCLI.prototype.show = function(id) {
var _this = this;
if (this.options.reporters) {
return this.listReporters("show");
}
return this.client.showDiscussion({
id: id
}, function(err, result) {
if (err) {
return console.log(err);
}
return _this.report(result, "show");
});
};
TenderCLI.prototype.create = function() {
var options,
_this = this;
options = {
authorEmail: this.options.email,
authorName: this.options.name,
"public": this.options["private"] ? false : true,
category: this.options.category,
title: this.options.title,
body: this.options.body
};
return this.client.createDiscussion(options, function(err, result) {
if (err) {
return console.log(err);
}
return console.log("Discussion #" + result.id + " created in " + _this.options.category + ".");
});
};
TenderCLI.prototype.reply = function(id) {
var options;
options = {
id: id,
authorEmail: this.options.email,
authorName: this.options.name,
internal: this.options.internal,
body: this.options.body
};
return this.client.replyDiscussion(options, function(err, result) {
if (err) {
return console.log(err);
}
return console.log("Discussion #" + id + " replied to.");
});
};
TenderCLI.prototype.resolve = function(id) {
var _this = this;
return this.client.resolveDiscussion({
id: id
}, function(err, result) {
if (err) {
return console.log(err);
}
return console.log("Discussion #" + id + " resolved.");
});
};
TenderCLI.prototype.reopen = function(id) {
var _this = this;
return this.client.reopenDiscussion({
id: id
}, function(err, result) {
if (err) {
return console.log(err);
}
return console.log("Discussion #" + id + " reopened.");
});
};
TenderCLI.prototype.ack = function(id) {
var _this = this;
return this.client.acknowledgeDiscussion({
id: id
}, function(err, result) {
if (err) {
return console.log(err);
}
return console.log("Discussion #" + id + " acknowledged.");
});
};
TenderCLI.prototype.queue = function(id) {
var _this = this;
return this.client.queueDiscussion({
id: id,
queue: this.options.queue
}, function(err, result) {
if (err) {
return console.log(err);
}
return console.log("Discussion #" + id + " added to the " + _this.options.queue + " queue.");
});
};
TenderCLI.prototype.unqueue = function(id) {
var _this = this;
return this.client.unqueueDiscussion({
id: id,
queue: this.options.queue
}, function(err, result) {
if (err) {
return console.log(err);
}
return console.log("Discussion #" + id + " removed from the " + _this.options.queue + " queue.");
});
};
TenderCLI.prototype.categorize = function(id) {
var _this = this;
return this.client.categorizeDiscussion({
id: id,
category: this.options.category
}, function(err, result) {
if (err) {
return console.log(err);
}
return console.log("Discussion #" + id + " moved to the " + _this.options.category + " category.");
});
};
TenderCLI.prototype.remove = function(id) {
var _this = this;
return this.client.deleteDiscussion({
id: id
}, function(err, result) {
if (err) {
return console.log(err);
}
return console.log("Discussion #" + id + " deleted.");
});
};
TenderCLI.prototype.restore = function(id) {
var _this = this;
return this.client.restoreDiscussion({
id: id
}, function(err, result) {
if (err) {
return console.log(err);
}
return console.log("Discussion #" + id + " restored.");
});
};
TenderCLI.prototype.toggle = function(id) {
var _this = this;
return this.client.toggleDiscussion({
id: id
}, function(err, result) {
if (err) {
return console.log(err);
}
return console.log("Discussion #" + id + " toggled.");
});
};
TenderCLI.prototype.report = function(data, type) {
var cmdReporters, reporter;
cmdReporters = reporters[type];
reporter = cmdReporters[this.options.reporter] || cmdReporters["default"];
return reporter({
data: data,
cmdOptions: this.options
});
};
TenderCLI.prototype.listReporters = function(type) {
var cmdReporters, key;
cmdReporters = reporters[type];
console.log("");
for (key in cmdReporters) {
if (key !== 'default') {
console.log(" " + key + " - " + cmdReporters[key].description);
}
}
return console.log("");
};
return TenderCLI;
})();
tenderCLI = function(program, options) {
return new TenderCLI(program, options);
};
module.exports = tenderCLI;