Skip to content

Commit

Permalink
feat(cli): add flag to specify output directory
Browse files Browse the repository at this point in the history
This change adds an --export-to flag to the package-dynamic-plugins
CLI command. This flag will skip the container build step, and instead
copy the staged dynamic plugin assets to the specified output directory.
The addition of this flag makes the --tag argument optional, and only
required if --export-to is not specified.

Signed-off-by: Stan Lewis <gashcrumb@gmail.com>
  • Loading branch information
gashcrumb committed Nov 4, 2024
1 parent 924d8cd commit 5b3b985
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 21 deletions.
7 changes: 7 additions & 0 deletions .changeset/mean-eagles-dress.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@janus-idp/cli": minor
---

feat(cli) add flag to specify output directory

This change adds an --export-to flag to the package-dynamic-plugins CLI command. This flag will skip the container build step, and instead copy the staged dynamic plugin assets to the specified output directory. The addition of this flag makes the --tag argument optional, and only required if --export-to is not specified.
8 changes: 6 additions & 2 deletions packages/cli/src/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,13 @@ export function registerScriptCommand(program: Command) {
'--preserve-temp-dir',
'Leave the temporary staging directory on the filesystem instead of deleting it',
)
.requiredOption(
.option(
'--export-to <directory>',
'Export the plugins to the specified directory, skips building the container image',
)
.option(
'-t, --tag <tag>',
'Tag name to use when building the plugin registry image',
'Tag name to use when building the plugin registry image. Required if "--export-to" is not specified',
)
.option(
'--use-docker',
Expand Down
64 changes: 45 additions & 19 deletions packages/cli/src/commands/package-dynamic-plugins/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,25 @@ import { paths } from '../../lib/paths';
import { Task } from '../../lib/tasks';

export async function command(opts: OptionValues): Promise<void> {
const { forceExport, preserveTempDir, tag, useDocker } = opts;
const containerTool = useDocker ? 'docker' : 'podman';
// check if the container tool is available
try {
await Task.forCommand(`${containerTool} --version`);
} catch (e) {
const { exportTo, forceExport, preserveTempDir, tag, useDocker } = opts;
if (!exportTo && !tag) {
Task.error(
`Unable to find ${containerTool} command: ${e}\nMake sure that ${containerTool} is installed and available in your PATH.`,
`Neither ${chalk.white('--export-to')} or ${chalk.white('--tag')} was specified, either specify ${chalk.white('--export-to')} to export plugins to a directory or ${chalk.white('--tag')} to export plugins to a container image`,
);
return;
}

const containerTool = useDocker ? 'docker' : 'podman';
// check if the container tool is available, skip if just exporting the plugins to a directory
if (!exportTo) {
try {
await Task.forCommand(`${containerTool} --version`);
} catch (e) {
Task.error(
`Unable to find ${containerTool} command: ${e}\nMake sure that ${containerTool} is installed and available in your PATH.`,
);
return;
}
}
const workspacePackage = await fs.readJson(
paths.resolveTarget('package.json'),
);
Expand Down Expand Up @@ -95,7 +102,12 @@ export async function command(opts: OptionValues): Promise<void> {
const targetDirectory = path.join(tmpDir, packageName);
Task.log(`Copying '${distDirectory}' to '${targetDirectory}`);
try {
fs.cpSync(distDirectory, targetDirectory, { recursive: true });
// Copy the exported package to the staging area and ensure symlinks
// are copied as normal folders
fs.cpSync(distDirectory, targetDirectory, {
recursive: true,
dereference: true,
});
const {
name,
version,
Expand Down Expand Up @@ -156,27 +168,41 @@ export async function command(opts: OptionValues): Promise<void> {
metadataFile,
JSON.stringify(pluginRegistryMetadata, undefined, 2),
);
// run the command to generate the image
Task.log(`Creating image using ${containerTool}`);
await Task.forCommand(
`echo "from scratch
if (exportTo) {
// copy the temporary directory contents to the target directory
fs.mkdirSync(exportTo, { recursive: true });
Task.log(`Writing exported plugins to ${exportTo}`);
fs.readdirSync(tmpDir).forEach(entry => {
const source = path.join(tmpDir, entry);
const destination = path.join(exportTo, entry);
fs.copySync(source, destination, { recursive: true, overwrite: true });
});
} else {
// run the command to generate the image
Task.log(`Creating image using ${containerTool}`);
await Task.forCommand(
`echo "from scratch
COPY . .
" | ${containerTool} build --annotation com.redhat.rhdh.plugins='${JSON.stringify(pluginRegistryMetadata)}' -t '${tag}' -f - .
`,
{ cwd: tmpDir },
);
Task.log(`Successfully built image ${tag} with following plugins:`);
for (const plugin of pluginRegistryMetadata) {
Task.log(` ${chalk.white(Object.keys(plugin)[0])}`);
{ 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];
const packageString = exportTo
? `./local-plugins/${packageName}`
: `oci://${tag}!${packageName}`;
return {
package: `oci://${tag}!${packageName}`,
package: packageString,
disabled: false,
...(pluginConfig ? { pluginConfig } : {}),
};
Expand Down

0 comments on commit 5b3b985

Please sign in to comment.