Skip to content

Commit

Permalink
[process] Enhance process command to support the collection of descri…
Browse files Browse the repository at this point in the history
…ptors in a single file - avoids to mess up dump when templates are used
  • Loading branch information
rmannibucau committed Aug 31, 2023
1 parent d66956f commit 28059c3
Showing 1 changed file with 30 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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<String>();
final BiConsumer<String, JsonObject> 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);
}
Expand All @@ -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,
Expand Down

0 comments on commit 28059c3

Please # to comment.