-
Notifications
You must be signed in to change notification settings - Fork 19
/
configure.js
128 lines (103 loc) · 3.58 KB
/
configure.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
'use strict';
const path = require('path');
const ConfigLoader = require('../config-loader');
const TerraformCommand = require('../terraform-command');
const { config, cfgPath } = require('../parameters');
class ConfigureCommand extends TerraformCommand {
/**
* Command configuration
*/
configure() {
this.setName('configure')
.setDescription('add, change or remove config parameters from terrahub config files')
.addOption('config', 'c', 'Configuration parameter to be affected by command execution', String, '')
.addOption('global', 'G', 'Update global config file instead of root or local', Boolean, false);
}
/**
* @returns {Promise}
*/
run() {
const configContent = this.getOption('config');
const global = this.getOption('global');
const data = configContent instanceof Array ? configContent : [configContent];
if (global === true) {
const content = ConfigLoader.readConfig(cfgPath);
data.forEach(it => this._updateConfig(it, content));
ConfigLoader.writeConfig(content, cfgPath);
return Promise.resolve('Done');
}
if ([this.getOption('include'), this.getOption('exclude'),
this.getOption('exclude-regex'), this.getOption('include-regex')].some(it => it.length)) {
const configs = this.getConfig();
Object.keys(configs).forEach(key => {
const componentPath = path.join(configs[key].project.root, configs[key].root, config.defaultFileName);
const content = ConfigLoader.readConfig(componentPath);
data.forEach(it => this._updateConfig(it, content));
ConfigLoader.writeConfig(content, componentPath);
});
return Promise.resolve('Done');
}
const rootConfigPath = path.join(this.getAppPath(), config.defaultFileName);
const content = ConfigLoader.readConfig(rootConfigPath);
data.forEach(it => this._updateConfig(it, content));
ConfigLoader.writeConfig(content, rootConfigPath);
return Promise.resolve('Done');
}
/**
* @param {String} string
* @param {Object} content
* @return {Object}
* @private
*/
_updateConfig(string, content) {
const regex = /([^[]+)\[\d*]/;
let [keyString, ...value] = string.split('=');
value = value.join('=');
let destination = content;
const keys = keyString.split('.');
for (let i = 0; i < keys.length - 1; i++) {
const key = keys[i];
destination = this._intermidiateFill(destination, key, regex);
}
const lastKey = keys[keys.length - 1];
const match = lastKey.match(regex);
const finalKey = match ? match.slice(-2)[1] : lastKey;
let load;
try {
load = JSON.parse(value);
} catch (error) {
load = value;
}
if (match) {
if (!destination[finalKey] || !(destination[finalKey] instanceof Array)) {
destination[finalKey] = [];
}
destination[finalKey].push(load);
} else {
destination[finalKey] = load;
}
}
/**
* @param {Object|Array} destination
* @param {String} key
* @param {RegExp} regex
* @return {Object|Array}
* @private
*/
_intermidiateFill(destination, key, regex) {
const match = key.match(regex);
const finalKey = match ? match.slice(-2)[1] : key;
const value = match ? [] : {};
if (destination instanceof Array) {
destination.push(value);
destination = destination[destination.length - 1];
} else {
if (!match && !destination.hasOwnProperty(finalKey)) {
destination[finalKey] = value;
}
destination = destination[finalKey];
}
return destination;
}
}
module.exports = ConfigureCommand;