ductile
Version:
Elasticsearch Bulk Loader
222 lines (215 loc) • 7.93 kB
JavaScript
// Generated by CoffeeScript 1.11.1
(function() {
var ductile, join, log, ref, sep;
ductile = require('./ductile');
ref = require('path'), join = ref.join, sep = ref.sep;
log = require('bog');
module.exports = function(stdin, stdout, stderr) {
return function(_argv) {
var argv, errmsg, outerr, readfile, yargs;
outerr = function(s1, s2, s3) {
if (arguments.length === 3) {
return stderr.write(s1 + " " + s2 + " " + s3 + "\n");
} else if (arguments.length === 2) {
return stderr.write(s1 + " " + s2 + "\n");
} else if (arguments.length === 1) {
return stderr.write(s1 + "\n");
} else {
return stderr.write("\n");
}
};
errmsg = function(err) {
var ref1, ref2, ref3, ref4;
return (ref1 = (ref2 = (ref3 = err.body) != null ? (ref4 = ref3.error) != null ? ref4.reason : void 0 : void 0) != null ? ref2 : err.message) != null ? ref1 : err;
};
log.redirect(outerr, outerr);
log.level('warn');
readfile = function(f) {
var ex, path;
path = f[0] === sep ? f : join(process.cwd(), f);
try {
return require(path);
} catch (error) {
ex = error;
if (ex.code === 'MODULE_NOT_FOUND') {
outerr("File not found: " + path);
if (process.env.__TESTING !== '1') {
return process.exit(-1);
}
} else {
throw ex;
}
}
};
yargs = require('yargs')(_argv).usage('\nUsage: ductile <command> [options] <url>').strict().wrap(null).command({
command: 'import [options] <url>',
alias: 'i',
desc: 'Bulk import items (anything exported)',
builder: function(yargs) {
return yargs.strict().usage('\nUsage: ductile import [options] <url>').option('d', {
alias: 'delete',
"default": false,
describe: 'change incoming index operations to delete',
type: 'boolean'
}).option('t', {
alias: 'transform',
describe: 'file with transform function',
type: 'string'
}).option('w', {
alias: 'wait',
describe: 'wait until elasticsearch becomes available',
type: 'boolean'
}).demand(1);
},
handler: function(argv) {
var odelete, trans;
odelete = argv["delete"];
trans = argv.t ? readfile(argv.t) : function(v) {
return v;
};
return ductile(argv.url).wait(argv.w).then(function(duct) {
return duct.writer(odelete, trans, stdin).on('progress', function(p) {
return outerr("Imported " + p.count);
}).on('info', outerr).on('error', function(err) {
outerr('IMPORT ERROR:', errmsg(err));
if (process.env.__TESTING !== '1') {
return process.exit(-1);
}
});
});
}
}).command({
command: 'export [options] <url>',
alias: 'e',
desc: 'Bulk export documents',
builder: function(yargs) {
return yargs.strict().usage('\nUsage: ductile export [options] <url>').option('d', {
alias: 'delete',
"default": false,
describe: 'output delete operations',
type: 'boolean'
}).option('q', {
alias: 'query',
describe: 'file with json query',
type: 'string'
}).option('t', {
alias: 'transform',
describe: 'file with transform function',
type: 'string'
}).demand(1);
},
handler: function(argv) {
var body, lsearch, odelete, ref1, trans;
odelete = argv["delete"];
if (argv.q) {
body = readfile(argv.q);
}
trans = (ref1 = (argv.t ? readfile(argv.t) : void 0)) != null ? ref1 : function(v) {
return v;
};
lsearch = {
body: body
};
return ductile(argv.url).reader(lsearch, odelete, trans).on('progress', function(p) {
return outerr("Exported " + p.from + "/" + p.total);
}).on('error', function(err) {
return outerr('EXPORT ERROR:', errmsg(err));
}).pipe(stdout).on('error', function(err) {
if (err.code === 'EPIPE') {
if (process.env.__TESTING !== '1') {
return process.exit(-1);
}
} else {
return outerr('EXPORT ERROR:', err);
}
});
}
}).command({
command: 'alias <url>',
alias: 'a',
desc: 'Bulk export aliases',
builder: function(yargs) {
return yargs.strict().usage('\nUsage: ductile alias <url>').demand(1);
},
handler: function(argv) {
return ductile(argv.url).alias().on('error', function(err) {
return outerr('EXPORT ERROR:', errmsg(err));
}).pipe(stdout).on('error', function(err) {
if (err.code === 'EPIPE') {
if (process.env.__TESTING !== '1') {
return process.exit(-1);
}
} else {
return outerr('EXPORT ERROR:', err);
}
});
}
}).command({
command: 'mappings <url>',
alias: 'm',
desc: 'Bulk export mappings',
builder: function(yargs) {
return yargs.strict().usage('\nUsage: ductile mappings <url>').demand(1);
},
handler: function(argv) {
return ductile(argv.url).mappings().on('error', function(err) {
return outerr('EXPORT ERROR:', errmsg(err));
}).pipe(stdout).on('error', function(err) {
if (err.code === 'EPIPE') {
if (process.env.__TESTING !== '1') {
return process.exit(-1);
}
} else {
return outerr('EXPORT ERROR:', err);
}
});
}
}).command({
command: 'settings <url>',
alias: 'm',
desc: 'Bulk export settings',
builder: function(yargs) {
return yargs.strict().usage('\nUsage: ductile settings <url>').demand(1);
},
handler: function(argv) {
return ductile(argv.url).settings().on('error', function(err) {
return outerr('EXPORT ERROR:', errmsg(err));
}).pipe(stdout).on('error', function(err) {
if (err.code === 'EPIPE') {
if (process.env.__TESTING !== '1') {
return process.exit(-1);
}
} else {
return outerr('EXPORT ERROR:', err);
}
});
}
}).command({
command: 'template <url>',
alias: 't',
desc: 'Bulk export template',
builder: function(yargs) {
return yargs.strict().usage('\nUsage: ductile template <url>').demand(1);
},
handler: function(argv) {
return ductile(argv.url).template().on('error', function(err) {
return outerr('EXPORT ERROR:', errmsg(err));
}).pipe(stdout).on('error', function(err) {
if (err.code === 'EPIPE') {
if (process.env.__TESTING !== '1') {
return process.exit(-1);
}
} else {
return outerr('EXPORT ERROR:', err);
}
});
}
}).example('ductile export http://localhost:9200/myindex').example('ductile export http://localhost:9200/myindex/mytype > dump.bulk').example('ductile import http://localhost:9200/myindex/mytype < dump.bulk').help().showHelpOnFail();
argv = yargs.argv;
if (!argv._.length) {
return yargs.showHelp();
}
};
};
}).call(this);
//# sourceMappingURL=cmd.js.map