From a0f23d3e3ff6aa83002468fe8d60399179ccf1a3 Mon Sep 17 00:00:00 2001 From: Stan Lewis Date: Fri, 4 Oct 2024 13:15:10 -0400 Subject: [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. Signed-off-by: Stan Lewis --- .../package-dynamic-plugins/command.ts | 65 ++++++++++++++----- 1 file changed, 49 insertions(+), 16 deletions(-) diff --git a/packages/cli/src/commands/package-dynamic-plugins/command.ts b/packages/cli/src/commands/package-dynamic-plugins/command.ts index 7dc4487dcc5..ef67cb70bd7 100644 --- a/packages/cli/src/commands/package-dynamic-plugins/command.ts +++ b/packages/cli/src/commands/package-dynamic-plugins/command.ts @@ -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'; @@ -73,6 +75,7 @@ export async function command(opts: OptionValues): Promise { path.join(os.tmpdir(), 'package-dynamic-plugins'), ); const pluginRegistryMetadata = []; + const pluginConfigs: Record = {}; 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) { @@ -121,6 +124,27 @@ export async function command(opts: OptionValues): Promise { 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}`, @@ -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 { @@ -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}`,