-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathtmosXcDiag.ts
254 lines (187 loc) · 7.15 KB
/
tmosXcDiag.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
'use strict';
import path from "path";
import fs from "fs";
import {
commands,
ExtensionContext,
Diagnostic,
DiagnosticCollection,
DiagnosticSeverity,
languages,
Position,
Range,
TextDocument,
Uri,
workspace
} from "vscode";
import { logger } from "./logger";
import { isArray } from "f5-conx-core";
export type xcDiagRule = {
code: string;
severity: "Error" | "Warning" | "Information" | "Hint";
title: string;
message: string;
regex: string;
};
export class XcDiag {
public enabled: boolean = false;
public lastDoc: TextDocument | undefined = undefined;
public diagXC: DiagnosticCollection;
settingsFileLocation: string;
rules: xcDiagRule[];
countDefualtRedirect: boolean = false;
constructor(context: ExtensionContext) {
// create diag collection
this.diagXC = languages.createDiagnosticCollection('f5-tmos-xc');
this.settingsFileLocation = path.join(context.extensionPath, 'diagRules', 'tmosXcRules.json');
this.rules = this.loadRules();
context.subscriptions.push(
workspace.onDidChangeTextDocument(e => this.updateDiagnostic(e.document))
);
context.subscriptions.push(
workspace.onDidCloseTextDocument(doc => this.diagXC.delete(doc.uri))
);
}
loadRules() {
logger.info("loading tmos -> xc rules file");
return this.rules = JSON.parse(fs.readFileSync(this.settingsFileLocation).toString());
}
openRules() {
// const loc = path.join(context.Extens)
// workspace.openTextDocument(this.settingsFileLocation);
logger.info("opening tmos -> xc rules file");
return commands.executeCommand("vscode.open", Uri.file(this.settingsFileLocation));
// workbench.action.files.openFile
}
// parse app to see if it gets excluded from diagnostics
getDiagnosticExlusion(text: string) {
// setup reasons array
const reasons: string[] = [];
const vsReg = /ltm virtual ([\/\w]+)/;
// capture virtual server (app name)
const vs = vsReg.exec(text);
const vsName = (vs && vs.length > 0) ? vs[1] : "no-app-name-found";
// setup the regex for the default redirect
const defaultRedirect = new RegExp('\/Common\/_sys_https_redirect');
// // test app for default redirect string
// if (defaultRedirect.test(text)) {
// reasons.push('default redirect');
// }
// return vs name and exlusion reasons
return { vs: vsName, reasons };
}
/**
* recursive function to dig config for diagnostics
* @param text
* @param diags (optional, only called from itself)
* @returns array of diagnostics
*/
getDiagnostic(text: string | string[], diags: Diagnostic[] = []): Diagnostic[] {
// setup diagnostics array
// const diags: Diagnostic[] = [];
// scan entire config for default redirect
if (isArray(text)) {
// recast the type as array
const apps = text as string[];
// loop through apps
apps.forEach(app => {
// if we don't have any app exclusions (this just excludes the app from diagnostics)
if (this.getDiagnosticExlusion(app).reasons.length === 0) {
// get diagnostics for app, but use the same diagnostic array
diags = this.getDiagnostic(app, diags);
}
});
} else {
// recast the type as string
text = text as string;
// if we don't have any app exclusions (this just excludes the app from diagnostics)
if (this.getDiagnosticExlusion(text).reasons.length === 0 && text) {
// split the config into lines
const lines = text.split('\n');
lines.forEach((value, index) => {
// loop through rules on each line
this.rules.forEach(rule => {
// if rule empty, pass
if (rule.regex === '') { return; }
// look for rule regex
const match = value.match(rule.regex);
if (match) {
// set rule severity
const severity
= rule.severity === "Error" ? DiagnosticSeverity.Error
: rule.severity === "Warning" ? DiagnosticSeverity.Warning
: rule.severity === "Information" ? DiagnosticSeverity.Information
: DiagnosticSeverity.Hint;
// push diagnostic
diags.push({
code: rule.code,
message: rule.message,
range: new Range(
new Position(index, match.index || 0),
new Position(index, match[0].length + (match.index || 0))
),
severity
});
}
});
});
}
}
return diags;
}
getDiagStats(diags: Diagnostic[]) {
const stats: {
Error?: number;
Warning?: number;
Information?: number;
Hint?: number
} = {};
diags.forEach((d) => {
if (d.severity === 0) {
if (stats.Error) {
stats.Error = stats.Error + 1;
} else {
stats.Error = 1;
}
}
if (d.severity === 1) {
if (stats.Warning) {
stats.Warning = stats.Warning + 1;
} else {
stats.Warning = 1;
}
}
if (d.severity === 2) {
if (stats.Information) {
stats.Information = stats.Information + 1;
} else {
stats.Information = 1;
}
}
if (d.severity === 3) {
if (stats.Hint) {
stats.Hint = stats.Hint + 1;
} else {
stats.Hint = 1;
}
}
});
return stats;
}
updateDiagnostic(doc: TextDocument) {
if(doc.fileName === 'app.conf') {
// clear current diags in this class
this.diagXC.clear();
// clear the current diags in the doc/editor
this.diagXC.delete(doc.uri);
if(this.enabled) {
// capture the doc we are working with
this.lastDoc = doc;
// get the text from the doc/editor and feed through xc diagnostics
const diags = this.getDiagnostic(doc.getText());
// pubish the diags to document
this.diagXC.set(doc.uri, diags);
}
}
}
}