Skip to content

Commit 6d1129c

Browse files
committed
prettify
1 parent 7882a32 commit 6d1129c

File tree

5 files changed

+106
-86
lines changed

5 files changed

+106
-86
lines changed

cli.js

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
11
#!/usr/bin/env node
22
/* eslint no-console: "off" */
3-
const os = require("os");
4-
const commander = require("commander");
5-
const puppetdbquery = require("./main");
6-
const querystring = require("querystring");
7-
const fs = require("fs");
8-
const http = require("http");
9-
const https = require("https");
3+
const os = require('os');
4+
const commander = require('commander');
5+
const puppetdbquery = require('./main');
6+
const querystring = require('querystring');
7+
const fs = require('fs');
8+
const http = require('http');
9+
const https = require('https');
1010

1111
const opts = commander
12-
.command("find-nodes [options] <query>")
13-
.option("-H, --host <host>", "PuppetDB host")
14-
.option("-p, --port <port>", "PuppetDB port", 8080)
15-
.option("-s, --ssl", "Use SSL")
12+
.command('find-nodes [options] <query>')
13+
.option('-H, --host <host>', 'PuppetDB host')
14+
.option('-p, --port <port>', 'PuppetDB port', 8080)
15+
.option('-s, --ssl', 'Use SSL')
1616
.option(
17-
"--key <keyfile>",
18-
"Private SSL key file",
19-
`/etc/puppet/ssl/private_keys/${os.hostname()}.pem`
17+
'--key <keyfile>',
18+
'Private SSL key file',
19+
`/etc/puppet/ssl/private_keys/${os.hostname()}.pem`,
2020
)
2121
.option(
22-
"--cert <certfile>",
23-
"SSL certificate file",
24-
`/etc/puppet/ssl/certs/${os.hostname()}.pem`
22+
'--cert <certfile>',
23+
'SSL certificate file',
24+
`/etc/puppet/ssl/certs/${os.hostname()}.pem`,
2525
)
26-
.option("--ca <cafile>", "SSL CA certificate file", "/etc/puppet/ssl/ca.pem")
27-
.option("-P, --print", "Print parsed query and exit")
26+
.option('--ca <cafile>', 'SSL CA certificate file', '/etc/puppet/ssl/ca.pem')
27+
.option('-P, --print', 'Print parsed query and exit')
2828
.parse(process.argv);
2929

