-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathsync.command.ts
More file actions
130 lines (125 loc) · 3.9 KB
/
sync.command.ts
File metadata and controls
130 lines (125 loc) · 3.9 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
import { Listr } from 'listr2';
import { lastValueFrom, toArray } from 'rxjs';
import {
DiffResourceTask,
ExperimentalRemoteStateFileTask,
FetchPluginSchemasTask,
LintTask,
LoadLocalConfigurationTask,
LoadRemoteConfigurationTask,
} from '../tasks';
import { InitializeBackendTask } from '../tasks/init_backend';
import { SignaleRenderer } from '../utils/listr';
import { TaskContext } from './diff.command';
import {
BackendCommand,
NoLintOption,
RequestConcurrentOption,
} from './helper';
import { BackendOptions } from './typing';
import { addBackendEventListener } from './utils';
export type SyncOption = BackendOptions & {
file: Array<string>;
lint: boolean;
requestConcurrent?: number;
// experimental feature
remoteStateFile: string;
};
export const SyncCommand = new BackendCommand<SyncOption>(
'sync',
'sync the local configuration to the backend',
'Synchronize the configuration from the local file(s) to the backend.',
)
.option(
'-f, --file <file-path>',
'file to synchronize',
(filePath, files: Array<string> = []) => files.concat(filePath),
)
.addOption(NoLintOption)
.addOption(RequestConcurrentOption)
.addExamples([
{
title: 'Synchronize configuration from a single file',
command: 'adc sync -f adc.yaml',
},
{
title: 'Synchronize configuration from multiple files',
command: 'adc sync -f service-a.yaml -f service-b.yaml',
},
{
title: 'Synchronize configuration in multiple files by glob expression',
command: 'adc sync -f "**/*.yaml" -f common.yaml',
},
{
title: 'Synchronize configuration to a specific gateway group',
command: 'adc sync -f adc.yaml --gateway-group production',
},
{
title: 'Synchronize configuration without lint check',
command: 'adc sync -f adc.yaml --no-lint',
},
{
title: 'Synchronize configuration with debug logs',
command: 'adc sync -f adc.yaml --verbose 2',
},
{
title:
'Synchronize only specified resource types from the configuration file',
command:
'adc sync -f adc.yaml --include-resource-type global_rule --include-resource-type plugin_metadata',
},
{
title: 'Synchronize only the resources with the specified lables',
command: 'adc sync -f adc.yaml --label-selector app=catalog',
},
])
.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(false, false),
{
title: 'Sync configuration',
task: async (ctx, task) => {
const cancel = addBackendEventListener(ctx.backend, task);
await lastValueFrom(
ctx.backend
.sync(ctx.diff, {
exitOnFailure: true,
concurrent: opts.requestConcurrent,
})
.pipe(toArray()),
);
cancel();
},
exitOnError: true,
},
],
{
renderer: SignaleRenderer,
rendererOptions: { verbose: opts.verbose },
ctx: { remote: {}, local: {}, diff: [] },
},
);
try {
await tasks.run();
} catch (err) {
if (opts.verbose === 2) console.log(err);
process.exit(1);
}
});