UNPKG

5.16 kBJavaScriptView Raw
1// Copyright © 2017, 2018 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 var 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('-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 // Remove defaults that don't apply when using shallow mode
65 if (program.mode === 'shallow' || envVarOptions.mode === 'shallow') {
66 delete defaults.parallelism;
67 delete defaults.log;
68 delete defaults.resume;
69 }
70
71 // Apply the options in order so that the CLI overrides env vars and env variables
72 // override defaults.
73 const opts = Object.assign({}, defaults, envVarOptions, program);
74
75 if (opts.resume && (opts.log === defaults.log)) {
76 // If resuming and the log file arg is the newly generated tmp name from defaults then we know that --log wasn't specified.
77 // We have to do this check here for the CLI case because of the default.
78 error.terminationCallback(new error.BackupError('NoLogFileName', 'To resume a backup, a log file must be specified'));
79 }
80
81 return opts;
82}
83
84function parseRestoreArgs() {
85 var program = require('commander');
86
87 // Option CLI defaults
88 const defaults = config.cliDefaults();
89
90 // Options set by environment variables
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 // Apply the options in order so that the CLI overrides env vars and env variables
116 // override defaults.
117 const opts = Object.assign({}, defaults, envVarOptions, program);
118
119 return opts;
120}
121
122module.exports = {
123 parseBackupArgs: parseBackupArgs,
124 parseRestoreArgs: parseRestoreArgs
125};