UNPKG

3.15 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
16var path = require('path');
17var 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 var defaults = apiDefaults();
38
39 // add additional legacy settings
40 defaults.db = 'test';
41 defaults.url = 'http://localhost:5984';
42
43 return defaults;
44}
45
46/**
47 Override settings **in-place** with environment variables.
48*/
49function applyEnvironmentVariables(opts) {
50 // if we have a custom CouchDB url
51 if (typeof process.env.COUCH_URL !== 'undefined') {
52 opts.url = process.env.COUCH_URL;
53 }
54
55 // if we have a specified databases
56 if (typeof process.env.COUCH_DATABASE !== 'undefined') {
57 opts.db = process.env.COUCH_DATABASE;
58 }
59
60 // if we have a specified buffer size
61 if (typeof process.env.COUCH_BUFFER_SIZE !== 'undefined') {
62 opts.bufferSize = parseInt(process.env.COUCH_BUFFER_SIZE);
63 }
64
65 // if we have a specified parallelism
66 if (typeof process.env.COUCH_PARALLELISM !== 'undefined') {
67 opts.parallelism = parseInt(process.env.COUCH_PARALLELISM);
68 }
69
70 // if we have a specified request timeout
71 if (typeof process.env.COUCH_REQUEST_TIMEOUT !== 'undefined') {
72 opts.requestTimeout = parseInt(process.env.COUCH_REQUEST_TIMEOUT);
73 }
74
75 // if we have a specified log file
76 if (typeof process.env.COUCH_LOG !== 'undefined') {
77 opts.log = path.normalize(process.env.COUCH_LOG);
78 }
79
80 // if we are instructed to resume
81 if (typeof process.env.COUCH_RESUME !== 'undefined' && process.env.COUCH_RESUME === 'true') {
82 opts.resume = true;
83 }
84
85 // if we are given an output filename
86 if (typeof process.env.COUCH_OUTPUT !== 'undefined') {
87 opts.output = path.normalize(process.env.COUCH_OUTPUT);
88 }
89
90 // if we only want a shallow copy
91 if (typeof process.env.COUCH_MODE !== 'undefined' && process.env.COUCH_MODE === 'shallow') {
92 opts.mode = 'shallow';
93 }
94
95 // if we have a specified API key
96 if (typeof process.env.CLOUDANT_IAM_API_KEY !== 'undefined') {
97 opts.iamApiKey = process.env.CLOUDANT_IAM_API_KEY;
98 }
99
100 // if we have a specified IAM token endpoint
101 if (typeof process.env.CLOUDANT_IAM_TOKEN_URL !== 'undefined') {
102 opts.iamTokenUrl = process.env.CLOUDANT_IAM_TOKEN_URL;
103 }
104}
105
106module.exports = {
107 apiDefaults: apiDefaults,
108 cliDefaults: cliDefaults,
109 applyEnvironmentVariables: applyEnvironmentVariables
110};