-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathrun-options.ts
192 lines (176 loc) · 4.83 KB
/
run-options.ts
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
// SPDX-FileCopyrightText: 2023 Friedrich-Alexander-Universitat Erlangen-Nurnberg
//
// SPDX-License-Identifier: AGPL-3.0-only
import {
type DebugGranularity,
DebugGranularityValues,
type DebugTargets,
DefaultDebugTargetsValue,
type Logger,
isDebugGranularity,
} from '@jvalue/jayvee-execution';
export interface RunOptions {
pipeline: string;
env: Map<string, string>;
debug: boolean;
debugGranularity: DebugGranularity;
debugTarget: DebugTargets;
parseOnly: boolean;
}
export function parseRunOptions(
optionsRaw: unknown,
logger: Logger,
): RunOptions | undefined {
if (typeof optionsRaw !== 'object' || optionsRaw == null) {
logger.logErr(
"Error in the interpreter: didn't receive a valid RunOptions object.",
);
return undefined;
}
const requiredFields = [
'pipeline',
'env',
'debug',
'debugGranularity',
'debugTarget',
'parseOnly',
];
if (requiredFields.some((f) => !(f in optionsRaw))) {
logger.logErr(
`Error in the interpreter: didn't receive a valid RunOptions object. Must have the fields ${requiredFields
.map((f) => `"${f}"`)
.join(', ')} but got object ${JSON.stringify(optionsRaw, null, 2)}`,
);
return undefined;
}
const options = optionsRaw as Record<keyof RunOptions, unknown>;
if (
!isPipelineArgument(options.pipeline, logger) ||
!isEnvArgument(options.env, logger) ||
!isDebugArgument(options.debug, logger) ||
!isDebugGranularityArgument(options.debugGranularity, logger) ||
!isDebugTargetArgument(options.debugTarget, logger) ||
!isParseOnlyArgument(options.parseOnly, logger)
) {
return undefined;
}
return {
pipeline: options.pipeline,
env: options.env,
debug: options.debug === true || options.debug === 'true',
debugGranularity: options.debugGranularity,
debugTarget: getDebugTargets(options.debugTarget),
parseOnly: options.parseOnly === true || options.parseOnly === 'true',
};
}
function getDebugTargets(debugTargetsString: string): DebugTargets {
const areAllBlocksTargeted = debugTargetsString === DefaultDebugTargetsValue;
if (areAllBlocksTargeted) {
return DefaultDebugTargetsValue;
}
return debugTargetsString.split(',').map((target) => target.trim());
}
function isPipelineArgument(arg: unknown, logger: Logger): arg is string {
if (typeof arg !== 'string') {
logger.logErr(
`Invalid value "${JSON.stringify(
arg,
)}" for pipeline selection option: -p --pipeline.\n` +
'Must be a string value.',
);
return false;
}
return true;
}
function isDebugGranularityArgument(
arg: unknown,
logger: Logger,
): arg is DebugGranularity {
if (!isDebugGranularity(arg)) {
logger.logErr(
`Invalid value "${JSON.stringify(
arg,
)}" for debug granularity option: -dg --debug-granularity.\n` +
`Must be one of the following values: ${DebugGranularityValues.join(
', ',
)}.`,
);
return false;
}
return true;
}
function isDebugTargetArgument(arg: unknown, logger: Logger): arg is string {
// options.debugTarget
if (typeof arg !== 'string') {
logger.logErr(
`Invalid value "${JSON.stringify(
arg,
)}" for debug target option: -dt --debug-target.\n` +
'Must be a string value.',
);
return false;
}
return true;
}
function isDebugArgument(
arg: unknown,
logger: Logger,
): arg is boolean | 'true' | 'false' {
if (typeof arg !== 'boolean' && arg !== 'true' && arg !== 'false') {
logger.logErr(
`Invalid value "${JSON.stringify(arg)}" for debug option: -d --debug.\n` +
'Must be true or false.',
);
return false;
}
return true;
}
function isParseOnlyArgument(
arg: unknown,
logger: Logger,
): arg is boolean | 'true' | 'false' {
if (typeof arg !== 'boolean' && arg !== 'true' && arg !== 'false') {
logger.logErr(
`Invalid value "${JSON.stringify(
arg,
)}" for parse-only option: -po --parse-only.\n` +
'Must be true or false.',
);
return false;
}
return true;
}
function isEnvArgument(
arg: unknown,
logger: Logger,
): arg is Map<string, string> {
if (
!(
arg instanceof Map &&
[...arg.entries()].every(
([key, value]) => typeof key === 'string' && typeof value === 'string',
)
)
) {
logger.logErr(
`Invalid value "${JSON.stringify(arg)}" for env option: -e --env.\n` +
'Must be map from string keys to string values.',
);
return false;
}
return true;
}
export function parsePipelineMatcherRegExp(
matcher: string,
logger: Logger,
): RegExp | undefined {
try {
return new RegExp(matcher);
} catch (e: unknown) {
logger.logErr(
`Invalid value "${matcher}" for pipeline selection option: -p --pipeline.\n` +
'Must be a valid regular expression.',
);
return undefined;
}
}