3030
let query;
@@ -45,8 +45,8 @@ if (opts.print) {
4545
port: opts.port,
4646
path: `/pdb/query/v4/nodes?${querystring.stringify({ query })}`,
4747
headers: {
48-
Accept: "application/json"
49-
}
48+
Accept: 'application/json',
49+
},
5050
};
5151
let httplib;
5252
if (opts.ssl) {
@@ -61,17 +61,17 @@ if (opts.print) {
6161
// We have the full response, parse it and print the node names
6262
httplib
6363
.get(options, res => {
64-
let data = "";
65-
res.on("data", chunk => {
64+
let data = '';
65+
res.on('data', chunk => {
6666
data += chunk;
6767
});
6868

69-
return res.on("end", () =>
70-
JSON.parse(data).forEach(node => console.log(node.certname))
69+
return res.on('end', () =>
70+
JSON.parse(data).forEach(node => console.log(node.certname)),
7171
);
7272
})
7373

74-
.on("error", e =>
75-
console.log(`Error fetching from ${opts.host} : ${opts.port} ${e}`)
74+
.on('error', e =>
75+
console.log(`Error fetching from ${opts.host} : ${opts.port} ${e}`),
7676
);
7777
}

lib/ast.js

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@ const isBoolean = builtin.boolean;
1010
const defaults = shared.defaults;
1111
const geq = shared.geq;
1212

13-
def('Printable')
14-
.field('loc',
15-
or(def('SourceLocation'), null),
16-
defaults.null,
17-
true);
13+
def('Printable').field(
14+
'loc',
15+
or(def('SourceLocation'), null),
16+
defaults.null,
17+
true,
18+
);
1819

1920
def('Node')
2021
.bases('Printable')
@@ -31,8 +32,7 @@ def('Position')
3132
.field('line', geq(1))
3233
.field('column', geq(0));
3334

34-
def('Literal')
35-
.bases('Node');
35+
def('Literal').bases('Node');
3636

3737
// Merge literals into Literal type?
3838
def('Boolean')
@@ -55,8 +55,7 @@ def('Date')
5555
.build('value')
5656
.field('value', isString);
5757

58-
def('Expression')
59-
.bases('Node');
58+
def('Expression').bases('Node');
6059

6160
def('BinaryExpression')
6261
.bases('Expression')
@@ -112,9 +111,7 @@ def('IdentifierPath')
112111
def('Query')
113112
.bases('Node')
114113
.build('expression')
115-
.field('expression',
116-
or(def('Expression'), null),
117-
defaults.null);
114+
.field('expression', or(def('Expression'), null), defaults.null);
118115

119116
def('Subquery')
120117
.bases('Expression')
@@ -128,9 +125,7 @@ def('Resource')
128125
.field('res_type', isString)
129126
.field('title', def('Identifier'))
130127
.field('exported', isBoolean)
131-
.field('parameters',
132-
or(def('BlockExpression'), null),
133-
defaults.null);
128+
.field('parameters', or(def('BlockExpression'), null), defaults.null);
134129

135130
def('RegexpNodeMatch')
136131
.bases('Expression')

lib/evaluator.js

Lines changed: 54 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import timespec from 'timespec';
22
import { visit } from './ast';
3-
import { formatLocation, capitalizeClass, regexpEscape, capitalize } from './util';
3+
import {
4+
formatLocation,
5+
capitalizeClass,
6+
regexpEscape,
7+
capitalize,
8+
} from './util';
49

510
const comparison = (operator, left, right) => {
611
if (operator === '!=' || operator === '!~') {
@@ -9,19 +14,29 @@ const comparison = (operator, left, right) => {
914
return [operator, left, right];
1015
};
1116

12-
export default (ast) => {
17+
export default ast => {
1318
const mode = ['fact'];
1419
return visit(ast, {
1520
visitComparison(path) {
1621
this.traverse(path);
1722
// Function to handle negating comparisons
1823
if (mode[0] === 'fact') {
19-
return ['in', 'certname',
20-
['extract', 'certname',
21-
['select_fact_contents',
22-
['and',
24+
return [
25+
'in',
26+
'certname',
27+
[
28+
'extract',
29+
'certname',
30+
[
31+
'select_fact_contents',
32+
[
33+
'and',
2334
path.node.left,
24-
comparison(path.node.operator, 'value', path.node.right)]]]];
35+
comparison(path.node.operator, 'value', path.node.right),
36+
],
37+
],
38+
],
39+
];
2540
} else if (mode[0] === 'subquery') {
2641
let left;
2742
if (path.node.left.length === 1) {
@@ -32,9 +47,17 @@ export default (ast) => {
3247
return comparison(path.node.operator, left, path.node.right);
3348
} else if (mode[0] === 'resource') {
3449
if (path.node.left[0] === 'tag') {
35-
return comparison(path.node.operator, path.node.left[0], path.node.right);
50+
return comparison(
51+
path.node.operator,
52+
path.node.left[0],
53+
path.node.right,
54+
);
3655
}
37-
return comparison(path.node.operator, ['parameter', path.node.left[0]], path.node.right);
56+
return comparison(
57+
path.node.operator,
58+
['parameter', path.node.left[0]],
59+
path.node.right,
60+
);
3861
}
3962
throw Error(`Unknown mode ${mode}`);
4063
},
@@ -85,9 +108,15 @@ export default (ast) => {
85108
mode.unshift('subquery');
86109
this.traverse(path);
87110
mode.shift();
88-
return ['in', 'certname',
89-
['extract', 'certname',
90-
[`select_${path.node.endpoint}s`, path.node.expression]]];
111+
return [
112+
'in',
113+
'certname',
114+
[
115+
'extract',
116+
'certname',
117+
[`select_${path.node.endpoint}s`, path.node.expression],
118+
],
119+
];
91120
},
92121
visitRegexpNodeMatch(path) {
93122
mode.unshift('regexp');
@@ -98,11 +127,7 @@ export default (ast) => {
98127
visitIdentifierPath(path) {
99128
this.traverse(path);
100129
if (mode[0] === 'fact') {
101-
return [
102-
(path.node.regexp ? '~>' : '='),
103-
'path',
104-
path.node.components,
105-
];
130+
return [path.node.regexp ? '~>' : '=', 'path', path.node.components];
106131
}
107132
return path.node.components;
108133
},
@@ -116,24 +141,28 @@ export default (ast) => {
116141
return path.node.name;
117142
},
118143
visitResource(path) {
119-
const regexp = (path.node.title.type === 'RegexpIdentifier');
144+
const regexp = path.node.title.type === 'RegexpIdentifier';
120145
mode.unshift('resource');
121146
this.traverse(path);
122147
mode.shift();
123148
let { title } = path.node;
124149
if (!regexp && capitalize(path.node.res_type) === 'Class') {
125150
title = capitalizeClass(title);
126151
}
127-
const andExpr = ['and',
128-
['=', 'type', capitalizeClass(path.node.res_type)],
129-
[(regexp ? '~' : '='), 'title', title],
130-
['=', 'exported', path.node.exported]];
152+
const andExpr = [
153+
'and',
154+
['=', 'type', capitalizeClass(path.node.res_type)],
155+
[regexp ? '~' : '=', 'title', title],
156+
['=', 'exported', path.node.exported],
157+
];
131158
if (path.node.parameters) {
132159
andExpr.push(path.node.parameters);
133160
}
134-
return ['in', 'certname',
135-
['extract', 'certname',
136-
['select_resources', andExpr]]];
161+
return [
162+
'in',
163+
'certname',
164+
['extract', 'certname', ['select_resources', andExpr]],
165+
];
137166
},
138167
});
139168
};

lib/main.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,9 @@ import './util';
33
import parser from './parser.jison';
44
import evaluator from './evaluator';
55

6-
const parse = (query) => {
6+
const parse = query => {
77
const ast = parser.parse(query);
88
return evaluator(ast);
99
};
1010

11-
export {
12-
evaluator,
13-
parser,
14-
parse,
15-
};
11+
export { evaluator, parser, parse };

lib/util.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,34 @@
11
import ast from './ast';
22

33
const capitalize = s => s[0].toUpperCase() + s.slice(1).toLowerCase();
4-
const capitalizeClass = s => s.split('::').map(module.exports.capitalize)
4+
const capitalizeClass = s =>
5+
s
6+
.split('::')
7+
.map(module.exports.capitalize)
58
.join('::');
69

7-
const regexpEscape = s => String(s).replace(/([-()\[\]{}+?*.$\^|,:#<!\\])/g, '\\$1');
10+
const regexpEscape = s =>
11+
String(s).replace(/([-()\[\]{}+?*.$\^|,:#<!\\])/g, '\\$1');
812

9-
const loc = (location) => {
13+
const loc = location => {
1014
const b = ast.builders;
1115
return b.sourceLocation(
1216
b.position(location.first_line, location.first_column),
13-
b.position(location.last_line, location.last_column)
17+
b.position(location.last_line, location.last_column),
1418
);
1519
};
1620

17-
const formatLocation = (astnode) => {
21+
const formatLocation = astnode => {
1822
if (astnode.loc != null) {
1923
const l = astnode.loc;
2024
if (l.start.line === l.end.line && l.start.column === l.end.column) {
2125
return `line ${l.start.line}:${l.start.column}`;
2226
}
23-
return `line ${l.start.line}:${l.start.column} - line ${l.end.line}:${l.end.column}`;
27+
return `line ${l.start.line}:${l.start.column} - line ${l.end.line}:${
28+
l.end.column
29+
}`;
2430
}
2531
return '';
2632
};
2733

28-
export {
29-
capitalize,
30-
capitalizeClass,
31-
regexpEscape,
32-
loc,
33-
formatLocation,
34-
};
34+
export { capitalize, capitalizeClass, regexpEscape, loc, formatLocation };

0 commit comments

Comments
 (0)