Skip to content

Commit

Permalink
feat(core): support resolve env var syntax in input (#153)
Browse files Browse the repository at this point in the history
  • Loading branch information
bzp2010 committed Jul 19, 2024
1 parent 53a44be commit 9515c46
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 1 deletion.
7 changes: 7 additions & 0 deletions apps/cli/src/command/diff.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
filterResourceType,
loadBackend,
mergeKVConfigurations,
recursiveReplaceEnvVars,
toConfiguration,
toKVConfiguration,
} from './utils';
Expand Down Expand Up @@ -87,6 +88,12 @@ export const LoadLocalConfigurationTask = (
ctx.local = toConfiguration(localKVConfiguration);
},
},
{
title: 'Resolve value variables',
task: async () => {
ctx.local = recursiveReplaceEnvVars(ctx.local);
},
},
{
title: 'Filter configuration resource type',
enabled: () =>
Expand Down
85 changes: 84 additions & 1 deletion apps/cli/src/command/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import * as ADCSDK from '@api7/adc-sdk';

import { fillLabels, recursiveRemoveMetadataField } from './utils';
import {
fillLabels,
recursiveRemoveMetadataField,
recursiveReplaceEnvVars,
} from './utils';

describe('CLI utils', () => {
it('should fill label selector for local resources', () => {
Expand Down Expand Up @@ -249,4 +253,83 @@ describe('CLI utils', () => {
ssls: [{ certificates: [], snis: ['test'] }],
});
});

describe('Environment Variables', () => {
it('mock config', () => {
const config: ADCSDK.Configuration = {
services: [
{
name: 'Test ${NAME}',
routes: [
{
name: 'Test ${NAME}',
uris: ['/test/${NAME}'],
},
],
},
],
consumers: [
{
username: 'TEST_${NAME}',
plugins: {
'key-auth': {
key: '${SECRET}',
},
},
},
],
ssls: [
{
snis: ['test.com'],
certificates: [
{
certificate: '${CERT}',
key: '${KEY}',
},
],
},
],
global_rules: {
// object key contains variables will not be parsed
'${GLOBAL_PLUGIN}': {
key: '${SECRET}',
},
},
plugin_metadata: {
'file-logger': {
log_format: {
note: '${NOTE}',
},
},
},
};
expect(
recursiveReplaceEnvVars(config, {
NAME: 'name',
SECRET: 'secret',
CERT: '-----',
KEY: '-----',
NOTE: 'note',
}),
).toEqual({
consumers: [
{ plugins: { 'key-auth': { key: 'secret' } }, username: 'TEST_name' },
],
global_rules: { '${GLOBAL_PLUGIN}': { key: 'secret' } },
plugin_metadata: { 'file-logger': { log_format: { note: 'note' } } },
services: [
{
name: 'Test name',
routes: [{ name: 'Test name', uris: ['/test/name'] }],
},
],
ssls: [
{
certificates: [{ certificate: '-----', key: '-----' }],
snis: ['test.com'],
},
],
});
});
});
});
26 changes: 26 additions & 0 deletions apps/cli/src/command/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { BackendAPI7 } from '@api7/adc-backend-api7';
import { BackendAPISIX } from '@api7/adc-backend-apisix';
import * as ADCSDK from '@api7/adc-sdk';
import chalk from 'chalk';
import { isObject, mapValues } from 'lodash';
import path from 'node:path';
import pluralize from 'pluralize';

Expand Down Expand Up @@ -269,6 +270,31 @@ export const recursiveRemoveMetadataField = (c: ADCSDK.Configuration) => {
});
};

export const recursiveReplaceEnvVars = (
c: ADCSDK.Configuration,
dataSource = process.env,
): ADCSDK.Configuration => {
const envVarRegex = /\$\{(\w+)\}/g;
const replaceValue = (value: unknown): unknown => {
if (typeof value === 'string')
return value.replace(
envVarRegex,
(_, envVar) => dataSource?.[envVar] || '',
);

return value;
};

const recurseReplace = (value: unknown): unknown =>
isObject(value) && !Array.isArray(value)
? mapValues(value, recurseReplace)
: Array.isArray(value)
? value.map(recurseReplace)
: replaceValue(value);

return mapValues(c, recurseReplace) as ADCSDK.Configuration;
};

export const configurePluralize = () => {
pluralize.addIrregularRule('route', 'routes');
pluralize.addIrregularRule('service', 'services');
Expand Down

0 comments on commit 9515c46

Please sign in to comment.