-
Notifications
You must be signed in to change notification settings - Fork 65
/
cli.js
executable file
·246 lines (237 loc) · 6.85 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#!/usr/bin/env node
/**
* Main CLI entry point
*
* @remarks
* As tempting as it may be to try to move stuff out of here, it _will_ break
* type inference, and you'll need to sort that out yourself.
*
* Regarding middleware: Any option which _does not_ either a) need other
* options for postprocessing, or b) need to be asynchronous--should use
* {@link yargs.coerce} instead.
* @packageDocumentation
*/
import assert from 'node:assert'
import path from 'node:path'
import yargs from 'yargs'
import { hideBin } from 'yargs/helpers'
import {
constants,
generateAndWritePolicy,
loadPolicies,
run,
} from './index.js'
import { readJsonFile } from './util.js'
const BEHAVIOR_GROUP = 'Behavior Options:'
/**
* Main entry point to CLI
*/
async function main(args = hideBin(process.argv)) {
// see note below above the call to `.version()`
const { version } = /** @type {import('type-fest').PackageJson} */ (
await readJsonFile(new URL('../package.json', import.meta.url))
)
yargs(args)
.scriptName('endomoat')
/**
* @remarks
* `yargs` seems to return the version provided by the `package.json` at the
* workspace root--not the `package.json` in this project. This is
* _probably_ not an issue anywhere other than in a dev environment, but I
* wanted to make sure.
*/
.version(`${version}`)
.options({
policy: {
alias: ['p'],
describe: 'Filepath to a policy file',
type: 'string',
normalize: true,
default: constants.DEFAULT_POLICY_PATH,
nargs: 1,
requiresArg: true,
global: true,
},
'policy-override': {
alias: ['o', 'override'],
describe: 'Filepath to a policy override file',
type: 'string',
normalize: true,
default: constants.DEFAULT_POLICY_OVERRIDE_PATH,
nargs: 1,
requiresArg: true,
global: true,
},
'policy-debug': {
alias: ['pd'],
describe: 'Filepath to a policy debug file',
default: constants.DEFAULT_POLICY_DEBUG_PATH,
nargs: 1,
type: 'string',
requiresArg: true,
global: true,
},
cwd: {
describe: 'Path to application root directory',
type: 'string',
nargs: 1,
requiresArg: true,
normalize: true,
default: process.cwd(),
defaultDescription: '(current directory)',
coerce: path.resolve,
global: true,
},
})
/**
* This resolves all paths from `cwd`.
*
* @remarks
* This runs _before_ validation.
*
* Note that this does _not_ change the properties named via aliases or
* camelcasing! Don't use them!!
*/
.middleware((argv) => {
argv.policy = path.resolve(argv.cwd, argv.policy)
argv['policy-override'] = path.resolve(argv.cwd, argv['policy-override'])
argv['policy-debug'] = path.resolve(argv.cwd, argv['policy-debug'])
}, true)
/**
* This should not fail. If it does, there is a bug.
*/
.check((argv) => {
assert(path.isAbsolute(argv.cwd), 'cwd must be an absolute path')
assert(path.isAbsolute(argv.policy), 'policy must be an absolute path')
assert(
path.isAbsolute(argv['policy-override']),
'policy-override must be an absolute path'
)
assert(
path.isAbsolute(argv['policy-debug']),
'policy-debug must be an absolute path'
)
return true
})
/**
* Default command (no command)
*/
.command(
['$0 <entrypoint>', 'run <entrypoint>'],
'Run an application',
(yargs) =>
yargs
.positional('entrypoint', {
describe: 'Path to the application entry point',
type: 'string',
normalize: true,
})
.demandOption('entrypoint')
/**
* Resolve entrypoint from `cwd`
*/
.middleware((argv) => {
argv.entrypoint = path.resolve(argv.cwd, argv.entrypoint)
}, true)
/**
* This should not fail. If it does, there is a bug.
*/
.check((argv) => {
assert(
path.isAbsolute(argv.entrypoint),
'entrypoint must be an absolute path'
)
return true
}),
/**
* Default command handler.
*
* @remarks
* (That's "handler for the default command"--_not_ "default handler for a
* command").
*/
async (argv) => {
await Promise.resolve()
/** @type {import('lavamoat-core').LavaMoatPolicy} */
let policy
try {
policy = await loadPolicies(argv.policy, argv['policy-override'])
} catch (e) {
const err = /** @type {NodeJS.ErrnoException} */ (e)
if (err.code === 'ENOENT') {
console.error(
`Could not load policy from ${argv.policy}: ${err.message}`
)
process.exitCode = 1
return
}
throw err
}
await run(argv.entrypoint, policy)
}
)
.command(
['gen <entrypoint>', 'generate <entrypoint>'],
'Generate policy files; overwrites existing policies',
(yargs) =>
yargs
.options({
run: {
describe: 'Run the application after policy generated',
type: 'boolean',
group: BEHAVIOR_GROUP,
},
debug: {
type: 'boolean',
describe: 'Additionally write a debug policy',
group: BEHAVIOR_GROUP,
},
})
.positional('entrypoint', {
describe: 'Path to the application entry point',
type: 'string',
normalize: true,
coerce: path.resolve,
})
.demandOption('entrypoint')
/**
* Resolve entrypoint from `cwd`
*/
.middleware((argv) => {
argv.entrypoint = path.resolve(argv.cwd, argv.entrypoint)
}, true)
/**
* This should not fail. If it does, there is a bug.
*/
.check((argv) => {
assert(
path.isAbsolute(argv.entrypoint),
'entrypoint must be an absolute path'
)
return true
}),
async ({
entrypoint,
debug,
run: shouldRun,
policy: policyPath,
'policy-debug': policyDebugPath,
}) => {
const policy = await generateAndWritePolicy(entrypoint, {
debug,
policyPath,
policyDebugPath,
})
if (debug) {
console.error(`Wrote debug policy to ${policyDebugPath}`)
}
console.error(`Wrote policy to ${policyPath}`)
if (shouldRun) {
await run(entrypoint, policy)
}
}
)
.demandCommand(1)
.parse()
}
main()