UNPKG

5.38 kBJavaScriptView Raw
1// Copyright © 2017, 2021 IBM Corp. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14'use strict';
15
16const cliutils = require('./cliutils.js');
17const config = require('./config.js');
18const error = require('./error.js');
19const path = require('path');
20const pkg = require('../package.json');
21
22function parseBackupArgs() {
23 const program = require('commander');
24
25 // Option CLI defaults
26 const defaults = config.cliDefaults();
27
28 // Options set by environment variables
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 // Remove defaults that don't apply when using shallow mode
67 if (program.opts().mode === 'shallow' || envVarOptions.mode === 'shallow') {
68 delete defaults.parallelism;
69 delete defaults.log;
70 delete defaults.resume;
71 }
72
73 // Apply the options in order so that the CLI overrides env vars and env variables
74 // override defaults.
75 const opts = Object.assign({}, defaults, envVarOptions, program.opts());
76
77 if (opts.resume && (opts.log === defaults.log)) {
78 // If resuming and the log file arg is the newly generated tmp name from defaults then we know that --log wasn't specified.
79 // We have to do this check here for the CLI case because of the default.
80 error.terminationCallback(new error.BackupError('NoLogFileName', 'To resume a backup, a log file must be specified'));
81 }
82
83 return opts;
84}
85
86function parseRestoreArgs() {
87 const program = require('commander');
88
89 // Option CLI defaults
90 const defaults = config.cliDefaults();
91
92 // Options set by environment variables
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 // Apply the options in order so that the CLI overrides env vars and env variables
120 // override defaults.
121 const opts = Object.assign({}, defaults, envVarOptions, program.opts());
122
123 return opts;
124}
125
126module.exports = {
127 parseBackupArgs: parseBackupArgs,
128 parseRestoreArgs: parseRestoreArgs
129};