Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core): support glob file path #212

Merged
merged 1 commit into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 32 additions & 21 deletions apps/cli/src/command/diff.command.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as ADCSDK from '@api7/adc-sdk';
import { globSync } from 'glob';
import { Listr, ListrTask } from 'listr2';
import { existsSync } from 'node:fs';
import { readFile, writeFile } from 'node:fs/promises';
Expand Down Expand Up @@ -43,11 +44,10 @@ export const LoadLocalConfigurationTask = (
}> => ({
title: `Load local configuration`,
task: async (ctx, task) => {
if (!files || files.length <= 0) {
task.output =
'No configuration file input\nPlease specify the declarative configuration file to use with -f or --file';
throw new Error();
}
if (!files || files.length <= 0)
throw new Error(
'No configuration file input, please specify the declarative configuration file to use with -f or --file',
);

interface LoadLocalContext {
configurations: Record<string, ADCSDK.Configuration>;
Expand All @@ -58,21 +58,25 @@ export const LoadLocalConfigurationTask = (
return task.newListr<LoadLocalContext>(
[
// load yaml files
...files.map((filePath): ListrTask<LoadLocalContext> => {
return {
title: `Load ${path.resolve(filePath)}`,
task: async (subCtx) => {
if (!existsSync(filePath))
throw new Error(
`File ${path.resolve(filePath)} does not exist`,
);
const fileContent =
(await readFile(filePath, { encoding: 'utf-8' })) ?? '';
...files
.flatMap((filePath) =>
globSync(filePath, { nodir: true, absolute: true }),
)
.map((filePath): ListrTask<LoadLocalContext> => {
return {
title: `Load ${filePath}`,
task: async (subCtx) => {
if (!existsSync(filePath))
throw new Error(
`File ${path.resolve(filePath)} does not exist`,
);
const fileContent =
(await readFile(filePath, { encoding: 'utf-8' })) ?? '';

subCtx.configurations[filePath] = YAML.parse(fileContent) ?? {};
},
};
}),
subCtx.configurations[filePath] = YAML.parse(fileContent) ?? {};
},
};
}),
// merge yaml files
{
title: 'Merge local configurations',
Expand Down Expand Up @@ -169,13 +173,20 @@ export const DiffCommand = new BackendCommand<DiffOptions>(
.addOption(NoLintOption)
.addExamples([
{
title: 'Compare configuration in a specified file with the backend configuration',
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',
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 backend = loadBackend(opts.backend, opts);
Expand Down
4 changes: 4 additions & 0 deletions apps/cli/src/command/sync.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ export const SyncCommand = new BackendCommand<SyncOption>(
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 diff -f "**/*.yaml" -f common.yaml',
},
{
title: 'Synchronize configuration to a specific gateway group',
command: 'adc sync -f adc.yaml --gateway-group production',
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"commander": "^12.1.0",
"deep-diff": "^1.0.2",
"dotenv": "^16.4.5",
"glob": "^11.0.0",
"immer": "^10.1.1",
"json-schema": "^0.4.0",
"listr2": "^8.2.1",
Expand Down
Loading