From 28059c337dc1e5cd79d7aa3c26f1a4e3dee4607f Mon Sep 17 00:00:00 2001 From: Romain Manni-Bucau Date: Thu, 31 Aug 2023 15:48:49 +0400 Subject: [PATCH] [process] Enhance process command to support the collection of descriptors in a single file - avoids to mess up dump when templates are used --- .../core/command/impl/ProcessCommand.java | 32 +++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/bundlebee-core/src/main/java/io/yupiik/bundlebee/core/command/impl/ProcessCommand.java b/bundlebee-core/src/main/java/io/yupiik/bundlebee/core/command/impl/ProcessCommand.java index 17c185b6..6ac9bde6 100644 --- a/bundlebee-core/src/main/java/io/yupiik/bundlebee/core/command/impl/ProcessCommand.java +++ b/bundlebee-core/src/main/java/io/yupiik/bundlebee/core/command/impl/ProcessCommand.java @@ -36,6 +36,7 @@ import java.io.StringWriter; import java.nio.file.Files; import java.nio.file.Path; +import java.util.ArrayList; import java.util.Map; import java.util.concurrent.CompletionStage; import java.util.function.BiConsumer; @@ -110,6 +111,11 @@ public class ProcessCommand extends BaseLabelEnricherCommand implements Completi @ConfigProperty(name = "bundlebee.process.output", defaultValue = UNSET) private String output; + @Inject + @Description("Should all descriptors be concatenated in a single file - output.") + @ConfigProperty(name = "bundlebee.process.concat", defaultValue = UNSET) + private boolean concat; + @Inject private KubeClient kube; @@ -143,16 +149,25 @@ public CompletionStage doExecute(final String from, final String manifest, fi final var out = output == null || UNSET.equals(output) ? null : Path.of(output); final var jsonReaderFactory = provider.createReaderFactory(Map.of()); final var jsonWriterFactory = provider.createWriterFactory(Map.of(JsonGenerator.PRETTY_PRINTING, true)); + final var collector = new ArrayList(); final BiConsumer onDescriptor = out == null ? (name, content) -> log.info(() -> name + ":\n" + format(content, jsonReaderFactory, jsonWriterFactory)) : (name, content) -> { + final var formatted = format(content, jsonReaderFactory, jsonWriterFactory); + if (concat) { + synchronized (collector) { + collector.add(formatted); + } + return; + } + final var target = out.resolve(name); log.info(() -> "Dumping '" + target + "'"); try { if (target.getParent() != null) { Files.createDirectories(target.getParent()); } - Files.writeString(target, format(content, jsonReaderFactory, jsonWriterFactory)); + Files.writeString(target, formatted); } catch (final IOException e) { throw new IllegalStateException(e); } @@ -169,7 +184,20 @@ public CompletionStage doExecute(final String from, final String manifest, fi .map(it -> doExecute(injectTimestamp, injectBundleBeeMetadata, cache, it, onDescriptor)) .collect(toList()), toList(), true) - .thenApply(ignored -> null)); + .thenApply(ignored -> null)) + .thenApply(it -> { + if (out != null && concat) { + try { + if (out.getParent() != null) { + Files.createDirectories(out.getParent()); + } + Files.writeString(out, String .join("\n---\n", collector)); + } catch (final IOException ioe) { + throw new IllegalStateException(ioe); + } + } + return it; + }); } public CompletionStage doExecute(final boolean injectTimestamp, final boolean injectBundleBeeMetadata,