-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathdiff.command.ts
More file actions
108 lines (101 loc) · 3.18 KB
/
diff.command.ts
File metadata and controls
108 lines (101 loc) · 3.18 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
import * as ADCSDK from '@api7/adc-sdk';
import YAML from 'js-yaml';
import { Listr } from 'listr2';
import { writeFile } from 'node:fs/promises';
import {
DiffResourceTask,
ExperimentalRemoteStateFileTask,
FetchPluginSchemasTask,
LintTask,
LoadLocalConfigurationTask,
} from '../tasks';
import { LoadRemoteConfigurationTask } from '../tasks';
import { InitializeBackendTask } from '../tasks/init_backend';
import { SignaleRenderer } from '../utils/listr';
import { BackendCommand, NoLintOption } from './helper';
import { BackendOptions } from './typing';
type DiffOptions = BackendOptions & {
file: Array<string>;
lint: boolean;
// experimental feature
remoteStateFile: string;
};
export interface TaskContext {
backend?: ADCSDK.Backend;
local?: ADCSDK.Configuration;
remote?: ADCSDK.Configuration;
diff?: ADCSDK.Event[];
//defaultValue?: ADCSDK.DefaultValue;
}
export const DiffCommand = new BackendCommand<DiffOptions>(
'diff',
'show differences between the local and the backend configurations',
'Compare the configuration in the specified file(s) with the backend configuration',
)
.option(
'-f, --file <file-path>',
'file to compare',
(filePath, files: Array<string> = []) => files.concat(filePath),
)
.addOption(NoLintOption)
.addExamples([
{
title:
'Compare configuration in a specified file with the backend configuration',
command: 'adc diff -f adc.yaml',
},
{
title:
'Compare configuration in multiple specified files with the backend configuration',
command: 'adc diff -f service-a.yaml -f service-b.yaml',
},
{
title:
'Compare configuration in multiple specified files by glob expression',
command: 'adc diff -f "**/*.yaml" -f common.yaml',
},
])
.handle(async (opts) => {
const tasks = new Listr<TaskContext, typeof SignaleRenderer>(
[
InitializeBackendTask(opts.backend, opts),
opts.lint ? FetchPluginSchemasTask() : { task: () => undefined },
LoadLocalConfigurationTask(
opts.file,
opts.labelSelector,
opts.includeResourceType,
opts.excludeResourceType,
),
opts.lint ? LintTask() : { task: () => undefined },
!opts.remoteStateFile
? LoadRemoteConfigurationTask({
labelSelector: opts.labelSelector,
includeResourceType: opts.includeResourceType,
excludeResourceType: opts.excludeResourceType,
})
: ExperimentalRemoteStateFileTask(opts.remoteStateFile),
DiffResourceTask(true, true),
{
title: 'Write detail diff result to file',
task: async (ctx, task) => {
await writeFile(
'./diff.yaml',
YAML.dump(ctx.diff, { noRefs: true }),
{ encoding: 'utf-8' },
);
task.output = 'Detail diff result has been wrote to diff.yaml';
},
},
],
{
renderer: SignaleRenderer,
rendererOptions: { verbose: opts.verbose },
ctx: { remote: {}, local: {}, diff: [] },
},
);
try {
await tasks.run();
} catch (err) {
process.exit(1);
}
});