Skip to content

Commit 8c002da

Browse files
committed
feat(cli): reading from stdin and file
1 parent 00c0eef commit 8c002da

File tree

1 file changed

+47
-75
lines changed

1 file changed

+47
-75
lines changed

index.js

Lines changed: 47 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,12 @@
1-
const Joi = require("joi");
1+
const Joi = require('joi');
2+
const program = require('commander');
3+
const fs = require('fs');
4+
5+
program
6+
.option('-s, --stdin', 'Read from stdin')
7+
.option('-f, --file [filePath]', 'Read from file')
8+
.parse(process.argv);
29

3-
let data = {
4-
engine: {
5-
name: "guardrails-engine-javascript",
6-
version: "1.11.0"
7-
},
8-
language: "javascript",
9-
type: "mixed",
10-
status: "success",
11-
executionTime: 3,
12-
issues: 12,
13-
errors: null,
14-
output: [
15-
{
16-
type: "issue",
17-
process: {
18-
name: "eslint",
19-
version: "^4.19.1"
20-
},
21-
rule: "@guardrails/guardrails/detect-unsafe-regex",
22-
description: "[GR:0001:stable] Unsafe Regular Expression",
23-
location: {
24-
path: "/src/GR0001.js",
25-
positions: {
26-
begin: {
27-
line: 8,
28-
column: 19
29-
},
30-
end: {
31-
line: 8,
32-
column: 19
33-
}
34-
}
35-
}
36-
},
37-
{
38-
id: 566,
39-
updated_at: "2018-05-08T14:27:01.549Z",
40-
created_at: "2018-02-15T16:45:53.321Z",
41-
publish_date: "2018-02-15T16:59:37.240Z",
42-
recommendation: "Update to version 4.2.1, 5.0.3 or later.",
43-
cvss_vector: "CVSS:3.0/AV:L/AC:L/PR:N/UI:N/S:U/C:N/I:N/A:L",
44-
cvss_score: 4,
45-
module: "hoek",
46-
version: "5.0.0",
47-
vulnerable_versions: "<= 4.2.0 || >= 5.0.0 < 5.0.3",
48-
patched_versions: "> 4.2.0 < 5.0.0 || >= 5.0.3",
49-
title: "Prototype pollution attack",
50-
path: ["guardrails-test-javascript@1.0.0", "hoek@5.0.0"],
51-
advisory: "https://nodesecurity.io/advisories/566",
52-
type: "advisory",
53-
process: {
54-
name: "nsp",
55-
version: "^3.2.1"
56-
}
57-
}
58-
]
59-
};
6010
const processSchema = Joi.object().keys({
6111
name: Joi.string().required(),
6212
version: Joi.string().required()
@@ -108,40 +58,62 @@ const baseSchema = Joi.object().keys({
10858
})
10959
.required(),
11060
language: Joi.string()
111-
.valid("javascript", "python", "mixed")
61+
.valid('javascript', 'python', 'mixed')
11262
.required(),
11363
type: Joi.string().required(),
11464
status: Joi.string()
115-
.valid("success", "failure")
65+
.valid('success', 'failure')
11666
.required(),
11767
executionTime: Joi.number().required(),
11868
issues: Joi.number().required(),
11969
errors: [Joi.array(), null],
12070
output: Joi.array().required()
12171
});
12272

123-
// ========== Validate Outer structure:
124-
Joi.validate(data, baseSchema, (err, value) => {
125-
if (err) {
126-
console.log(err);
127-
} else {
128-
console.log("------------------> No Error outer");
73+
let reportData = { output: [] };
74+
75+
function readFromStdin() {
76+
return readFromFile('/dev/stdin');
77+
}
78+
79+
function readFromFile(filePath) {
80+
try {
81+
let data = fs.readFileSync(filePath).toString();
82+
return JSON.parse(data);
83+
} catch (err) {
84+
console.log(err.message);
85+
process.exit(1);
12986
}
130-
});
87+
}
88+
89+
if (program.stdin) {
90+
reportData = readFromStdin();
91+
} else if (program.file) {
92+
reportData = readFromFile(program.file);
93+
}
13194

132-
// ========== Validate Output
133-
Joi.validate(data.output[0], sourceCodeSchema, (err, value) => {
95+
// ========== Validate Outer structure:
96+
Joi.validate(reportData, baseSchema, (err, value) => {
13497
if (err) {
13598
console.log(err);
13699
} else {
137-
console.log("------------------> No Error code");
100+
console.log('envelope ✅');
138101
}
139102
});
140103

141-
Joi.validate(data.output[1], dependenciesSchema, (err, value) => {
142-
if (err) {
143-
console.log(err);
144-
} else {
145-
console.log("------------------> No Error");
104+
reportData.output.forEach(lineItem => {
105+
let schema = Joi.object();
106+
if (lineItem.type === 'issue' || lineItem.type === 'sourcecode') {
107+
schema = sourceCodeSchema;
108+
} else if (lineItem.type === 'advisory') {
109+
schema = dependenciesSchema;
110+
} else if (lineItem.type === 'secrets') {
146111
}
112+
Joi.validate(lineItem, schema, (err, value) => {
113+
if (err) {
114+
console.log(err);
115+
} else {
116+
console.log(lineItem.type + ' ✅');
117+
}
118+
});
147119
});

0 commit comments

Comments
 (0)