1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | 'use strict';
|
15 |
|
16 | const cliutils = require('./cliutils.js');
|
17 | const config = require('./config.js');
|
18 | const error = require('./error.js');
|
19 | const path = require('path');
|
20 | const pkg = require('../package.json');
|
21 |
|
22 | function parseBackupArgs() {
|
23 | const program = require('commander');
|
24 |
|
25 |
|
26 | const defaults = config.cliDefaults();
|
27 |
|
28 |
|
29 | const envVarOptions = {};
|
30 | config.applyEnvironmentVariables(envVarOptions);
|
31 |
|
32 | program
|
33 | .version(pkg.version)
|
34 | .description('Backup a CouchDB/Cloudant database to a backup text file.')
|
35 | .usage('[options...]')
|
36 | .option('-b, --buffer-size <n>',
|
37 | cliutils.getUsage('number of documents fetched at once', defaults.bufferSize),
|
38 | Number)
|
39 | .option('-d, --db <db>',
|
40 | cliutils.getUsage('name of the database to backup', defaults.db))
|
41 | .option('-k, --iam-api-key <API key>',
|
42 | cliutils.getUsage('IAM API key to access the Cloudant server'))
|
43 | .option('-l, --log <file>',
|
44 | cliutils.getUsage('file to store logging information during backup; invalid in "shallow" mode', 'a temporary file'),
|
45 | path.normalize)
|
46 | .option('-m, --mode <mode>',
|
47 | cliutils.getUsage('"shallow" if only a superficial backup is done (ignoring conflicts and revision tokens), else "full" for complete backup', defaults.mode),
|
48 | (mode) => { return mode.toLowerCase(); })
|
49 | .option('-o, --output <file>',
|
50 | cliutils.getUsage('file name to store the backup data', 'stdout'),
|
51 | path.normalize)
|
52 | .option('-p, --parallelism <n>',
|
53 | cliutils.getUsage('number of HTTP requests to perform in parallel when performing a backup; ignored in "shallow" mode', defaults.parallelism),
|
54 | Number)
|
55 | .option('-q, --quiet',
|
56 | cliutils.getUsage('suppress batch messages', defaults.quiet))
|
57 | .option('-r, --resume',
|
58 | cliutils.getUsage('continue a previous backup from its last known position; invalid in "shallow" mode', defaults.resume))
|
59 | .option('-t, --request-timeout <n>',
|
60 | cliutils.getUsage('milliseconds to wait for a response to a HTTP request before retrying the request', defaults.requestTimeout),
|
61 | Number)
|
62 | .option('-u, --url <url>',
|
63 | cliutils.getUsage('URL of the CouchDB/Cloudant server', defaults.url))
|
64 | .parse(process.argv);
|
65 |
|
66 |
|
67 | if (program.opts().mode === 'shallow' || envVarOptions.mode === 'shallow') {
|
68 | delete defaults.parallelism;
|
69 | delete defaults.log;
|
70 | delete defaults.resume;
|
71 | }
|
72 |
|
73 |
|
74 |
|
75 | const opts = Object.assign({}, defaults, envVarOptions, program.opts());
|
76 |
|
77 | if (opts.resume && (opts.log === defaults.log)) {
|
78 |
|
79 |
|
80 | error.terminationCallback(new error.BackupError('NoLogFileName', 'To resume a backup, a log file must be specified'));
|
81 | }
|
82 |
|
83 | return opts;
|
84 | }
|
85 |
|
86 | function parseRestoreArgs() {
|
87 | const program = require('commander');
|
88 |
|
89 |
|
90 | const defaults = config.cliDefaults();
|
91 |
|
92 |
|
93 | const envVarOptions = {};
|
94 | config.applyEnvironmentVariables(envVarOptions);
|
95 |
|
96 | program
|
97 | .version(pkg.version)
|
98 | .description('Restore a CouchDB/Cloudant database from a backup text file.')
|
99 | .usage('[options...]')
|
100 | .option('-b, --buffer-size <n>',
|
101 | cliutils.getUsage('number of documents restored at once', defaults.bufferSize),
|
102 | Number)
|
103 | .option('-d, --db <db>',
|
104 | cliutils.getUsage('name of the new, existing database to restore to', defaults.db))
|
105 | .option('-k, --iam-api-key <API key>',
|
106 | cliutils.getUsage('IAM API key to access the Cloudant server'))
|
107 | .option('-p, --parallelism <n>',
|
108 | cliutils.getUsage('number of HTTP requests to perform in parallel when restoring a backup', defaults.parallelism),
|
109 | Number)
|
110 | .option('-q, --quiet',
|
111 | cliutils.getUsage('suppress batch messages', defaults.quiet))
|
112 | .option('-t, --request-timeout <n>',
|
113 | cliutils.getUsage('milliseconds to wait for a response to a HTTP request before retrying the request', defaults.requestTimeout),
|
114 | Number)
|
115 | .option('-u, --url <url>',
|
116 | cliutils.getUsage('URL of the CouchDB/Cloudant server', defaults.url))
|
117 | .parse(process.argv);
|
118 |
|
119 |
|
120 |
|
121 | const opts = Object.assign({}, defaults, envVarOptions, program.opts());
|
122 |
|
123 | return opts;
|
124 | }
|
125 |
|
126 | module.exports = {
|
127 | parseBackupArgs: parseBackupArgs,
|
128 | parseRestoreArgs: parseRestoreArgs
|
129 | };
|