Skip to content

Commit

Permalink
fix(cli): improve example configuration display
Browse files Browse the repository at this point in the history
This change changes the CLI package-dynamic-plugins command to take
advantage of some dynamic plugin projects that have followed the
convention of maintaining a app-config.janus-idp.yaml in the root of the
plugin source directory.  When this file is present, it will be read in
as the command processes dynamic plugins in the monorepo and then
included in the example dynamic-plugins.yaml configuration printed out
when the command successfully completes.  This change also adjusts the
control flow where this is printed out to ensure it's after successful
completion of the container image.

Signed-off-by: Stan Lewis <gashcrumb@gmail.com>
  • Loading branch information
gashcrumb committed Nov 4, 2024
1 parent 661173a commit 924d8cd
Show file tree
Hide file tree
Showing 4 changed files with 1,423 additions and 1,276 deletions.
15 changes: 15 additions & 0 deletions .changeset/eighty-tips-learn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
"@janus-idp/cli": patch
---

fix(cli): Improve example configuration display

This change changes the CLI package-dynamic-plugins command to take
advantage of some dynamic plugin projects that have followed the
convention of maintaining a app-config.janus-idp.yaml in the root of the
plugin source directory. When this file is present, it will be read in
as the command processes dynamic plugins in the monorepo and then
included in the example dynamic-plugins.yaml configuration printed out
when the command successfully completes. This change also adjusts the
control flow where this is printed out to ensure it's after successful
completion of the container image.
1 change: 1 addition & 0 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
"typescript-json-schema": "^0.64.0",
"webpack": "^5.89.0",
"webpack-dev-server": "^4.15.1",
"yaml": "^2.5.1",
"yml-loader": "^2.1.0",
"yn": "^4.0.0"
},
Expand Down
91 changes: 75 additions & 16 deletions packages/cli/src/commands/package-dynamic-plugins/command.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { PackageRoles } from '@backstage/cli-node';

import chalk from 'chalk';
import { OptionValues } from 'commander';
import fs from 'fs-extra';
import YAML from 'yaml';

import os from 'os';
import path from 'path';
Expand Down Expand Up @@ -73,6 +75,7 @@ export async function command(opts: OptionValues): Promise<void> {
path.join(os.tmpdir(), 'package-dynamic-plugins'),
);
const pluginRegistryMetadata = [];
const pluginConfigs: Record<string, string> = {};
try {
// copy the dist-dynamic output folder for each plugin to some temp directory and generate the metadata entry for each plugin
for (const pluginPkg of packages) {
Expand Down Expand Up @@ -121,6 +124,25 @@ export async function command(opts: OptionValues): Promise<void> {
keywords,
},
});
// some plugins include configuration snippets in an app-config.janus-idp.yaml
const pluginConfigPath =
discoverPluginConfigurationFile(packageDirectory);
if (typeof pluginConfigPath !== 'undefined') {
try {
const pluginConfig = fs.readFileSync(pluginConfigPath);
pluginConfigs[packageName] = YAML.parse(
pluginConfig.toLocaleString(),
);
} catch (err) {
Task.log(
`Encountered an error parsing configuration at ${pluginConfigPath}, no example configuration will be displayed`,
);
}
} else {
Task.log(
`No plugin configuration found at ${pluginConfigPath} create this file as needed if this plugin requires configuration`,
);
}
} catch (err) {
Task.log(
`Encountered an error copying static assets for plugin ${packageFilePath}, the plugin will not be packaged. The error was ${err}`,
Expand All @@ -143,6 +165,31 @@ COPY . .
`,
{ cwd: tmpDir },
);
Task.log(`Successfully built image ${tag} with following plugins:`);
for (const plugin of pluginRegistryMetadata) {
Task.log(` ${chalk.white(Object.keys(plugin)[0])}`);
}
// print out a configuration example based on available plugin data
try {
const configurationExample = YAML.stringify({
plugins: pluginRegistryMetadata.map(plugin => {
const packageName = Object.keys(plugin)[0];
const pluginConfig = pluginConfigs[packageName];
return {
package: `oci://${tag}!${packageName}`,
disabled: false,
...(pluginConfig ? { pluginConfig } : {}),
};
}),
});
Task.log(
`\nHere is an example dynamic-plugins.yaml for these plugins: \n\n${chalk.white(configurationExample)}\n\n`,
);
} catch (err) {
Task.error(
`An error occurred while creating configuration example: ${err}`,
);
}
} catch (e) {
Task.error(`Error encountered while packaging dynamic plugins: ${e}`);
} finally {
Expand All @@ -153,22 +200,6 @@ COPY . .
if (preserveTempDir) {
Task.log(`Keeping temporary directory ${tmpDir}`);
}

Task.log(`Successfully built image ${tag} with following plugins:`);
for (const plugin of pluginRegistryMetadata) {
Task.log(` ${Object.keys(plugin)[0]}`);
}
Task.log(`
Configuration example for the dynamic-plugins.yaml:
packages:`);
for (const plugin of pluginRegistryMetadata) {
Task.log(`- package: oci://${tag}!${Object.keys(plugin)[0]}
disabled: false
pluginConfig:
# add required plugin configuration here
`);
}
} catch (err) {
Task.error(
`An error occurred while removing the temporary staging directory: ${err}`,
Expand Down Expand Up @@ -225,6 +256,34 @@ async function discoverPluginPackages() {
});
}

/**
* Scans the specified directory for plugin configuration files that
* match known potential file names.
* @param directory
* @returns
*/
function discoverPluginConfigurationFile(
directory: string,
): string | undefined {
// Possible file names, the first match will be used
const supportedFilenames = [
'app-config.janus-idp.yaml',
'app-config.backstage-community.yaml',
'app-config.yaml',
];
return supportedFilenames
.map<boolean>((fileName: string) => {
const candidate = path.join(directory, fileName);
return fs.existsSync(candidate);
})
.reduce<string | undefined>((val, current, index) => {
if (typeof val === 'undefined' && current) {
return path.join(directory, supportedFilenames[index]);
}
return val;
}, undefined);
}

/**
* Scan all subdirectories for the included files, skipping any excluded
* directories.
Expand Down
Loading

0 comments on commit 924d8cd

Please sign in to comment.