json_dataset_tool
Version:
CLI tool to send JSON to Geckoboard's Dataset Platform
62 lines (47 loc) • 1.13 kB
JavaScript
;
const pkg = require('./package');
const nopt = require('nopt');
const options = {
id: String,
api_key: String,
create: String,
update: String,
delete: Boolean,
};
const parsed = nopt(options, undefined, process.argv, 2);
const msg = (m, o) => console.log(m, o ? JSON.stringify(o) : '' );
module.exports = actionHandler(parsed)
.then(res => msg('Success!', res))
.catch(e => {
if (e) msg('Error!', e);
msg(`Please see the README.md for usage instructions, or file an issue at ${pkg.bugs.url}`);
});
function actionHandler(parsed) {
const gb = require('tuko')(parsed.api_key);
const { id, create, update, } = parsed;
msg('Dataset ID:', id);
if (parsed.delete) {
return gb.del(id);
}
const body = (file) => {
let path = process.cwd() + '/' + file;
return require(path);
}
let payload;
if (create) {
payload = {
fields: body(create)
};
return gb.create(id, payload);
}
if (update) {
payload = {
data: body(update)
};
return gb.update(id, payload);
}
else {
return Promise.reject();
}
}