Skip to content

Commit

Permalink
Add channel versions command to list installed channel versions
Browse files Browse the repository at this point in the history
  • Loading branch information
spyrkob committed Oct 18, 2023
1 parent 26a2b4d commit 6d0717d
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ public static CommandLine createCommandLine(CliConsole console, String[] args, A
channelCmd.addSubcommand(new ChannelAddCommand(console, actionFactory));
channelCmd.addSubcommand(new ChannelRemoveCommand(console, actionFactory));
channelCmd.addSubcommand(new ChannelCommand.ChannelListCommand(console, actionFactory));
channelCmd.addSubcommand(new ChannelCommand.ChannelVersionCommand(console, actionFactory));
channelCmd.addSubcommand(new ChannelInitializeCommand(console, actionFactory));
channelCmd.addSubcommand(new ChannelPromoteCommand(console, actionFactory));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -623,4 +623,8 @@ default String quitGenerating() {
default String metadataExistsAlready(Path path, String distName) {
return format(bundle.getString("prospero.update.subscribe.meta.exists"), path, distName);
}

default String serverVersionsHeader() {
return bundle.getString("prospero.channels.versions.header");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.wildfly.prospero.cli.CliMessages;
import org.wildfly.prospero.cli.ReturnCodes;
import org.wildfly.prospero.cli.printers.ChannelPrinter;
import org.wildfly.prospero.metadata.ManifestVersionRecord;
import picocli.CommandLine;

@CommandLine.Command(name = CliConstants.Commands.CHANNEL)
Expand Down Expand Up @@ -78,5 +79,46 @@ public Integer call() throws Exception {
}
}

@CommandLine.Command(name = CliConstants.Commands.VERSIONS)
public static class ChannelVersionCommand extends AbstractCommand {

protected static final String PREFIX = " * ";
@CommandLine.Option(names = CliConstants.DIR)
private Optional<Path> directory;

public ChannelVersionCommand(CliConsole console, ActionFactory actionFactory) {
super(console, actionFactory);
}

@Override
public Integer call() throws Exception {
final Path installationDir = determineInstallationDirectory(directory);

console.println(CliMessages.MESSAGES.serverVersionsHeader());
try (MetadataAction metadataAction = actionFactory.metadataActions(installationDir)) {
final ManifestVersionRecord channelVersions = metadataAction.getChannelVersions();
for (ManifestVersionRecord.MavenManifest mavenManifest : channelVersions.getMavenManifests()) {
if (mavenManifest.getDescription() != null) {
console.println(PREFIX + mavenManifest.getDescription());
} else {
console.println(PREFIX + buildManifestGav(mavenManifest));
}
}
for (ManifestVersionRecord.UrlManifest urlManifest : channelVersions.getUrlManifests()) {
if (urlManifest.getDescription() != null) {
console.println(PREFIX + urlManifest.getDescription());
} else {
console.println(PREFIX + String.format("%s [%s]", urlManifest.getUrl(), urlManifest.getHash()));
}
}
}
return ReturnCodes.SUCCESS;
}

private static String buildManifestGav(ManifestVersionRecord.MavenManifest mavenManifest) {
return String.format("%s:%s [%s]", mavenManifest.getGroupId(), mavenManifest.getArtifactId(), mavenManifest.getVersion());
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ private CliConstants() {
public static final class Commands {


protected static final String VERSIONS = "versions";

private Commands() {
}

Expand Down
2 changes: 2 additions & 0 deletions prospero-cli/src/main/resources/UsageMessages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ ${prospero.dist.name}.channel.list.usage.header = Lists the channels used by t
${prospero.dist.name}.channel.remove.usage.header = Unsubscribes the installation from a channel.
${prospero.dist.name}.channel.initialize.usage.header = Add a custom channel to be used by the server
${prospero.dist.name}.channel.promote.usage.header = Promote a bundle of artifacts to a custom repository
${prospero.dist.name}.channel.versions.usage.header = Displays currently used versions of manifests from registered channels.

${prospero.dist.name}.clone.usage.header = Exports installation details required to recreate a server.
${prospero.dist.name}.clone.export.usage.header = Exports the installation details that can be used to recreate a server.
Expand Down Expand Up @@ -299,6 +300,7 @@ prospero.export.done=Export complete

prospero.channels.list.header=Server %s is subscribed to following channels:%n
prospero.channels.add.header=Subscribing %s to channel %s%n
prospero.channels.versions.header=Installed server components:
prospero.channels.added=Channel '%s' added.
prospero.channels.remove.header=Unsubscribing %s from channel %s%n
prospero.channels.removed=Channel '%s' removed.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,15 @@ public void testListInvalidInstallationDir() {
.getMessage()));
}

@Test
public void testVersionInvalidInstallationDir() {
int exitCode = commandLine.execute(CliConstants.Commands.CHANNEL, CliConstants.Commands.VERSIONS);

Assert.assertEquals(ReturnCodes.INVALID_ARGUMENTS, exitCode);
assertTrue(getErrorOutput().contains(CliMessages.MESSAGES.invalidInstallationDir(ChannelCommand.currentDir())
.getMessage()));
}

@Test
public void testAddEmptyRepository() {
int exitCode = commandLine.execute(CliConstants.Commands.CHANNEL, CliConstants.Commands.ADD,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package org.wildfly.prospero.actions;

import java.io.IOException;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
Expand All @@ -26,6 +27,7 @@
import org.wildfly.prospero.ProsperoLogger;
import org.wildfly.prospero.api.InstallationMetadata;
import org.wildfly.prospero.api.exceptions.MetadataException;
import org.wildfly.prospero.metadata.ManifestVersionRecord;
import org.wildfly.prospero.model.ProsperoConfig;

/**
Expand Down Expand Up @@ -90,6 +92,14 @@ public List<Channel> getChannels() throws MetadataException {
return new ArrayList<>(installationMetadata.getProsperoConfig().getChannels());
}

public ManifestVersionRecord getChannelVersions() {
try {
return installationMetadata.getManifestVersions().orElse(new ManifestVersionRecord());
} catch (IOException e) {
throw new RuntimeException(e);
}
}

@Override
public void close() {
this.installationMetadata.close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,12 @@
import org.wildfly.prospero.api.MavenOptions.Builder;
import org.wildfly.prospero.galleon.GalleonCallbackAdapter;
import org.wildfly.prospero.metadata.ManifestVersionRecord;
import org.wildfly.prospero.metadata.ProsperoMetadataUtils;
import org.wildfly.prospero.spi.internal.CliProvider;
import org.wildfly.prospero.api.SavedState;
import org.wildfly.prospero.api.exceptions.MetadataException;
import org.wildfly.prospero.api.exceptions.OperationException;
import org.wildfly.prospero.updates.UpdateSet;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.LocalDateTime;
Expand Down Expand Up @@ -221,22 +219,17 @@ public String generateApplyRevertCommand(Path scriptHome, Path candidatePath, Os
}

@Override
public Collection<ManifestVersion> getInstalledVersions() {
try {
final Optional<ManifestVersionRecord> versionRecord = ManifestVersionRecord.read(installationDir.resolve(ProsperoMetadataUtils.METADATA_DIR).resolve(ProsperoMetadataUtils.CURRENT_VERSION_FILE));
if (versionRecord.isPresent()) {
return Stream.concat(
versionRecord.get().getMavenManifests().stream()
.map(m->new ManifestVersion(m.getGroupId()+":"+m.getArtifactId(), m.getDescription(), m.getVersion(), ManifestVersion.Type.MAVEN)),
versionRecord.get().getUrlManifests().stream()
.map(m->new ManifestVersion(m.getUrl(), m.getDescription(), m.getHash(), ManifestVersion.Type.URL))
)
.collect(Collectors.toList());
}
} catch (IOException e) {
throw new RuntimeException(e);
public Collection<ManifestVersion> getInstalledVersions() throws MetadataException {
try (MetadataAction metadataAction = actionFactory.getMetadataAction()) {
final ManifestVersionRecord versionRecord = metadataAction.getChannelVersions();
return Stream.concat(
versionRecord.getMavenManifests().stream()
.map(m->new ManifestVersion(m.getGroupId()+":"+m.getArtifactId(), m.getDescription(), m.getVersion(), ManifestVersion.Type.MAVEN)),
versionRecord.getUrlManifests().stream()
.map(m->new ManifestVersion(m.getUrl(), m.getDescription(), m.getHash(), ManifestVersion.Type.URL))
)
.collect(Collectors.toList());
}
return Collections.emptyList();
}

private String escape(Path absolutePath) {
Expand Down

0 comments on commit 6d0717d

Please sign in to comment.