11#!/usr/bin/env node
22/* eslint no-console: "off" */
3- const os = require ( 'os' ) ;
4- const nomnom = require ( 'nomnom' ) ;
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
11- const getCommandlineOptions = ( ) => {
12- const opts = nomnom . script ( 'find-nodes' )
13- . option ( 'host' , {
14- abbr : 'H' ,
15- default : 'puppetdb' ,
16- metavar : 'HOST' ,
17- help : 'PuppetDB host' ,
18- } )
19- . option ( 'port' , {
20- abbr : 'p' ,
21- default : 8080 ,
22- metavar : 'PORT' ,
23- help : 'PuppetDB port' ,
24- callback ( port ) {
25- return parseInt ( port , 10 ) ;
26- } ,
27- } )
28- . option ( 'ssl' , {
29- abbr : 's' ,
30- flag : true ,
31- default : false ,
32- help : 'Use SSL' ,
33- } )
34- . option ( 'key' , {
35- default : `/etc/puppet/ssl/private_keys/${ os . hostname ( ) } .pem` ,
36- metavar : 'KEY' ,
37- help : 'Private SSL key file' ,
38- } )
39- . option ( 'cert' , {
40- default : `/etc/puppet/ssl/certs/${ os . hostname ( ) } .pem` ,
41- metavar : 'CERT' ,
42- help : 'SSL certificate file' ,
43- } )
44- . option ( 'ca' , {
45- default : '/etc/puppet/ssl/ca.pem' ,
46- metavar : 'CACERT' ,
47- help : 'SSL CA certificate file' ,
48- } )
49- . option ( 'version' , {
50- abbr : 'v' ,
51- flag : true ,
52- help : 'print version and exit' ,
53- callback ( ) {
54- return process . env . npm_package_version ;
55- } ,
56- } )
57- . option ( 'print' , {
58- abbr : 'P' ,
59- flag : true ,
60- default : false ,
61- help : 'Print parsed query and exit' ,
62- } )
63- . option ( 'query' , {
64- flag : true ,
65- required : true ,
66- position : 0 ,
67- help : 'query string' ,
68- } )
69- . parse ( ) ;
70- return opts ;
71- } ;
11+ 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" )
16+ . option (
17+ "--key <keyfile>" ,
18+ "Private SSL key file" ,
19+ `/etc/puppet/ssl/private_keys/${ os . hostname ( ) } .pem`
20+ )
21+ . option (
22+ "--cert <certfile>" ,
23+ "SSL certificate file" ,
24+ `/etc/puppet/ssl/certs/${ os . hostname ( ) } .pem`
25+ )
26+ . option ( "--ca <cafile>" , "SSL CA certificate file" , "/etc/puppet/ssl/ca.pem" )
27+ . option ( "-P, --print" , "Print parsed query and exit" )
28+ . parse ( process . argv ) ;
7229
73- const opts = getCommandlineOptions ( ) ;
7430let query ;
7531try {
76- query = JSON . stringify ( puppetdbquery . parse ( opts . query ) ) ;
32+ query = JSON . stringify ( puppetdbquery . parse ( opts . args [ 0 ] ) ) ;
7733} catch ( err ) {
34+ console . log ( opts ) ;
7835 console . log ( err ) ;
7936}
8037
@@ -88,8 +45,8 @@ if (opts.print) {
8845 port : opts . port ,
8946 path : `/pdb/query/v4/nodes?${ querystring . stringify ( { query } ) } ` ,
9047 headers : {
91- Accept : ' application/json' ,
92- } ,
48+ Accept : " application/json"
49+ }
9350 } ;
9451 let httplib ;
9552 if ( opts . ssl ) {
@@ -102,14 +59,19 @@ if (opts.print) {
10259 }
10360
10461 // We have the full response, parse it and print the node names
105- httplib . get ( options , ( res ) => {
106- let data = '' ;
107- res . on ( 'data' , ( chunk ) => { data += chunk ; } ) ;
62+ httplib
63+ . get ( options , res => {
64+ let data = "" ;
65+ res . on ( "data" , chunk => {
66+ data += chunk ;
67+ } ) ;
10868
109- return res . on ( ' end' , ( ) =>
110- JSON . parse ( data ) . forEach ( node => console . log ( node . certname ) )
111- ) ;
112- } )
69+ return res . on ( " end" , ( ) =>
70+ JSON . parse ( data ) . forEach ( node => console . log ( node . certname ) )
71+ ) ;
72+ } )
11373
114- . on ( 'error' , e => 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 } ` )
76+ ) ;
11577}
0 commit comments