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 | var 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('-r, --resume',
|
56 | cliutils.getUsage('continue a previous backup from its last known position; invalid in "shallow" mode', defaults.resume))
|
57 | .option('-t, --request-timeout <n>',
|
58 | cliutils.getUsage('milliseconds to wait for a response to a HTTP request before retrying the request', defaults.requestTimeout),
|
59 | Number)
|
60 | .option('-u, --url <url>',
|
61 | cliutils.getUsage('URL of the CouchDB/Cloudant server', defaults.url))
|
62 | .parse(process.argv);
|
63 |
|
64 |
|
65 | if (program.mode === 'shallow' || envVarOptions.mode === 'shallow') {
|
66 | delete defaults.parallelism;
|
67 | delete defaults.log;
|
68 | delete defaults.resume;
|
69 | }
|
70 |
|
71 |
|
72 |
|
73 | const opts = Object.assign({}, defaults, envVarOptions, program);
|
74 |
|
75 | if (opts.resume && (opts.log === defaults.log)) {
|
76 |
|
77 |
|
78 | error.terminationCallback(new error.BackupError('NoLogFileName', 'To resume a backup, a log file must be specified'));
|
79 | }
|
80 |
|
81 | return opts;
|
82 | }
|
83 |
|
84 | function parseRestoreArgs() {
|
85 | var program = require('commander');
|
86 |
|
87 |
|
88 | const defaults = config.cliDefaults();
|
89 |
|
90 |
|
91 | const envVarOptions = {};
|
92 | config.applyEnvironmentVariables(envVarOptions);
|
93 |
|
94 | program
|
95 | .version(pkg.version)
|
96 | .description('Restore a CouchDB/Cloudant database from a backup text file.')
|
97 | .usage('[options...]')
|
98 | .option('-b, --buffer-size <n>',
|
99 | cliutils.getUsage('number of documents restored at once', defaults.bufferSize),
|
100 | Number)
|
101 | .option('-d, --db <db>',
|
102 | cliutils.getUsage('name of the new, existing database to restore to', defaults.db))
|
103 | .option('-k, --iam-api-key <API key>',
|
104 | cliutils.getUsage('IAM API key to access the Cloudant server'))
|
105 | .option('-p, --parallelism <n>',
|
106 | cliutils.getUsage('number of HTTP requests to perform in parallel when restoring a backup', defaults.parallelism),
|
107 | Number)
|
108 | .option('-t, --request-timeout <n>',
|
109 | cliutils.getUsage('milliseconds to wait for a response to a HTTP request before retrying the request', defaults.requestTimeout),
|
110 | Number)
|
111 | .option('-u, --url <url>',
|
112 | cliutils.getUsage('URL of the CouchDB/Cloudant server', defaults.url))
|
113 | .parse(process.argv);
|
114 |
|
115 |
|
116 |
|
117 | const opts = Object.assign({}, defaults, envVarOptions, program);
|
118 |
|
119 | return opts;
|
120 | }
|
121 |
|
122 | module.exports = {
|
123 | parseBackupArgs: parseBackupArgs,
|
124 | parseRestoreArgs: parseRestoreArgs
|
125 | };
|