forked from firecow/gitlab-ci-local
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathargv.ts
More file actions
357 lines (286 loc) · 11 KB
/
Copy pathargv.ts
File metadata and controls
357 lines (286 loc) · 11 KB
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
import assert from "assert";
import fs from "fs-extra";
import * as dotenv from "dotenv";
import * as path from "path";
import camelCase from "camelcase";
import {Utils} from "./utils.js";
import {WriteStreams} from "./write-streams.js";
import chalk from "chalk";
async function isInGitRepository () {
try {
await Utils.spawn(["git", "rev-parse", "--is-inside-work-tree"]);
return true;
} catch {
return false;
}
}
async function gitRootPath () {
const {stdout} = await Utils.spawn(["git", "rev-parse", "--show-toplevel"]);
return stdout;
}
export class Argv {
static readonly default = {
"variablesFile": ".gitlab-ci-local-variables.yml",
"evaluateRuleChanges": true,
"ignoreSchemaPaths": [],
};
map: Map<string, any> = new Map<string, any>();
private readonly writeStreams: WriteStreams | undefined;
private async fallbackCwd (args: any) {
if (args.cwd !== undefined || args.file !== undefined) return;
if (fs.existsSync(`${process.cwd()}/.gitlab-ci.yml`)) return;
if (!(await isInGitRepository())) return;
this.writeStreams?.stderr(chalk`{yellow .gitlab-ci.yml not found in cwd, falling back to git root directory}\n`);
this.map.set("cwd", path.relative(process.cwd(), await gitRootPath()));
}
static async build (args: any, writeStreams?: WriteStreams) {
const argv = new Argv(args, writeStreams);
await argv.fallbackCwd(args);
argv.injectDotenv(`${argv.home}/.gitlab-ci-local/.env`, args);
argv.injectDotenv(`${argv.cwd}/.gitlab-ci-local-env`, args);
if (!argv.shellExecutorNoImage && argv.shellIsolation) {
writeStreams?.stderr(chalk`{black.bgYellowBright WARN } --shell-isolation does not work with --no-shell-executor-no-image\n`);
}
if (argv.defaultImageExplicitlySet && argv.shellIsolation) {
writeStreams?.stderr(chalk`{black.bgYellowBright WARN } --default-image does not work with --shell-isolation=true\n`);
}
if (argv.defaultImageExplicitlySet && argv.shellExecutorNoImage) {
writeStreams?.stderr(chalk`{black.bgYellowBright WARN } --default-image does not work with --shell-executor-no-image=true\n`);
}
if (argv.defaultImageExplicitlySet && argv.forceShellExecutor) {
writeStreams?.stderr(chalk`{black.bgYellowBright WARN } --default-image does not work with --force-shell-executor=true\n`);
}
return argv;
}
private constructor (argv: any, writeStreams?: WriteStreams) {
if (argv.noColor) {
chalk.level = 0;
}
this.writeStreams = writeStreams;
for (const [key, value] of Object.entries(argv)) {
this.map.set(key, value);
}
}
private injectDotenv (potentialDotenvFilepath: string, argv: any) {
if (fs.existsSync(potentialDotenvFilepath)) {
const config = dotenv.parse(fs.readFileSync(potentialDotenvFilepath));
for (const [key, value] of Object.entries(config)) {
const argKey = camelCase(key);
// Special handle KEY=VALUE variable keys
if (argKey === "variable") {
let currentVal = argv[argKey];
if (currentVal == null) {
currentVal = [];
this.map.set(argKey, currentVal);
}
if (!Array.isArray(currentVal)) {
continue;
}
for (const pair of value.split(" ")) {
currentVal.unshift(pair);
}
} else if (argv[argKey] == null) {
// Work around `dotenv.parse` limitation https://github.com/motdotla/dotenv/issues/51#issuecomment-552559070
if (value === "true") this.map.set(argKey, true);
else if (value === "false") this.map.set(argKey, false);
else if (value === "null") this.map.set(argKey, null);
else if (!isNaN(Number(value))) this.map.set(argKey, Number(value));
else this.map.set(argKey, value);
}
}
}
}
get cwd (): string {
let cwd = this.map.get("cwd") ?? ".";
assert(typeof cwd != "object", "--cwd option cannot be an array");
assert(!path.isAbsolute(cwd), "Please use relative path for the --cwd option");
cwd = path.normalize(`${process.cwd()}/${cwd}`);
cwd = cwd.replace(/\/$/, "");
assert(fs.pathExistsSync(cwd), `${cwd} is not a directory`);
return cwd;
}
get variablesFile (): string {
return this.map.get("variablesFile") ?? Argv.default.variablesFile;
}
get evaluateRuleChanges (): boolean {
return this.map.get("evaluateRuleChanges") ?? Argv.default.evaluateRuleChanges;
}
get file (): string {
return this.map.get("file") ?? ".gitlab-ci.yml";
}
get stateDir (): string {
return (this.map.get("stateDir") ?? ".gitlab-ci-local").replace(/\/$/, "");
}
get home (): string {
return (this.map.get("home") ?? process.env.HOME ?? "").replace(/\/$/, "");
}
get volume (): string[] {
const val = this.map.get("volume") ?? [];
return typeof val == "string" ? val.split(" ") : val;
}
get network (): string[] {
const val = this.map.get("network") ?? [];
return typeof val == "string" ? val.split(" ") : val;
}
get extraHost (): string[] {
const val = this.map.get("extraHost") ?? [];
return typeof val == "string" ? val.split(" ") : val;
}
get ignoreSchemaPaths (): string[] {
return this.map.get("ignoreSchemaPaths") ?? Argv.default.ignoreSchemaPaths;
}
get pullPolicy (): string {
return this.map.get("pullPolicy") ?? "if-not-present";
}
get remoteVariables (): string {
return this.map.get("remoteVariables");
}
get variable (): {[key: string]: string} {
const val = this.map.get("variable");
const variables: {[key: string]: string} = {};
const pairs = typeof val == "string" ? val.split(" ") : val;
(pairs ?? []).forEach((variablePair: string) => {
const exec = /(?<key>\w*?)(=)(?<value>(.|\n|\r)*)/.exec(variablePair);
if (exec?.groups?.key) {
variables[exec.groups.key] = exec?.groups?.value;
}
});
return variables;
}
get unsetVariables (): string[] {
return this.map.get("unsetVariable") ?? [];
}
get manual (): string[] {
const val = this.map.get("manual") ?? [];
return typeof val == "string" ? val.split(" ") : val;
}
get job (): string[] {
return this.map.get("job") ?? [];
}
get autoCompleting (): boolean {
return this.map.get("autoCompleting") ?? false;
}
get cleanup (): boolean {
return this.map.get("cleanup") ?? true;
}
get quiet (): boolean {
return this.map.get("quiet") ?? false;
}
get umask (): boolean {
// TODO: default to false in 5.x.x
return this.map.get("umask") ?? true;
}
get userns (): string | undefined {
return this.map.get("userns");
}
get privileged (): boolean {
return this.map.get("privileged") ?? false;
}
get device (): string[] {
const val = this.map.get("device") ?? [];
return typeof val == "string" ? val.split(" ") : val;
}
get ulimit (): string | null {
const ulimit = this.map.get("ulimit");
if (!ulimit) return null;
return ulimit;
}
get needs (): boolean {
return this.map.get("needs") ?? false;
}
get onlyNeeds (): boolean {
return this.map.get("onlyNeeds") ?? false;
}
get stage (): string | null {
return this.map.get("stage") ?? null;
}
get completion (): boolean {
return this.map.get("completion") ?? false;
}
get list (): boolean {
return this.map.get("list") ?? false;
}
get listAll (): boolean {
return this.map.get("listAll") ?? false;
}
get listJson (): boolean {
return this.map.get("listJson") ?? false;
}
get listCsv (): boolean {
return this.map.get("listCsv") ?? false;
}
get listCsvAll (): boolean {
return this.map.get("listCsvAll") ?? false;
}
get preview (): boolean {
return this.map.get("preview") ?? false;
}
get validateDependencyChain (): boolean {
return this.map.get("validateDependencyChain") ?? false;
}
get shellIsolation (): boolean {
// TODO: default to true in 5.x.x
return this.map.get("shellIsolation") ?? false;
}
get fetchIncludes (): boolean {
return this.map.get("fetchIncludes") ?? false;
}
get mountCache (): boolean {
return this.map.get("mountCache") ?? false;
}
get artifactsToSource (): boolean {
// TODO: default to false in 5.x.x
return this.map.get("artifactsToSource") ?? true;
}
get showTimestamps (): boolean {
return this.map.get("timestamps") ?? false;
}
get maxJobNamePadding (): number | null {
return this.map.get("maxJobNamePadding") ?? null;
}
get containerMacAddress (): string | null {
return this.map.get("containerMacAddress") ?? null;
}
get containerEmulate (): string | null {
return this.map.get("containerEmulate") ?? null;
}
get concurrency (): number | null {
const concurrency = this.map.get("concurrency");
if (!concurrency) return null;
return Number(concurrency);
}
get containerExecutable (): string {
return this.map.get("containerExecutable") ?? "docker";
}
get jsonSchemaValidation (): boolean {
return this.map.get("jsonSchemaValidation") ?? true;
}
get shellExecutorNoImage (): boolean {
// TODO: default to false in 5.x.x
return this.map.get("shellExecutorNoImage") ?? true;
}
get forceShellExecutor (): boolean {
return this.map.get("forceShellExecutor") ?? false;
}
get defaultImage (): string {
return this.map.get("defaultImage") ?? "docker.io/ruby:3.1";
}
get waitImage (): string {
return this.map.get("waitImage") ?? "docker.io/sumina46/wait-for-it:latest";
}
get helperImage (): string {
return this.map.get("helperImage") ?? "docker.io/firecow/gitlab-ci-local-util:latest";
}
get defaultImageExplicitlySet (): boolean {
return this.map.get("defaultImage") ?? false;
}
get maximumIncludes (): number {
return this.map.get("maximumIncludes") ?? 150; // https://docs.gitlab.com/ee/administration/settings/continuous_integration.html#maximum-includes
}
get childPipelineDepth (): number {
return this.map.get("childPipelineDepth");
}
get registry (): boolean {
return this.map.get("registry") ?? false;
}
}