-
Notifications
You must be signed in to change notification settings - Fork 67
/
cli.js
executable file
·115 lines (100 loc) · 2.5 KB
/
cli.js
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
#! /usr/bin/env node
'use strict'
const minimist = require('minimist')
const pump = require('pump')
const fs = require('fs')
const path = require('path')
const pinoElasticSearch = require('./lib')
function start (opts) {
if (opts.help) {
console.log(fs.readFileSync(path.join(__dirname, './usage.txt'), 'utf8'))
return
}
if (opts.version) {
console.log('pino-elasticsearch', require('./package.json').version)
return
}
if (opts.username && opts.password) {
opts.auth = {
username: opts.username,
password: opts.password
}
}
if (opts['api-key']) {
opts.auth = { apiKey: opts['api-key'] }
}
if (opts.cloud) {
opts.cloud = { id: opts.cloud }
}
if (opts.rejectUnauthorized) {
opts.rejectUnauthorized = opts.rejectUnauthorized !== 'false'
}
const stream = pinoElasticSearch(opts)
stream.on('unknown', (line, error) => {
console.error('Elasticsearch client json error in line:\n' + line + '\nError:', error)
})
stream.on('error', (error) => {
console.error('Elasticsearch client error:', error)
})
stream.on('insertError', (error) => {
console.error('Elasticsearch server error:', error)
})
pump(process.stdin, stream)
}
function startCli (flags) {
const allowedProps = [
'node',
'index',
'flush-bytes',
'flush-interval',
'trace-level',
'username',
'password',
'api-key',
'cloud',
'es-version',
'rejectUnauthorized',
'opType'
]
if (flags['read-config']) {
if (flags['read-config'].match(/.*\.json$/) !== null) {
const config = JSON.parse(fs.readFileSync(path.join(process.cwd(), flags['read-config']), 'utf-8'))
allowedProps.forEach(key => {
if (config[key] !== undefined) {
flags[key] = config[key]
}
})
}
if (flags['read-config'].match(/.*\.js$/) !== null) {
const config = require(path.join(process.cwd(), flags['read-config']))
allowedProps.forEach(key => {
if (config[key] !== undefined) {
flags[key] = config[key]
}
})
}
}
start(flags)
}
if (require.main === module) {
startCli(minimist(process.argv.slice(2), {
alias: {
version: 'v',
help: 'h',
node: 'n',
index: 'i',
'flush-bytes': 'f',
'flush-interval': 't',
'trace-level': 'l',
username: 'u',
password: 'p',
'api-key': 'k',
cloud: 'c',
'read-config': 'r'
},
default: {
node: 'http://localhost:9200'
}
}))
}
module.exports = startCli