Removed dependency, introduced isValidUrl, and reinstated configPaths

Signed-off-by: Matto <muhamadto@gmail.com>
This commit is contained in:
Matto
2021-10-27 20:33:33 +11:00
parent 8e85a0bca0
commit 1cecd737f9
14 changed files with 247 additions and 39 deletions
+1
View File
@@ -43,6 +43,7 @@ export function loadConfig(options: LoadConfigOptions): Promise<AppConfig[]>;
// @public
export type LoadConfigOptions = {
configRoot: string;
configPaths: string[];
configTargets: ConfigTarget[];
env?: string;
experimentalEnvFunc?: (name: string) => Promise<string | undefined>;
-1
View File
@@ -30,7 +30,6 @@
"clean": "backstage-cli clean"
},
"dependencies": {
"@backstage/integration": "^0.6.5",
"@backstage/cli-common": "^0.1.4",
"@backstage/config": "^0.1.9",
"@backstage/errors": "^0.1.3",
+1
View File
@@ -17,3 +17,4 @@
export { readEnvConfig } from './env';
export * from './transform';
export * from './schema';
export { isValidUrl } from './urls';
@@ -0,0 +1,34 @@
/*
* Copyright 2021 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { isValidUrl } from './urls';
describe('isValidUrl', () => {
it('should return true for url', () => {
const validUrl = isValidUrl('http://some.valid.url');
expect(validUrl).toBe(true);
});
it('should return false for absolute path', () => {
const validUrl = isValidUrl('/some/absolute/path');
expect(validUrl).toBe(false);
});
it('should return false for relative path', () => {
const validUrl = isValidUrl('../some/relative/path');
expect(validUrl).toBe(false);
});
});
+25
View File
@@ -0,0 +1,25 @@
/*
* Copyright 2021 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
export function isValidUrl(url: string): boolean {
try {
// eslint-disable-next-line no-new
new URL(url);
return true;
} catch {
return false;
}
}
+48 -1
View File
@@ -67,6 +67,13 @@ describe('loadConfig', () => {
$file: secrets/session-key.txt
escaped: \$\${Escaped}
`,
'/root/app-config2.yaml': `
app:
title: Example App 2
sessionKey:
$file: secrets/session-key.txt
escaped: \$\${Escaped}
`,
'/root/app-config.development.yaml': `
app:
sessionKey: development-key
@@ -111,6 +118,7 @@ describe('loadConfig', () => {
await expect(
loadConfig({
configRoot: '/root',
configPaths: [],
configTargets: [],
env: 'production',
}),
@@ -136,6 +144,7 @@ describe('loadConfig', () => {
await expect(
loadConfig({
configRoot: '/root',
configPaths: [],
configTargets: [{ url: configUrl }],
env: 'production',
remote: {
@@ -156,10 +165,43 @@ describe('loadConfig', () => {
]);
});
it('loads config with secrets', async () => {
it('loads config with secrets from two different files', async () => {
await expect(
loadConfig({
configRoot: '/root',
configPaths: ['/root/app-config2.yaml'],
configTargets: [{ path: '/root/app-config.yaml' }],
env: 'production',
}),
).resolves.toEqual([
{
context: 'app-config.yaml',
data: {
app: {
title: 'Example App',
sessionKey: 'abc123',
escaped: '${Escaped}',
},
},
},
{
context: 'app-config2.yaml',
data: {
app: {
title: 'Example App 2',
sessionKey: 'abc123',
escaped: '${Escaped}',
},
},
},
]);
});
it('loads config with secrets from single file', async () => {
await expect(
loadConfig({
configRoot: '/root',
configPaths: ['/root/app-config.yaml'],
configTargets: [{ path: '/root/app-config.yaml' }],
env: 'production',
}),
@@ -181,6 +223,7 @@ describe('loadConfig', () => {
await expect(
loadConfig({
configRoot: '/root',
configPaths: [],
configTargets: [
{ path: '/root/app-config.yaml' },
{ path: '/root/app-config.development.yaml' },
@@ -221,6 +264,7 @@ describe('loadConfig', () => {
await expect(
loadConfig({
configRoot: '/root',
configPaths: [],
configTargets: [{ path: '/root/app-config.substitute.yaml' }],
env: 'development',
}),
@@ -246,6 +290,7 @@ describe('loadConfig', () => {
await expect(
loadConfig({
configRoot: '/root',
configPaths: [],
configTargets: [],
watch: {
onChange: onChange.resolve,
@@ -294,6 +339,7 @@ describe('loadConfig', () => {
await expect(
loadConfig({
configRoot: '/root',
configPaths: [],
configTargets: [{ url: configUrl }],
watch: {
onChange: onChange.resolve,
@@ -339,6 +385,7 @@ describe('loadConfig', () => {
await loadConfig({
configRoot: '/root',
configPaths: [],
configTargets: [],
watch: {
onChange: () => {
+13 -1
View File
@@ -25,10 +25,10 @@ import {
createIncludeTransform,
createSubstitutionTransform,
EnvFunc,
isValidUrl,
readEnvConfig,
} from './lib';
import fetch from 'node-fetch';
import { isValidUrl } from '@backstage/integration';
export type ConfigTarget = { path: string } | { url: string };
@@ -83,6 +83,11 @@ export type LoadConfigOptions = {
// The root directory of the config loading context. Used to find default configs.
configRoot: string;
/** Absolute paths to load config files from. Configs from earlier paths have lower priority.
* @deprecated Use {@link configTargets} instead.
*/
configPaths: string[];
// Paths to load config files from. Configs from earlier paths have lower priority.
configTargets: ConfigTarget[];
@@ -122,6 +127,13 @@ export async function loadConfig(
.filter((e): e is { path: string } => e.hasOwnProperty('path'))
.map(configTarget => configTarget.path);
// Append deprecated configPaths to the absolute config paths received via configTargets.
options.configPaths.forEach(cp => {
if (!configPaths.includes(cp)) {
configPaths.push(cp);
}
});
const configUrls: string[] = options.configTargets
.slice()
.filter((e): e is { url: string } => e.hasOwnProperty('url'))