This repository has been archived by the owner on Apr 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathindex.ts
191 lines (168 loc) · 6.24 KB
/
index.ts
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#!/usr/bin/env node
/**
* Module dependencies.
*/
import yaml = require('js-yaml');
import program = require('commander');
import docsBaseImport = require('./docs');
import validatorBaseImport = require('./validator');
require('colors');
const util = require('util');
let version = require('../package').version;
/**
* Used for parsing comma separated commandline argument values whilst, taking into account backslash escapes.
* Returns an array of strings (e.g. ["Arg1=Val1", "Arg2=Val2"]).
*/
function list(val: string) {
// prepare for a negated lookahead
val = val.replace(/\\,/g, ',\\');
// split and remove escapes
return val.split(/,(?!\\)/g)
.map((x) => {
return x.replace(/,\\/g, ',');
})
.filter((x) => {
return !!x;
});
}
function doNoCommand() {
console.error('\n', 'No command provided!');
process.exit(1);
}
function doNoArgument() {
console.error('\n', 'Missing required argument!');
process.exit(1);
}
program
.version(version)
.action(function() {
program.help();
doNoCommand();
})
.on('--help', function() {
doNoCommand();
});
program
.command('validate')
.usage('<file> [options]')
.option('-p, --parameters <items>', 'List of params', list)
.option('-p, --pseudo <items>', 'List of pseudo overrides', list)
.option('--guess-parameters', 'Guess any parameters that are not explicitely passed in and have no Default. This is the default behaviour.')
.option('-G, --no-guess-parameters', 'Fail validation if a parameter with no Default is not passed')
.option('-g, --only-guess-parameters <items>', 'Guess the provided parameters, and fail validation if a parameter with no Default is passed', list)
.option('-i, --import-values <items>', 'List of import values', list)
.option('-c, --custom-resource-attributes <items>', 'List of attributes', list)
.option('-v, --verbose', 'Verbose error messages')
.action(function(file, cmd) {
// Patch for CommanderJS bug that defaults this to true
if (cmd.parent.rawArgs.indexOf('--guess-parameters') != -1) {
cmd.guessParameters = true;
}
const validator = require('./validator') as typeof validatorBaseImport;
if(cmd.parameters) {
for(let param of cmd.parameters) {
// Set the parameter
let kv = param.split('=');
validator.addParameterValue(kv[0], kv[1]);
}
}
if(cmd.importValues) {
for(let imp of cmd.importValues) {
let kv = imp.split('=');
validator.addImportValue(kv[0], kv[1]);
}
}
if(cmd.pseudo) {
for(let pseudo of cmd.pseudo) {
// Set the parameter
let kv = pseudo.split('=');
validator.addPseudoValue(kv[0], kv[1]);
}
}
if(cmd.customResourceAttributes){
for(let customResourceAttribute of cmd.customResourceAttributes){
// Set the parameter
let kv = customResourceAttribute.split('=');
let key_kv = kv[0].split('.');
let resource: string = key_kv[0];
let attribute: string = key_kv[1];
let value: any = yaml.safeLoad(kv[1]);
validator.addCustomResourceAttributeValue(resource, attribute, value);
}
}
let guessParameters: string[] | undefined;
if (cmd.guessParameters === false) {
guessParameters = [];
} else if (cmd.onlyGuessParameters) {
guessParameters = cmd.onlyGuessParameters;
} else {
guessParameters = undefined;
}
const options = {
guessParameters
};
let result = Object();
try {
result = validator.validateFile(file, options);
} catch(err) {
let error: string = function(msg: string, errors: any) {
for (let error of Object.keys(errors)) {
if (RegExp(error).test(msg)) {
return errors[error];
}
}
return errors[''];
}(err.message, {
'Could not find file .*. Check the input path.': 'No such file.',
'': 'Unable to parse template! Use --verbose for more information.'
});
console.log(error);
if (cmd.verbose) {
console.error(err);
}
process.exit(1);
}
// Show the errors
console.log((result['errors']['info'].length + " infos").grey);
for(let info of result['errors']['info']) {
console.log('Resource: '+ info['resource'].grey);
console.log('Message: '+ info['message'].grey);
console.log('Documentation: '+ info['documentation'].grey + '\n');
}
console.log((result['errors']['warn'].length + " warn").yellow);
for(let warn of result['errors']['warn']) {
console.log('Resource: ' + warn['resource'].yellow);
console.log('Message: ' + warn['message'].yellow);
console.log('Documentation: ' + warn['documentation'].yellow + '\n');
}
console.log((result['errors']['crit'].length + " crit").red);
for(let crit of result['errors']['crit']) {
console.log('Resource: ' + crit['resource'].red);
console.log('Message: ' + crit['message'].red);
console.log('Documentation: ' + crit['documentation'].red + '\n');
}
if(result['templateValid'] === false) {
console.log('Template invalid!'.red.bold);
process.exit(1);
} else {
console.log('Template valid!'.green);
process.exit(0);
}
})
.on('--help', function() {
doNoArgument();
});
program
.command('docs')
.usage('<reference> [options]')
.action(function(reference) {
const docs = require('./docs') as typeof docsBaseImport;
console.log(docs.getDoc(reference));
})
.on('--help', function() {
doNoArgument();
});
if (process.argv.length < 4) {
process.argv.push('--help');
}
program.parse(process.argv);