-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from spyrkob/merge_channels
channel merge-repositories command
- Loading branch information
Showing
6 changed files
with
194 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
src/main/java/org/wildfly/prospero/extras/channel/merge/ChannelMergeCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
src/test/java/org/wildfly/prospero/extras/channel/merge/ChannelMergeCommandTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |