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 Oct 4, 2024
1 parent 04b222b commit a0f23d3
Showing 1 changed file with 49 additions and 16 deletions.
65 changes: 49 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,27 @@ export async function command(opts: OptionValues): Promise<void> {
keywords,
},
});
// some plugins include configuration snippets in an app-config.janus-idp.yaml
const pluginConfigPath = path.join(
packageDirectory,
'app-config.janus-idp.yaml',
);
if (fs.existsSync(pluginConfigPath)) {
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 +167,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 pluginName = Object.keys(plugin)[0];
const pluginConfig = pluginConfigs[pluginName];
return {
package: `oci://${tag}!${pluginName}`,
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 +202,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

0 comments on commit a0f23d3

Please sign in to comment.