1 |
|
2 | (function() {
|
3 | var ductile, join, log, ref, sep;
|
4 |
|
5 | ductile = require('./ductile');
|
6 |
|
7 | ref = require('path'), join = ref.join, sep = ref.sep;
|
8 |
|
9 | log = require('bog');
|
10 |
|
11 | module.exports = function(stdin, stdout, stderr) {
|
12 | return function(_argv) {
|
13 | var argv, errmsg, outerr, readfile, yargs;
|
14 | outerr = function(s1, s2, s3) {
|
15 | if (arguments.length === 3) {
|
16 | return stderr.write(s1 + " " + s2 + " " + s3 + "\n");
|
17 | } else if (arguments.length === 2) {
|
18 | return stderr.write(s1 + " " + s2 + "\n");
|
19 | } else if (arguments.length === 1) {
|
20 | return stderr.write(s1 + "\n");
|
21 | } else {
|
22 | return stderr.write("\n");
|
23 | }
|
24 | };
|
25 | errmsg = function(err) {
|
26 | var ref1, ref2, ref3, ref4;
|
27 | return (ref1 = (ref2 = (ref3 = err.body) != null ? (ref4 = ref3.error) != null ? ref4.reason : void 0 : void 0) != null ? ref2 : err.message) != null ? ref1 : err;
|
28 | };
|
29 | log.redirect(outerr, outerr);
|
30 | log.level('warn');
|
31 | readfile = function(f) {
|
32 | var ex, path;
|
33 | path = f[0] === sep ? f : join(process.cwd(), f);
|
34 | try {
|
35 | return require(path);
|
36 | } catch (error) {
|
37 | ex = error;
|
38 | if (ex.code === 'MODULE_NOT_FOUND') {
|
39 | outerr("File not found: " + path);
|
40 | if (process.env.__TESTING !== '1') {
|
41 | return process.exit(-1);
|
42 | }
|
43 | } else {
|
44 | throw ex;
|
45 | }
|
46 | }
|
47 | };
|
48 | yargs = require('yargs')(_argv).usage('\nUsage: ductile <command> [options] <url>').strict().wrap(null).command({
|
49 | command: 'export [options] <url>',
|
50 | alias: 'e',
|
51 | desc: 'Bulk export items',
|
52 | builder: function(yargs) {
|
53 | return yargs.strict().usage('\nUsage: ductile export [options] <url>').option('d', {
|
54 | alias: 'delete',
|
55 | "default": false,
|
56 | describe: 'output delete operations',
|
57 | type: 'boolean'
|
58 | }).option('q', {
|
59 | alias: 'query',
|
60 | describe: 'file with json query',
|
61 | type: 'string'
|
62 | }).option('t', {
|
63 | alias: 'transform',
|
64 | describe: 'file with transform function',
|
65 | type: 'string'
|
66 | }).demand(1);
|
67 | },
|
68 | handler: function(argv) {
|
69 | var body, lsearch, odelete, ref1, trans;
|
70 | odelete = argv["delete"];
|
71 | if (argv.q) {
|
72 | body = readfile(argv.q);
|
73 | }
|
74 | trans = (ref1 = (argv.t ? readfile(argv.t) : void 0)) != null ? ref1 : function(v) {
|
75 | return v;
|
76 | };
|
77 | lsearch = {
|
78 | body: body
|
79 | };
|
80 | return ductile(argv.url).reader(lsearch, odelete, trans).on('progress', function(p) {
|
81 | return outerr("Exported " + p.from + "/" + p.total);
|
82 | }).on('error', function(err) {
|
83 | return outerr('EXPORT ERROR:', errmsg(err));
|
84 | }).pipe(stdout).on('error', function(err) {
|
85 | if (err.code === 'EPIPE') {
|
86 | if (process.env.__TESTING !== '1') {
|
87 | return process.exit(-1);
|
88 | }
|
89 | } else {
|
90 | return outerr('EXPORT ERROR:', err);
|
91 | }
|
92 | });
|
93 | }
|
94 | }).command({
|
95 | command: 'import [options] <url>',
|
96 | alias: 'i',
|
97 | desc: 'Bulk import items',
|
98 | builder: function(yargs) {
|
99 | return yargs.strict().usage('\nUsage: ductile import [options] <url>').option('d', {
|
100 | alias: 'delete',
|
101 | "default": false,
|
102 | describe: 'change incoming index operations to delete',
|
103 | type: 'boolean'
|
104 | }).option('t', {
|
105 | alias: 'transform',
|
106 | describe: 'file with transform function',
|
107 | type: 'string'
|
108 | }).demand(1);
|
109 | },
|
110 | handler: function(argv) {
|
111 | var odelete, trans;
|
112 | odelete = argv["delete"];
|
113 | trans = argv.t ? readfile(argv.t) : function(v) {
|
114 | return v;
|
115 | };
|
116 | return ductile(argv.url).writer(odelete, trans, stdin).on('progress', function(p) {
|
117 | return outerr("Imported " + p.count);
|
118 | }).on('info', outerr).on('error', function(err) {
|
119 | outerr('IMPORT ERROR:', errmsg(err));
|
120 | if (process.env.__TESTING !== '1') {
|
121 | return process.exit(-1);
|
122 | }
|
123 | });
|
124 | }
|
125 | }).command({
|
126 | command: 'alias <url>',
|
127 | alias: 'a',
|
128 | desc: 'Bulk export aliases',
|
129 | builder: function(yargs) {
|
130 | return yargs.strict().usage('\nUsage: ductile alias <url>').demand(1);
|
131 | },
|
132 | handler: function(argv) {
|
133 | return ductile(argv.url).alias().on('error', function(err) {
|
134 | return outerr('EXPORT ERROR:', errmsg(err));
|
135 | }).pipe(stdout).on('error', function(err) {
|
136 | if (err.code === 'EPIPE') {
|
137 | if (process.env.__TESTING !== '1') {
|
138 | return process.exit(-1);
|
139 | }
|
140 | } else {
|
141 | return outerr('EXPORT ERROR:', err);
|
142 | }
|
143 | });
|
144 | }
|
145 | }).command({
|
146 | command: 'mappings <url>',
|
147 | alias: 'm',
|
148 | desc: 'Bulk export mappings',
|
149 | builder: function(yargs) {
|
150 | return yargs.strict().usage('\nUsage: ductile mappings <url>').demand(1);
|
151 | },
|
152 | handler: function(argv) {
|
153 | return ductile(argv.url).mappings().on('error', function(err) {
|
154 | return outerr('EXPORT ERROR:', errmsg(err));
|
155 | }).pipe(stdout).on('error', function(err) {
|
156 | if (err.code === 'EPIPE') {
|
157 | if (process.env.__TESTING !== '1') {
|
158 | return process.exit(-1);
|
159 | }
|
160 | } else {
|
161 | return outerr('EXPORT ERROR:', err);
|
162 | }
|
163 | });
|
164 | }
|
165 | }).command({
|
166 | command: 'settings <url>',
|
167 | alias: 'm',
|
168 | desc: 'Bulk export settings',
|
169 | builder: function(yargs) {
|
170 | return yargs.strict().usage('\nUsage: ductile settings <url>').demand(1);
|
171 | },
|
172 | handler: function(argv) {
|
173 | return ductile(argv.url).settings().on('error', function(err) {
|
174 | return outerr('EXPORT ERROR:', errmsg(err));
|
175 | }).pipe(stdout).on('error', function(err) {
|
176 | if (err.code === 'EPIPE') {
|
177 | if (process.env.__TESTING !== '1') {
|
178 | return process.exit(-1);
|
179 | }
|
180 | } else {
|
181 | return outerr('EXPORT ERROR:', err);
|
182 | }
|
183 | });
|
184 | }
|
185 | }).command({
|
186 | command: 'template <url>',
|
187 | alias: 't',
|
188 | desc: 'Bulk export template',
|
189 | builder: function(yargs) {
|
190 | return yargs.strict().usage('\nUsage: ductile template <url>').demand(1);
|
191 | },
|
192 | handler: function(argv) {
|
193 | return ductile(argv.url).template().on('error', function(err) {
|
194 | return outerr('EXPORT ERROR:', errmsg(err));
|
195 | }).pipe(stdout).on('error', function(err) {
|
196 | if (err.code === 'EPIPE') {
|
197 | if (process.env.__TESTING !== '1') {
|
198 | return process.exit(-1);
|
199 | }
|
200 | } else {
|
201 | return outerr('EXPORT ERROR:', err);
|
202 | }
|
203 | });
|
204 | }
|
205 | }).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();
|
206 | argv = yargs.argv;
|
207 | if (!argv._.length) {
|
208 | return yargs.showHelp();
|
209 | }
|
210 | };
|
211 | };
|
212 |
|
213 | }).call(this);
|
214 |
|
215 |
|