|
| 1 | +import { Listr } from 'listr2'; |
| 2 | + |
| 3 | +import { |
| 4 | + DiffResourceTask, |
| 5 | + LintTask, |
| 6 | + LoadLocalConfigurationTask, |
| 7 | + ValidateTask, |
| 8 | +} from '../tasks'; |
| 9 | +import { InitializeBackendTask } from '../tasks/init_backend'; |
| 10 | +import { SignaleRenderer } from '../utils/listr'; |
| 11 | +import { TaskContext } from './diff.command'; |
| 12 | +import { BackendCommand, NoLintOption } from './helper'; |
| 13 | +import { BackendOptions } from './typing'; |
| 14 | + |
| 15 | +export type ValidateOptions = BackendOptions & { |
| 16 | + file: Array<string>; |
| 17 | + lint: boolean; |
| 18 | +}; |
| 19 | + |
| 20 | +export const ValidateCommand = new BackendCommand<ValidateOptions>( |
| 21 | + 'validate', |
| 22 | + 'validate the local configuration against the backend', |
| 23 | + 'Validate the configuration from the local file(s) against the backend without applying any changes.', |
| 24 | +) |
| 25 | + .option( |
| 26 | + '-f, --file <file-path>', |
| 27 | + 'file to validate', |
| 28 | + (filePath, files: Array<string> = []) => files.concat(filePath), |
| 29 | + ) |
| 30 | + .addOption(NoLintOption) |
| 31 | + .addExamples([ |
| 32 | + { |
| 33 | + title: 'Validate configuration from a single file', |
| 34 | + command: 'adc validate -f adc.yaml', |
| 35 | + }, |
| 36 | + { |
| 37 | + title: 'Validate configuration from multiple files', |
| 38 | + command: 'adc validate -f service-a.yaml -f service-b.yaml', |
| 39 | + }, |
| 40 | + { |
| 41 | + title: 'Validate configuration against API7 EE backend', |
| 42 | + command: |
| 43 | + 'adc validate -f adc.yaml --backend api7ee --gateway-group default', |
| 44 | + }, |
| 45 | + { |
| 46 | + title: 'Validate configuration without lint check', |
| 47 | + command: 'adc validate -f adc.yaml --no-lint', |
| 48 | + }, |
| 49 | + ]) |
| 50 | + .handle(async (opts) => { |
| 51 | + const tasks = new Listr<TaskContext, typeof SignaleRenderer>( |
| 52 | + [ |
| 53 | + InitializeBackendTask(opts.backend, opts), |
| 54 | + LoadLocalConfigurationTask( |
| 55 | + opts.file, |
| 56 | + opts.labelSelector, |
| 57 | + opts.includeResourceType, |
| 58 | + opts.excludeResourceType, |
| 59 | + ), |
| 60 | + opts.lint ? LintTask() : { task: () => undefined }, |
| 61 | + DiffResourceTask(), |
| 62 | + ValidateTask(), |
| 63 | + ], |
| 64 | + { |
| 65 | + renderer: SignaleRenderer, |
| 66 | + rendererOptions: { verbose: opts.verbose }, |
| 67 | + ctx: { remote: {}, local: {}, diff: [] }, |
| 68 | + }, |
| 69 | + ); |
| 70 | + |
| 71 | + try { |
| 72 | + await tasks.run(); |
| 73 | + } catch (err) { |
| 74 | + if (opts.verbose === 2) console.log(err); |
| 75 | + process.exit(1); |
| 76 | + } |
| 77 | + }); |
0 commit comments