-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathcli.js
120 lines (116 loc) · 3.15 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
116
117
118
119
120
#!/usr/bin/env node
import exhort from './index.js'
import { hideBin } from 'yargs/helpers'
import yargs from 'yargs'
import * as path from "path";
// command for component analysis take manifest type and content
const component = {
command: 'component </path/to/manifest>',
desc: 'produce component report for manifest path',
builder: yargs => yargs.positional(
'/path/to/manifest',
{
desc: 'manifest path for analyzing',
type: 'string',
normalize: true,
}
),
handler: async args => {
let manifestName = args['/path/to/manifest']
let res = await exhort.componentAnalysis(manifestName)
console.log(JSON.stringify(res, null, 2))
}
}
const validateToken = {
command: 'validate-token <token-provider> [--token-value thevalue]',
desc: 'Validates input token if authentic and authorized',
builder: yargs => yargs.positional(
'token-provider',
{
desc: 'the token provider',
type: 'string',
choices: ['snyk','oss-index'],
}
).options({
tokenValue: {
alias: 'value',
desc: 'the actual token value to be checked',
type: 'string',
}
}),
handler: async args => {
let tokenProvider = args['token-provider'].toUpperCase()
let opts={}
if(args['tokenValue'] !== undefined && args['tokenValue'].trim() !=="" ) {
let tokenValue = args['tokenValue'].trim()
opts[`EXHORT_${tokenProvider}_TOKEN`] = tokenValue
}
let res = await exhort.validateToken(opts)
console.log(res)
}
}
// command for stack analysis takes a manifest path
const stack = {
command: 'stack </path/to/manifest> [--html|--summary]',
desc: 'produce stack report for manifest path',
builder: yargs => yargs.positional(
'/path/to/manifest',
{
desc: 'manifest path for analyzing',
type: 'string',
normalize: true,
}
).options({
html: {
alias: 'r',
desc: 'Get the report as HTML instead of JSON',
type: 'boolean',
conflicts: 'summary'
},
summary: {
alias: 's',
desc: 'For JSON report, get only the \'summary\'',
type: 'boolean',
conflicts: 'html'
}
}),
handler: async args => {
let manifest = args['/path/to/manifest']
let html = args['html']
let summary = args['summary']
let theProvidersSummary = new Map();
let theProvidersObject ={}
let res = await exhort.stackAnalysis(manifest, html)
if(summary)
{
for (let provider in res.providers ) {
if (res.providers[provider].sources !== undefined) {
for(let source in res.providers[provider].sources ) {
if(res.providers[provider].sources[source].summary) {
theProvidersSummary.set(source,res.providers[provider].sources[source].summary)
}
}
}
}
for (let [provider, providerSummary] of theProvidersSummary) {
theProvidersObject[provider]=providerSummary
}
}
console.log(html ? res : JSON.stringify(
!html && summary ? theProvidersObject : res,
null,
2
))
}
}
// parse and invoke the command
yargs(hideBin(process.argv))
.usage(`Usage: ${process.argv[0].includes("node") ? path.parse(process.argv[1]).base : path.parse(process.argv[0]).base} {component|stack|validate-token}`)
.command(stack)
.command(component)
.command(validateToken)
.scriptName('')
.version(false)
.demandCommand(1)
.wrap(null)
.parse()