Skip to content

Commit

Permalink
Merge pull request #19 from spyrkob/query_version_return_string
Browse files Browse the repository at this point in the history
Make channel query-version return simple version string
  • Loading branch information
spyrkob authored Mar 22, 2024
2 parents f073b81 + 94073f0 commit f92a881
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/main/java/org/wildfly/prospero/extras/ReturnCodes.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@
public class ReturnCodes {
public static final Integer SUCCESS = 0;
public static final Integer INVALID_ARGUMENTS = 1;
public static final Integer ERROR = 2;
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package org.wildfly.prospero.extras.channel.query;

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.eclipse.aether.RepositorySystem;
import org.wildfly.channel.ChannelMapper;
import org.wildfly.channel.ChannelSession;
import org.wildfly.channel.VersionResult;
import org.wildfly.channel.maven.VersionResolverFactory;
import org.wildfly.channel.spi.MavenVersionsResolver;
import org.wildfly.prospero.extras.ReturnCodes;
Expand All @@ -16,6 +21,9 @@
@CommandLine.Command(name = "query-version")
public class QueryVersionCommand extends CommandWithHelp {

private static final JsonFactory JSON_FACTORY = new JsonFactory();
private static final ObjectMapper OBJECT_MAPPER = (new ObjectMapper(JSON_FACTORY)).configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false).configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true);

@CommandLine.Option(names={"--channel"}, required = false)
private Path channelFile;

Expand All @@ -25,14 +33,46 @@ public class QueryVersionCommand extends CommandWithHelp {
@CommandLine.Option(names={"--artifactId"})
private String artifactId;

@CommandLine.Option(names={"--json"})
private boolean displayJson;

@Override
public Integer call() throws Exception {
final MavenSessionManager msm = new MavenSessionManager();
final RepositorySystem system = msm.newRepositorySystem();
MavenVersionsResolver.Factory factory = new VersionResolverFactory(system, msm.newRepositorySystemSession(system));

ChannelSession ses = new ChannelSession(List.of(ChannelMapper.from(channelFile.toUri().toURL())), factory);
System.out.println(ses.findLatestMavenArtifactVersion(groupId, artifactId, null, null, null));
return ReturnCodes.SUCCESS;
final VersionResult version = ses.findLatestMavenArtifactVersion(groupId, artifactId, null, null, null);
if (version == null) {
System.err.printf("No version of artifact %s:%s found in the channel.%n", groupId, artifactId);
return ReturnCodes.ERROR;
} else {
if (displayJson) {
System.out.println(OBJECT_MAPPER.writeValueAsString(new JsonVersionResult(version)));
} else {
System.out.println(version.getVersion());
}
return ReturnCodes.SUCCESS;
}
}

class JsonVersionResult {

private final String version;
private final String channelName;

JsonVersionResult(VersionResult result) {
version = result.getVersion();
channelName = result.getChannelName().orElse(null);
}

public String getVersion() {
return version;
}

public String getChannelName() {
return channelName;
}
}
}
6 changes: 6 additions & 0 deletions src/main/resources/UsageMessages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ tools.channel.merge-repositories.manifest-url=the URL the new manifest can be re
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.
tools.channel.query-version.usage.header=Retrieves a version of artifact found in the channel.
tools.channel.query-version.usage.description=Retrieves a version of artifact found in the channel. Returns error code (2) if no version is found
tools.channel.query-version.groupId=Maven groupId coordinate of the artifact to find.
tools.channel.query-version.artifactId=Maven artifactId coordinate of the artifact to find.
tools.channel.query-version.json=Print the output in JSON format.
usage.parameterListHeading = %nPositional parameters:%n
usage.optionListHeading = %nOptions:%n
usage.synopsisHeading = %nSyntax:%n
Expand Down

0 comments on commit f92a881

Please sign in to comment.