-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.ts
481 lines (473 loc) · 17.2 KB
/
index.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
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
#!/usr/bin/env node
/*
* Copyright © 2019 Atomist, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import * as yb from "@atomist/sdm-local/lib/cli/invocation/command/support/yargBuilder";
// tslint:disable-next-line:no-import-side-effect
import "source-map-support/register";
import * as yargs from "yargs";
import {
cliCommand,
isEmbeddedSdmCommand,
shouldAddLocalSdmCommands,
} from "./lib/command";
import { config } from "./lib/config";
import { execute } from "./lib/execute";
import { gitHook } from "./lib/gitHook";
import { gqlFetch } from "./lib/gqlFetch";
import { install } from "./lib/install";
import { kubeCrypt } from "./lib/kubeCrypt";
import { kubeEdit } from "./lib/kubeEdit";
import { kubeFetch } from "./lib/kubeFetch";
import { kubeInstall } from "./lib/kubeInstall";
import * as print from "./lib/print";
import { repositoryStart } from "./lib/repositoryStart";
import { updateSdm } from "./lib/updateSdm";
import { version } from "./lib/version";
process.env.SUPPRESS_NO_CONFIG_WARNING = "true";
if (!isEmbeddedSdmCommand(process.argv)) {
process.env.ATOMIST_DISABLE_LOGGING = "true";
}
function setupYargs(yargBuilder: yb.YargBuilder): void {
const commonOptions: { [key: string]: yb.CommandLineParameter } = {
changeDir: {
parameterName: "change-dir",
alias: "C",
default: process.cwd(),
describe: "Path to automation client project",
type: "string",
},
compile: {
parameterName: "compile",
default: true,
describe: "Run 'npm run compile' before running",
type: "boolean",
},
install: {
parameterName: "install",
describe: "Run 'npm install' before running/compiling, default is to install if no " +
"'node_modules' directory exists",
type: "boolean",
},
};
yargBuilder.withSubcommand({
command: "config",
describe: "Configure connection to Atomist",
parameters: [{
parameterName: "api-key",
describe: "Atomist API key",
type: "string",
}, {
parameterName: "workspace-id",
describe: "Atomist workspace ID",
type: "string",
}, {
parameterName: "create-api-key",
describe: "Create a new API key regardless if currently one is configured",
type: "boolean",
default: false,
}],
handler: argv => cliCommand(() => config({
apiKey: argv["api-key"],
workspaceId: argv["workspace-id"],
createApiKey: argv["create-api-key"],
})),
});
yargBuilder.withSubcommand({
command: "connect",
aliases: ["login"],
describe: "DEPRECATED use 'config'",
parameters: [{
parameterName: "api-key",
describe: "Atomist API key",
type: "string",
}, {
parameterName: "workspace-id",
describe: "Atomist workspace ID",
type: "string",
}, {
parameterName: "create-api-key",
describe: "Create a new API key regardless if currently one is configured",
type: "boolean",
default: false,
}],
handler: argv => {
deprecated(argv, "atomist config");
return cliCommand(() => config({
apiKey: argv["api-key"],
workspaceId: argv["workspace-id"],
createApiKey: argv["create-api-key"],
}));
},
});
yargBuilder.withSubcommand({
command: "execute <name>",
describe: "Run a command",
positional: [{
key: "name", opts: {
describe: "Name of command to run, command parameters PARAM=VALUE can follow",
},
}],
parameters: [commonOptions.changeDir, commonOptions.install, commonOptions.changeDir],
handler: argv => cliCommand(() => execute({
name: argv.name,
cwd: argv["change-dir"],
compile: argv.compile,
install: argv.install,
args: argv._.filter((a: any) => a !== "execute" && a !== "exec" && a !== "cmd"),
})),
});
yargBuilder.withSubcommand({
command: "install [keywords]",
describe: "DEPRECATED use 'npm install'",
positional: [{
key: "keywords",
opts: {
describe: "keywords to search for",
},
}],
parameters: [
commonOptions.changeDir,
{
parameterName: "registry",
describe: "NPM registry to search",
type: "string",
required: false,
}],
handler: argv => {
deprecated(argv, "npm install");
return cliCommand(() => install({
keywords: [argv.keywords, ...argv._.filter((a: any) => a !== "install")],
cwd: argv["change-dir"],
registry: argv.registry,
}));
},
});
yargBuilder.withSubcommand({
command: "update sdm",
describe: "Update an SDM to the latest dependency version of Atomist of a certain branch",
parameters: [
commonOptions.changeDir,
{
parameterName: "tag",
describe: "NPM tag to update the dependencies to",
type: "string",
required: false,
default: "latest",
}],
handler: argv => cliCommand(() => updateSdm({
versionTag: argv.tag,
cwd: argv["change-dir"],
})),
});
yargBuilder.withSubcommand({
command: "git-hook",
describe: "(not for human use)",
handler: argv => cliCommand(() => gitHook(process.argv)),
});
yargBuilder.withSubcommand({
command: "gql-fetch", describe: "Retrieve GraphQL schema",
parameters: [commonOptions.changeDir, commonOptions.install],
handler: argv => cliCommand(() => gqlFetch({
cwd: argv["change-dir"],
install: argv.install,
})),
});
yargBuilder.withSubcommand({
command: "kube",
aliases: ["k8s"],
describe: "DEPRECATED use 'kube-install'",
parameters: [{
parameterName: "environment",
describe: "Informative name for your Kubernetes cluster",
type: "string",
}, {
parameterName: "namespace",
describe: "Deploy utilities in namespace mode",
type: "string",
}, {
parameterName: "url",
describe: "URL of publicly accessible hostname (e.g. http://a.atomist.io)",
type: "string",
}, {
parameterName: "dry-run",
describe: "Only print the k8s objects that would be deployed, without sending them",
type: "boolean",
}, {
parameterName: "yes",
describe: "Confirm all questions with yes",
type: "boolean",
}],
handler: (argv: any) => {
deprecated(argv, "kube-install");
return cliCommand(() => kubeInstall({
env: argv.environment,
ns: argv.namespace,
dryRun: argv["dry-run"],
yes: argv.yes,
url: argv.url,
}));
},
});
yargBuilder.withSubcommand({
command: "kube-decrypt",
describe: "Decrypt encrypted Kubernetes secret data values",
parameters: [{
parameterName: "file",
describe: "Decrypt Kubernetes secret data values from secret spec file",
type: "string",
conflicts: "literal",
}, {
parameterName: "literal",
describe: "Decrypt secret data value provided as a literal string",
type: "string",
conflicts: "file",
}, {
parameterName: "secret-key",
describe: "Key to use to decrypt secret data values",
type: "string",
}, {
parameterName: "base64",
describe: "Base64 decode data after decrypting",
type: "boolean",
default: false,
}],
handler: (argv: any) => cliCommand(() => kubeCrypt({
action: "decrypt",
base64: argv.base64,
file: argv.file,
literal: argv.literal,
secretKey: argv["secret-key"],
})),
});
yargBuilder.withSubcommand({
command: "kube-encrypt",
describe: "Encrypt Base64 encoded Kubernetes secret data values",
parameters: [{
parameterName: "file",
describe: "Encrypt Kubernetes secret data values from secret spec file",
type: "string",
conflicts: "literal",
}, {
parameterName: "literal",
describe: "Encrypt secret data value provided as a literal string",
type: "string",
conflicts: "file",
}, {
parameterName: "secret-key",
describe: "Key to use to encrypt secret data values",
type: "string",
}, {
parameterName: "base64",
describe: "Base64 encode data before encrypting",
type: "boolean",
default: false,
}],
handler: (argv: any) => cliCommand(() => kubeCrypt({
action: "encrypt",
base64: argv.base64,
file: argv.file,
literal: argv.literal,
secretKey: argv["secret-key"],
})),
});
yargBuilder.withSubcommand({
command: "kube-edit <secret-spec-file>",
describe: "Decrypts a secret and opens it in an editor. Output is re-encrypted and saved back to the original file",
positional: [{
key: "secret-spec-file", opts: {
describe: "Kubernetes secret spec file",
},
}],
parameters: [{
parameterName: "secret-key",
describe: "Key used to decrypt & encrypt secret data values",
type: "string",
}],
handler: (argv: any) => cliCommand(() => kubeEdit({
file: argv["secret-spec-file"],
secretKey: argv["secret-key"],
})),
});
yargBuilder.withSubcommand({
command: "kube-fetch",
describe: "Fetch resources from a Kubernetes cluster using the currently configured Kubernetes credentials, " +
"remove system-populated properties, and save each resource specification to a file",
parameters: [{
parameterName: "options-file",
describe: "Path to file containing a JSON object defining options selecting which resources to fetch, see " +
"https://atomist.github.io/sdm-pack-k8s/interfaces/_lib_kubernetes_fetch_.kubernetesfetchoptions.html " +
"for details on the structure of the object",
type: "string",
}, {
parameterName: "output-dir",
describe: "Directory to write spec files in, if not provided current directory is used",
type: "string",
}, {
parameterName: "output-format",
describe: "File format to write spec files in, supported formats are 'json' and 'yaml', 'yaml' is the default",
type: "string",
}, {
parameterName: "secret-key",
describe: "Key to use to encrypt secret data values before writing to file",
type: "string",
}],
handler: (argv: any) => cliCommand(() => kubeFetch({
optionsFile: argv["options-file"],
outputDir: argv["output-dir"],
outputFormat: argv["output-format"],
secretKey: argv["secret-key"],
})),
});
yargBuilder.withSubcommand({
command: "kube-install",
describe: "Deploy Atomist utilities to Kubernetes a cluster",
parameters: [{
parameterName: "environment",
describe: "Informative name for your Kubernetes cluster",
type: "string",
}, {
parameterName: "namespace",
describe: "Deploy utilities in namespace mode",
type: "string",
}, {
parameterName: "url",
describe: "URL of publicly accessible hostname (e.g. http://a.atomist.io)",
type: "string",
}, {
parameterName: "dry-run",
describe: "Only print the k8s objects that would be deployed, without sending them",
type: "boolean",
}, {
parameterName: "yes",
describe: "Confirm all questions with yes",
type: "boolean",
}],
handler: (argv: any) => cliCommand(() => kubeInstall({
env: argv.environment,
ns: argv.namespace,
dryRun: argv["dry-run"],
yes: argv.yes,
url: argv.url,
})),
});
yargBuilder.withSubcommand({
command: "start",
describe: "Start an SDM or automation client",
parameters: [
commonOptions.changeDir,
commonOptions.compile,
commonOptions.install, {
parameterName: "local",
default: false,
describe: "Start SDM in local mode",
type: "boolean",
}, {
parameterName: "profile",
describe: "Name of configuration profiles to include",
type: "string",
required: false,
}, {
parameterName: "watch",
describe: "Enable watch mode",
type: "boolean",
default: false,
required: false,
}, {
parameterName: "debug",
describe: "Enable Node.js debugger",
type: "boolean",
default: false,
required: false,
}, {
parameterName: "repository-url",
describe: "Git URL to clone",
type: "string",
required: false,
}, {
parameterName: "index",
describe: "Name of the file that exports the configuration",
type: "string",
required: false,
implies: "repository-url",
conflicts: "yaml",
}, {
parameterName: "yaml",
describe: "Glob patters for yaml files to import",
type: "string",
required: false,
conflicts: "index",
}, {
parameterName: "sha",
describe: "Git sha to checkout",
type: "string",
required: false,
implies: "repository-url",
}, {
parameterName: "seed-url",
describe: "Git URL to clone the seed to overlay with SDM repository",
type: "string",
required: false,
}],
handler: (argv: any) => cliCommand(() => {
return repositoryStart({
cwd: argv["change-dir"],
cloneUrl: argv["repository-url"],
index: argv.index,
yaml: argv.yaml,
sha: argv.sha,
local: argv.local,
profile: argv.profile,
seedUrl: argv["seed-url"],
install: argv.install,
compile: argv.compile,
watch: argv.watch,
debug: argv.debug,
});
}),
});
yargBuilder.build().save(yargs);
// tslint:disable-next-line:no-unused-expression
yargs.completion("completion", false as any)
.showHelpOnFail(false, "Specify --help for available options")
.alias("help", ["h", "?"])
.version(version())
.alias("version", "v")
.describe("version", "Show version information")
.strict()
.wrap(Math.min(100, yargs.terminalWidth()))
.argv;
}
function deprecated(argv: any, alt?: string): void {
print.warn(`The '${argv._[0]}' command is DEPRECATED and will be removed in a future release.`);
if (alt) {
print.warn(`Use '${alt}' instead.`);
}
}
async function main(): Promise<any> {
const YargBuilder = yb.freshYargBuilder({ epilogForHelpMessage: "Copyright Atomist, Inc. 2019" });
if (shouldAddLocalSdmCommands(process.argv)) {
// Lazily load sdm-local to prevent early initialization
const sdmLocal = require("@atomist/sdm-local");
await sdmLocal.addLocalSdmCommands(YargBuilder);
}
setupYargs(YargBuilder);
}
main()
.catch((err: Error) => {
print.error(`Unhandled error: ${err.message}`);
print.error(err.stack);
process.exit(102);
});