UNPKG

3.36 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 path = require('path');
17const tmp = require('tmp');
18
19/**
20 Return API default settings.
21*/
22function apiDefaults() {
23 return {
24 parallelism: 5,
25 bufferSize: 500,
26 requestTimeout: 120000,
27 log: tmp.fileSync().name,
28 resume: false,
29 mode: 'full'
30 };
31}
32
33/**
34 Return CLI default settings.
35*/
36function cliDefaults() {
37 const defaults = apiDefaults();
38
39 // add additional legacy settings
40 defaults.db = 'test';
41 defaults.url = 'http://localhost:5984';
42
43 // add CLI only option
44 defaults.quiet = false;
45
46 return defaults;
47}
48
49/**
50 Override settings **in-place** with environment variables.
51*/
52function applyEnvironmentVariables(opts) {
53 // if we have a custom CouchDB url
54 if (typeof process.env.COUCH_URL !== 'undefined') {
55 opts.url = process.env.COUCH_URL;
56 }
57
58 // if we have a specified databases
59 if (typeof process.env.COUCH_DATABASE !== 'undefined') {
60 opts.db = process.env.COUCH_DATABASE;
61 }
62
63 // if we have a specified buffer size
64 if (typeof process.env.COUCH_BUFFER_SIZE !== 'undefined') {
65 opts.bufferSize = parseInt(process.env.COUCH_BUFFER_SIZE);
66 }
67
68 // if we have a specified parallelism
69 if (typeof process.env.COUCH_PARALLELISM !== 'undefined') {
70 opts.parallelism = parseInt(process.env.COUCH_PARALLELISM);
71 }
72
73 // if we have a specified request timeout
74 if (typeof process.env.COUCH_REQUEST_TIMEOUT !== 'undefined') {
75 opts.requestTimeout = parseInt(process.env.COUCH_REQUEST_TIMEOUT);
76 }
77
78 // if we have a specified log file
79 if (typeof process.env.COUCH_LOG !== 'undefined') {
80 opts.log = path.normalize(process.env.COUCH_LOG);
81 }
82
83 // if we are instructed to resume
84 if (typeof process.env.COUCH_RESUME !== 'undefined' && process.env.COUCH_RESUME === 'true') {
85 opts.resume = true;
86 }
87
88 // if we are given an output filename
89 if (typeof process.env.COUCH_OUTPUT !== 'undefined') {
90 opts.output = path.normalize(process.env.COUCH_OUTPUT);
91 }
92
93 // if we only want a shallow copy
94 if (typeof process.env.COUCH_MODE !== 'undefined' && process.env.COUCH_MODE === 'shallow') {
95 opts.mode = 'shallow';
96 }
97
98 // if we are instructed to be quiet
99 if (typeof process.env.COUCH_QUIET !== 'undefined' && process.env.COUCH_QUIET === 'true') {
100 opts.quiet = true;
101 }
102
103 // if we have a specified API key
104 if (typeof process.env.CLOUDANT_IAM_API_KEY !== 'undefined') {
105 opts.iamApiKey = process.env.CLOUDANT_IAM_API_KEY;
106 }
107
108 // if we have a specified IAM token endpoint
109 if (typeof process.env.CLOUDANT_IAM_TOKEN_URL !== 'undefined') {
110 opts.iamTokenUrl = process.env.CLOUDANT_IAM_TOKEN_URL;
111 }
112}
113
114module.exports = {
115 apiDefaults: apiDefaults,
116 cliDefaults: cliDefaults,
117 applyEnvironmentVariables: applyEnvironmentVariables
118};