-
Notifications
You must be signed in to change notification settings - Fork 11
/
configure.js
158 lines (143 loc) · 2.98 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
"use strict";
let homeDir = require('os').homedir();
let path = require("path");
let fs = require("fs");
let extend = require("extend");
let async = require("async");
const readline = require('readline');
let configPath = path.resolve(`${homeDir}/.leo`, "config.json");
let configDir = path.dirname(configPath);
let parsed = parse();
let options = parsed.options;
let commands = parsed.commands;
if (commands[0] == "show") {
let p = options.leoprofile || "default";
console.log(`\nProfile: ${p}`);
console.log(JSON.stringify(get()[p] || {}, null, 2));
return;
}
let questions = [{
field: "leoprofile",
question: "Leo profile to configure? [Enter for default]",
default: "default"
}, {
field: "region",
question: "AWS Region? [Enter to skip]"
}, {
field: "kinesis",
question: "Kinesis stream? [Enter to skip]"
}, {
field: "s3",
question: "S3 stream? [Enter to skip]"
}, {
field: "firehose",
question: "Firehose stream? [Enter to skip]"
}, {
field: "profile",
question: "AWS Profile? [Enter to skip]"
}, ];
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
async.eachSeries(questions, (data, done) => {
if (!options[data.field]) {
rl.question(data.question + " ", (input) => {
let transform = data.transform || (a => a);
options[data.field] = input != "" ? transform(input) : data.default;
done();
});
} else {
done();
}
}, () => {
rl.close();
let config = get();
var leoprofile = options.leoprofile || "default";
delete options.leoprofile;
extend(true, config, {
[leoprofile]: options
});
fs.writeFileSync(configPath, JSON.stringify(config, null, 2));
console.log(`\nLeo profile "${leoprofile}" updated!`);
});
function get() {
createPath(configDir);
let config = {
default: {}
};
if (fs.existsSync(configPath)) {
config = require(configPath) || {};
}
return config;
}
function createPath(dir) {
if (!fs.existsSync(dir)) {
var parent = path.dirname(dir);
if (parent) {
createPath(parent);
}
fs.mkdirSync(dir);
}
}
function parse() {
let optionsMap = {
p: {
name: "leoprofile",
consume: 1
},
profile: {
name: "leoprofile",
consume: 1
},
kinesis: {
name: "kinesis",
consume: 1
},
s3: {
name: "s3",
consume: 1
},
firehose: {
name: "firehose",
consume: 1
},
r: {
name: "region",
consume: 1
},
region: {
name: "region",
consume: 1
},
};
let options = {};
let commands = [];
let regex = /^-(.)$|^--(.*)$/;
let args = [].concat(process.argv.concat(process.execArgv));
for (let i = 0; i < args.length; i++) {
let arg = args[i];
var o = arg.match(regex);
if (arg != "--" && o) {
var c = optionsMap[o[1] || o[2]] || {
name: o[1],
consume: 0
};
var key = c.name;
if (c.consume == 0) {
options[key] = true;
} else {
if (!(args[i + c.consume] || "").match(regex)) {
options[key] = args[i + c.consume];
i += c.consume;
}
}
} else if (i > 1) {
commands.push(arg);
}
}
return {
options,
commands
};
}