generated from metcoder95/lib_template
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
cli.js
executable file
·103 lines (88 loc) · 2.18 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
#!/usr/bin/env node
'use strict'
const { pipeline } = require('node:stream')
const { readFileSync } = require('node:fs')
const { join } = require('node:path')
const minimist = require('minimist')
const CloudPine = require('./index')
function parseOptions (argv) {
const opts = {
logName: argv.name,
cloudLoggingOptions: {
sync: argv.sync,
skipInit: argv['skip-init'],
googleCloudOptions: {
projectId: argv.projectId,
keyFilename: argv.key
},
resource: {
type: argv.resource,
labels: {}
},
defaultLabels: {}
}
}
if (argv['resource-labels'] != null) {
const plainLabels = Array.isArray(argv['resource-labels'])
? argv['resource-labels']
: [argv['resource-labels']]
for (const label of plainLabels) {
const [key, value] = label.split('=')
opts.cloudLoggingOptions.resource.labels[key] = value
}
}
if (argv.labels != null) {
const plainLabels = Array.isArray(argv.labels) ? argv.labels : [argv.labels]
for (const label of plainLabels) {
const [key, value] = label.split('=')
opts.cloudLoggingOptions.defaultLabels[key] = value
}
}
return opts
}
async function handler (fgs) {
let rs, rj
const promise = new Promise(
// eslint-ignore-next-line no-return-assign
(resolve, reject) => {
rs = resolve
rj = reject
}
)
if (fgs.version) {
const { version } = JSON.parse(
readFileSync(join(__dirname, 'package.json'), 'utf-8')
)
console.log(`cloud-pine@v${version}`)
rs()
} else if (fgs.help) {
console.log(readFileSync(join(__dirname, 'help.txt'), 'utf-8'))
rs()
} else {
const options = parseOptions(fgs)
pipeline(process.stdin, await CloudPine(options), err => {
if (err) rj(err)
})
}
return promise
}
const flags = minimist(process.argv.slice(2), {
alias: {
name: 'n',
version: 'v',
help: 'h',
sync: 's',
projectId: 'p',
key: 'k',
labels: 'l',
resource: 'r',
'resource-labels': 'rs',
'skip-init': 'i'
},
boolean: ['skip-init', 'sync'],
default: {
sync: true,
'skip-init': false
}
})
handler(flags).then(null, console.error)