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 | ;
|
15 |
|
16 | /**
|
17 | * Utility methods for the command line interface.
|
18 | * @module cliutils
|
19 | * @see module:cliutils
|
20 | */
|
21 |
|
22 | const url = require('url');
|
23 |
|
24 | module.exports = {
|
25 |
|
26 | /**
|
27 | * Combine a base URL and a database name, ensuring at least single slash
|
28 | * between root and database name. This allows users to have Couch behind
|
29 | * proxies that mount Couch's / endpoint at some other mount point.
|
30 | * @param {string} root - root URL
|
31 | * @param {string} databaseName - database name
|
32 | * @return concatenated URL.
|
33 | *
|
34 | * @private
|
35 | */
|
36 | databaseUrl: function databaseUrl(root, databaseName) {
|
37 | if (!root.endsWith('/')) {
|
38 | root = root + '/';
|
39 | }
|
40 | return new url.URL(encodeURIComponent(databaseName), root).toString();
|
41 | },
|
42 |
|
43 | /**
|
44 | * Generate CLI argument usage text.
|
45 | *
|
46 | * @param {string} description - argument description.
|
47 | * @param {string} defaultValue - default argument value.
|
48 | *
|
49 | * @private
|
50 | */
|
51 | getUsage: function getUsage(description, defaultValue) {
|
52 | return `${description} ${defaultValue !== undefined ? ` (default: ${defaultValue})` : ''}`;
|
53 | }
|
54 | };
|