-
Notifications
You must be signed in to change notification settings - Fork 5
/
cli.js
executable file
·551 lines (501 loc) · 14.3 KB
/
cli.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
#!/usr/bin/env node
require('isomorphic-fetch')
const fs = require('fs')
const appPackage = require('./package.json')
const path = require('path')
const { keyBy, sortBy } = require('lodash')
const spawnSync = require('child_process').spawnSync
const { ACH, importData, assert, log, askConfirmation } = require('./libs')
const runBatch = require('./libs/runBatch')
const scriptLib = require('./libs/scripts')
const parseDataFile = require('./libs/parseDataFile')
const getHandlebarsOptions = require('./libs/getHandlebarsOptions')
const { parseBool } = require('./libs/utils')
const urls = require('url')
const DEFAULT_COZY_URL = 'http://cozy.localhost:8080'
// Add promise rejection handling
process.on('unhandledRejection', function(err) {
log.error('Unhandled promise rejection.\n' + err.stack)
})
function fileExists(p) {
if (p && !fs.existsSync(path.resolve(p))) {
return false
} else {
return true
}
}
const handleErrors = fn =>
async function() {
try {
await fn.apply(this, arguments)
} catch (e) {
console.error(e)
process.exit(1)
}
}
const handleImportCommand = args => {
const { handlebarsOptionsFile, url } = args
let { filepath, token } = args
try {
assert(fileExists(filepath), `${filepath} does not exist`)
} catch (error) {
const achFilepath = path.join(__dirname, './data', filepath)
if (fileExists(achFilepath)) {
filepath = achFilepath
} else {
throw error
}
}
assert(
fileExists(handlebarsOptionsFile),
`${handlebarsOptionsFile} does not exist`
)
const options = { parallel: parseBool(process.env.ACH_PARALLEL, true) }
const handlebarsOptions = getHandlebarsOptions(handlebarsOptionsFile, options)
const templateDir = path.dirname(path.resolve(filepath))
const data = parseDataFile(filepath, handlebarsOptions)
const doctypes = Object.keys(data)
token = token || autotoken(url, doctypes)
return importData(url, token, data, templateDir, options)
}
const handleImportDirCommand = async args => {
const { url } = args
let { directoryPath, token } = args
if (!directoryPath) directoryPath = './DirectoriesToInject'
// get directories tree in JSON format
const dirTree = require('directory-tree')
const JSONtree = dirTree(directoryPath, {})
const doctypes = ['io.cozy.files']
token = token || autotoken(url, doctypes)
const ach = new ACH(token, url, doctypes)
await ach.connect()
await ach.importFolder(JSONtree)
}
const handleGenerateFilesCommand = async args => {
const {
dirId = 'io.cozy.files.root-dir',
filesCount = 10,
url,
token,
qualify,
mime = 'text/plain'
} = args
if (qualify && !token) {
log.warn(
'To view files in MyPapers, an app/konnector token is required to create qualified files.\nThis token allows you to have the `cozyMetadata.createdByApp` prop on files.'
)
}
const ach = new ACH(token || autotoken(url, ['io.cozy.files']), url, [
'io.cozy.files'
])
await ach.connect()
await ach.createFiles({
filesCount: parseInt(filesCount),
dirId,
qualify,
mime
})
}
const handleDropCommand = async args => {
const { doctypes, yes, url } = args
let { token } = args
token = token || autotoken(url, doctypes)
const question = `This doctypes will be removed.
${doctypes.map(x => `* ${x}`).join(' \n')}
Type "yes" if ok.
`
const confirmation = yes ? Promise.resolve() : askConfirmation(question)
try {
await confirmation
} catch (e) {
console.log('Cancelled drop')
return
}
const ach = new ACH(token, url, doctypes)
await ach.connect()
await ach.dropCollections(doctypes)
}
const handleExportCommand = async args => {
let { doctypes, filename, url, token, last } = args
doctypes = doctypes.split(',') // should be done with type
token = token || autotoken(url, doctypes)
const ach = new ACH(token, url, doctypes)
await ach.connect()
await ach.export(doctypes, filename, last)
}
const handleExportSingleCommand = async args => {
let { doctype, id, filename, url, token } = args
token = token || autotoken(url, [doctype])
const ach = new ACH(token, url, [doctype])
await ach.connect()
await ach.exportSingle(doctype, id, filename)
}
const handleUpdateSettingsCommand = async args => {
const { url, token } = args
let { settings } = args
settings = JSON.parse(settings) // should be done with type
const ach = new ACH(token || autotoken(url, ['io.cozy.settings']), url, [
'io.cozy.settings'
])
await ach.connect()
await ach.updateSettings(settings)
}
const handleGenerateTokenCommand = async args => {
const { url, token, doctypes } = args
const ach = new ACH(token, url, doctypes)
await ach.connect()
console.log(ach.client.stackClient.token.token)
}
const handleBatchCommand = async function(args) {
const { scriptName, domainsFile, execute, fromDomain } = args
let { poolSize, limit } = args
const script = scriptLib.require(scriptName)
try {
limit = !isNaN(limit) ? limit : undefined
poolSize = !isNaN(poolSize) ? poolSize : 30
const dryRun = !execute
await runBatch({
script,
domainsFile,
limit,
poolSize,
dryRun,
fromDomain
})
} catch (e) {
console.error('Error during batch execution')
throw e
}
}
const handleScriptCommand = async args => {
const { scriptName, autotoken, execute, url, parameters } = args
let { token } = args
const script = scriptLib.require(scriptName)
const { getDoctypes, run } = script
const doctypes = getDoctypes()
if (autotoken) {
token = autotoken(url, doctypes)
}
const ach = new ACH(token, url, doctypes)
const dryRun = !execute
log.info(`Launching script ${scriptName}...`)
log.info(`Dry run : ${dryRun}`)
await ach.connect()
await run(ach, dryRun, parameters)
}
const handleDownloadFileCommand = async args => {
const { url, fileid } = args
let { token } = args
const doctypes = ['io.cozy.files']
token = token || autotoken(url, doctypes) // should be done with types
const ach = new ACH(token, url, doctypes)
await ach.connect()
await ach.downloadFile(fileid)
}
const handleListScriptsCommand = function() {
const scripts = scriptLib.list()
console.log(scripts.join('\n'))
}
const isCommandAvailable = command => {
try {
const spawned = spawnSync('which', [command], {
stdio: 'pipe'
})
return spawned.stdout.length > 0
} catch (err) {
return false
}
}
const handleDeleteDocumentsCommand = async args => {
const { url, token, doctype, ids } = args
const ach = new ACH(token || autotoken(url, [doctype]), url, [doctype])
await ach.connect()
await ach.deleteDocuments(doctype, ids)
}
const makeToken = (url, doctypes) => {
const args = [url.replace(/https?:\/\//, ''), ...doctypes]
const spawned = spawnSync('make-token', args, {
stdio: 'pipe',
encoding: 'utf-8'
})
let token = null
if (spawned.stdout) {
token = spawned.stdout.split('\n')[0]
}
if (token) {
log.info('Made token automatically: ' + token)
} else {
log.info('Could not automatically create token')
log.debug(spawned.stderr)
}
return token
}
const autotoken = (url, doctypes) => {
if (
isCommandAvailable('make-token') &&
url.indexOf('cozy.localhost') === -1
) {
return makeToken(url, doctypes)
}
}
/**
* Removes the "app" part in a cozy URL
*
* This is useful for ergonomics as users often copy/paste an
* URL contaning the app
*
* input: https://moncozy-drive.mycozy.cloud
* ouput: https://moncozy.mycozy.cloud
*/
const parseCozyURL = stringUrl => {
if (!stringUrl.startsWith('http')) {
throw new Error('Cozy url must start with `http`')
}
const parsedUrl = urls.parse(stringUrl)
const splittedHost = parsedUrl.host.split('.')
parsedUrl.host = `${splittedHost[0].split('-')[0]}.${splittedHost
.slice(1)
.join('.')}`
// Using url module has a side effect: it adds a / at the end
// We remove it because it breaks make-token
return urls.format(parsedUrl).replace(/\/$/, '')
}
exports.parseCozyURL = parseCozyURL
// the CLI interface
let program = require('commander')
program
.version(appPackage.version)
.option('-t --token [token]', 'Token file to use')
.option('-y --yes', 'Does not ask for confirmation on sensitive operations')
.option(
'-u --url [url]',
`URL of the cozy to use. Defaults to "${DEFAULT_COZY_URL}".'`,
parseCozyURL,
DEFAULT_COZY_URL
)
program
.command('import <filepath> [handlebarsOptionsFile]')
.description(
'The file containing the JSON data to import. Defaults to "example-data.json". If the file doesn\'t exist in the application, ACH will try to find it inside its data folder. Then the dummy helpers JS file (optional).'
)
.action(
handleErrors(async function(filepath, handlebarsOptionsFile) {
await handleImportCommand({
url: program.url,
token: program.token,
filepath,
handlebarsOptionsFile
})
})
)
program
.command('importDir <directoryPath>')
.description(
'The path to the directory content to import. Defaults to "./DirectoriesToInject".'
)
.action(
handleErrors(async function(directoryPath) {
await handleImportDirCommand({
url: program.url,
token: program.token,
directoryPath
})
})
)
program
.command('generateFiles [filesCount] [dirId]')
.option('-q, --qualify', 'Add qualification to the files')
.option('-m, --mime <s>', 'Create files with this mime type')
.description('Generates a given number of small files.')
.action(
handleErrors(async function(filesCount, dirId, options) {
await handleGenerateFilesCommand({
url: program.url,
token: program.token,
filesCount,
dirId,
...options
})
})
)
program
.command('drop <doctypes...>')
.option('-y, --yes', 'Do not ask for confirmation')
.description('Deletes all documents of the provided doctypes. For real.')
.action(
handleErrors(async function(doctypes) {
await handleDropCommand({
url: program.url,
token: program.token,
doctypes,
yes: program.yes
})
})
)
program
.command('export <doctypes> [filename]')
.option(
'-l, --last <n>',
'Retrieve only the last documents, sorted by updated date',
x => parseInt(x, 10)
)
.description(
'Exports data from the doctypes (separated by commas) to filename'
)
.action(
handleErrors(async function(doctypes, filename, options) {
await handleExportCommand({
url: program.url,
token: program.token,
doctypes,
filename,
...options
})
})
)
program
.command('exportSingle <doctype> <id> [filename]')
.description('Exports single document to filename')
.action(
handleErrors(async function(doctype, id, filename, options) {
await handleExportSingleCommand({
url: program.url,
token: program.token,
doctype,
id,
filename,
...options
})
})
)
program
.command('downloadFile <fileid>')
.description('Download the file')
.action(
handleErrors(async function(fileid) {
await handleDownloadFileCommand({
url: program.url,
token: program.token,
fileid
})
})
)
program
.command('delete <doctype> <ids...>')
.description('Delete document(s)')
.action(
handleErrors(async function(doctype, ids) {
await handleDeleteDocumentsCommand({
url: program.url,
token: program.token,
doctype,
ids
})
})
)
program
.command('updateSettings')
.description('Update settings')
.action(
handleErrors(async function(settings) {
await handleUpdateSettingsCommand({
url: program.url,
token: program.token,
settings
})
})
)
program
.command('token <doctypes...>')
.description('Generate token')
.action(
handleErrors(async function(doctypes) {
await handleGenerateTokenCommand({
url: program.url,
doctypes
})
})
)
program
.command('script <scriptName>')
.arguments(
'[params...]',
'A list of parameters to be passed to the script, separated by a space'
)
.option('--autotoken', 'Automatically generate the permission token')
.option('-x, --execute', 'Execute the script (disable dry run)')
.option('-d, --doctypes', 'Print necessary doctypes (useful for automation)')
.description('Launch script')
.action(
handleErrors(async function(scriptName, parameters, action) {
await handleScriptCommand({
url: program.url,
token: program.token,
scriptName,
parameters,
...action
})
})
)
program
.command('ls-scripts')
.description('Lists all built-in scripts, useful for autocompletion')
.action(handleListScriptsCommand)
program
.command('batch <scriptName> <domainsFile>')
.option('-l, --limit <n>', 'Only take limit instances from the file', x =>
parseInt(x, 10)
)
.option(
'--fromDomain <domain>',
'Only consider <domainsFile> from <domain>. Useful for unfinished jobs.'
)
.option(
'-p, --pool-size <n>',
'Limit the number of instances on which we execute the script at the same time',
x => parseInt(x, 10)
)
.option('-x, --execute', 'Execute the script (disable dry run)')
.description('Launch script')
.action(
handleErrors(async function(scriptName, domainsFile, action) {
await handleBatchCommand({
url: program.url,
token: program.token,
scriptName,
domainsFile,
...action
})
})
)
const findCommand = program => {
const lastArg = program.args[program.args.length - 1]
if (lastArg instanceof program.Command) {
return lastArg._name
} else {
return program.args[0]
}
}
const main = async () => {
program.parse(process.argv)
// Check for unknown commands
const commands = keyBy(program.commands, '_name')
const command = findCommand(program)
if (!commands[command]) {
if (command) {
console.log(`Unknown command "${command}"`)
} else {
console.log('You must pass a command to ACH')
}
const availableCommands = sortBy(Object.keys(commands)).join(', ')
console.log(`Available commands: ${availableCommands}`)
console.log('Use `ACH --help` to have more help.')
process.exit(1)
}
}
if (require.main === module) {
main().catch(e => {
console.error(e)
process.exit(1)
})
}