Skip to content

Commit

Permalink
Merge pull request #7 from spyrkob/merge_channels
Browse files Browse the repository at this point in the history
channel merge-repositories command
  • Loading branch information
spyrkob authored Feb 5, 2024
2 parents a5005fd + 7932dde commit e0a77aa
Show file tree
Hide file tree
Showing 6 changed files with 194 additions and 2 deletions.
9 changes: 8 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
<version.org.mockito>3.12.4</version.org.mockito>
<version.junit>4.13.2</version.junit>
<version.maven-shade-plugin>2.3</version.maven-shade-plugin>
<version.org.wildfly.channel>1.0.0.Beta4</version.org.wildfly.channel>
<version.org.wildfly.channel>1.0.2.Final</version.org.wildfly.channel>
<version.maven-compiler-plugin>3.10.1</version.maven-compiler-plugin>
<version.info.picocli>4.6.3</version.info.picocli>
<version.system-rules>1.19.0</version.system-rules>
Expand Down Expand Up @@ -77,6 +77,13 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.25.2</version>
<scope>test</scope>
</dependency>


</dependencies>

Expand Down
2 changes: 2 additions & 0 deletions src/main/java/org/wildfly/prospero/extras/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import org.wildfly.prospero.extras.bundle.create.CreateBundleCommand;
import org.wildfly.prospero.extras.channel.ChannelCommands;
import org.wildfly.prospero.extras.channel.merge.ChannelMergeCommand;
import org.wildfly.prospero.extras.channel.query.QueryVersionCommand;
import org.wildfly.prospero.extras.manifest.diff.ManifestsDiffCommand;
import org.wildfly.prospero.extras.manifest.download.DownloadDiffCommand;
Expand Down Expand Up @@ -50,6 +51,7 @@ private static CommandLine createCommandLine() {
final ChannelCommands channelSubcommand = new ChannelCommands(commandLine);
commandLine.addSubcommand(channelSubcommand);
channelSubcommand.addSubCommand(new QueryVersionCommand());
channelSubcommand.addSubCommand(new ChannelMergeCommand());

final RepositoryCommands repositoryCommands = new RepositoryCommands(commandLine);
commandLine.addSubcommand(repositoryCommands);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package org.wildfly.prospero.extras.channel.merge;

import org.wildfly.channel.Channel;
import org.wildfly.channel.ChannelMapper;
import org.wildfly.channel.Repository;
import org.wildfly.prospero.extras.ReturnCodes;
import org.wildfly.prospero.extras.shared.CommandWithHelp;
import picocli.CommandLine;

import java.io.IOException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Stream;

/**
* Produces a channel definition listing repositories from all input channels and using provided manifest.
*/
@CommandLine.Command(name = "merge-repositories", sortOptions = false)
public class ChannelMergeCommand extends CommandWithHelp {

@CommandLine.Option(names = "--manifest-url", required = true, order = 1)
URL manifestUrl;
@CommandLine.Option(names = "--channel", split = ",", required = true, order = 2)
List<Path> inputChannelPaths;
@CommandLine.Option(names = "--name", required = false, order = 3)
String name;
@CommandLine.Option(names = "--description", required = false, order = 4)
String description;

@Override
public Integer call() throws Exception {
final Stream<Channel> channels = inputChannelPaths.stream()
.map(ChannelMergeCommand::read)
.flatMap(List::stream);

final Channel channel = merge(channels, manifestUrl, name, description);

System.out.println(ChannelMapper.toYaml(channel));

return ReturnCodes.SUCCESS;
}

Channel merge(Stream<Channel> channels, URL url, String name, String description) {
final Channel.Builder channelBuilder = new Channel.Builder()
.setManifestUrl(url);

if (name != null) {
channelBuilder.setName(name);
}
if (description != null) {
channelBuilder.setDescription(description);
}

channels
.map(Channel::getRepositories)
.flatMap(List::stream)
.distinct()
.sorted(Comparator.comparing(Repository::getId))
.forEach(r -> channelBuilder.addRepository(r.getId(), r.getUrl()));

return channelBuilder.build();

}

private static List<Channel> read(Path path) {
try {
return ChannelMapper.fromString(Files.readString(path));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
import picocli.CommandLine;

public abstract class CommandWithHelp implements Command {
@CommandLine.Option(names = {"-h", "--help"}, usageHelp = true)
@CommandLine.Option(names = {"-h", "--help"}, usageHelp = true, order = Integer.MAX_VALUE)
boolean help;
}
14 changes: 14 additions & 0 deletions src/main/resources/UsageMessages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,19 @@ tools.manifest-merge.usage.description.1=The LATEST merge strategy compares the
tools.manifest-merge.usage.description.2=The FIRST merge strategy chooses the stream from `<manifestOne>`.
mode=merge strategy to use. The default strategy is ${DEFAULT-VALUE}.
tools.channel.merge-repositories.usage.header=Merges repositories from multiple channels into one channel.
tools.channel.merge-repositories.usage.description.0=Prints a channel using all the repositories from the input channels.\
The new channel uses a manifest provided as @|bold manifestUrl|@.
tools.channel.merge-repositories.usage.description.1=This command can be used to generate a channel after provisioning a combination \
of open channels.
tools.channel.merge-repositories.channel=path to input channel definition.
tools.channel.merge-repositories.manifest-url=the URL the new manifest can be resolved from.
tools.channel.merge-repositories.description=optional text to use in description of the new channel.
tools.channel.merge-repositories.name=optional name of the new channel.
usage.parameterListHeading = %nPositional parameters:%n
usage.optionListHeading = %nOptions:%n
usage.synopsisHeading = %nSyntax:%n
usage.descriptionHeading = %nDescription:%n
help=Display this help message.
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package org.wildfly.prospero.extras.channel.merge;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.wildfly.channel.Channel;
import org.wildfly.channel.Repository;

import java.net.URL;
import java.util.stream.Stream;

import static org.assertj.core.api.Assertions.assertThat;

class ChannelMergeCommandTest {

private ChannelMergeCommand channelMergeCommand;


@BeforeEach
public void setUp() {
channelMergeCommand = new ChannelMergeCommand();
}

@Test
public void noRepositories() throws Exception {
final Channel c1 = new Channel.Builder()
.setManifestUrl(new URL("http://manifest-one"))
.build();
final Channel c2 = new Channel.Builder()
.setManifestUrl(new URL("http://manifest-two"))
.build();

final Channel res = channelMergeCommand.merge(Stream.of(c1, c2), new URL("http://new-manifest"), null, null);

assertThat(res.getRepositories())
.isEmpty();
}

@Test
public void mergesRepositories() throws Exception {
final Channel c1 = new Channel.Builder()
.setManifestUrl(new URL("http://manifest-one"))
.addRepository("repo-one", "http://repo-one")
.build();
final Channel c2 = new Channel.Builder()
.setManifestUrl(new URL("http://manifest-two"))
.addRepository("repo-two", "http://repo-two")
.build();

final Channel res = channelMergeCommand.merge(Stream.of(c1, c2), new URL("http://new-manifest"), null, null);

assertThat(res.getRepositories())
.containsExactly(
new Repository("repo-one", "http://repo-one"),
new Repository("repo-two", "http://repo-two")
);
}

@Test
public void removesDuplicateRepositories() throws Exception {
final Channel c1 = new Channel.Builder()
.setManifestUrl(new URL("http://manifest-one"))
.addRepository("repo-one", "http://repo-one")
.build();
final Channel c2 = new Channel.Builder()
.setManifestUrl(new URL("http://manifest-two"))
.addRepository("repo-one", "http://repo-one")
.build();

final Channel res = channelMergeCommand.merge(Stream.of(c1, c2), new URL("http://new-manifest"), null, null);

assertThat(res.getRepositories())
.containsExactly(
new Repository("repo-one", "http://repo-one")
);
}

@Test
public void setNameAndDesctiptionIfAvailable() throws Exception {
final Channel c1 = new Channel.Builder()
.setManifestUrl(new URL("http://manifest-one"))
.addRepository("repo-one", "http://repo-one")
.build();
final Channel c2 = new Channel.Builder()
.setManifestUrl(new URL("http://manifest-two"))
.addRepository("repo-two", "http://repo-two")
.build();

final Channel res = channelMergeCommand.merge(Stream.of(c1, c2), new URL("http://new-manifest"), "test-name", "test-desc");

assertThat(res)
.hasFieldOrPropertyWithValue("name", "test-name")
.hasFieldOrPropertyWithValue("description", "test-desc");
}
}

0 comments on commit e0a77aa

Please sign in to comment.