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