Version option should display if snapshot

We have a command-line flag -V or --version that can be used to display
the version of Elasticsearch. However, the version that we display does
not contain whether or not the version is a snapshot build. This commit
changes the behavior here so that if the build is a snapshot, that is
included in the version string.

Relates #25970
This commit is contained in:
Jason Tedor 2017-07-31 11:45:06 +09:00 committed by GitHub
parent bd538aa72c
commit 1afc9afcac
5 changed files with 21 additions and 9 deletions

View File

@ -382,6 +382,10 @@ public class Version implements Comparable<Version> {
return sb.toString();
}
public static String displayVersion(final Version version, final boolean isSnapshot) {
return version + (isSnapshot ? "-SNAPSHOT" : "");
}
@Override
public boolean equals(Object o) {
if (this == o) {

View File

@ -24,6 +24,7 @@ import joptsimple.OptionSpec;
import joptsimple.OptionSpecBuilder;
import joptsimple.util.PathConverter;
import org.elasticsearch.Build;
import org.elasticsearch.Version;
import org.elasticsearch.cli.EnvironmentAwareCommand;
import org.elasticsearch.cli.ExitCodes;
import org.elasticsearch.cli.Terminal;
@ -97,7 +98,7 @@ class Elasticsearch extends EnvironmentAwareCommand {
throw new UserException(ExitCodes.USAGE, "Positional arguments not allowed, found " + options.nonOptionArguments());
}
if (options.has(versionOption)) {
terminal.println("Version: " + org.elasticsearch.Version.CURRENT
terminal.println("Version: " + Version.displayVersion(Version.CURRENT, Build.CURRENT.isSnapshot())
+ ", Build: " + Build.CURRENT.shortHash() + "/" + Build.CURRENT.date()
+ ", JVM: " + JvmInfo.jvmInfo().version());
return;

View File

@ -277,7 +277,7 @@ public class Node implements Closeable {
final JvmInfo jvmInfo = JvmInfo.jvmInfo();
logger.info(
"version[{}], pid[{}], build[{}/{}], OS[{}/{}/{}], JVM[{}/{}/{}/{}]",
displayVersion(Version.CURRENT, Build.CURRENT.isSnapshot()),
Version.displayVersion(Version.CURRENT, Build.CURRENT.isSnapshot()),
jvmInfo.pid(),
Build.CURRENT.shortHash(),
Build.CURRENT.date(),
@ -515,19 +515,14 @@ public class Node implements Closeable {
}
}
// visible for testing
static void warnIfPreRelease(final Version version, final boolean isSnapshot, final Logger logger) {
if (!version.isRelease() || isSnapshot) {
logger.warn(
"version [{}] is a pre-release version of Elasticsearch and is not suitable for production",
displayVersion(version, isSnapshot));
Version.displayVersion(version, isSnapshot));
}
}
private static String displayVersion(final Version version, final boolean isSnapshot) {
return version + (isSnapshot ? "-SNAPSHOT" : "");
}
protected TransportService newTransportService(Settings settings, Transport transport, ThreadPool threadPool,
TransportInterceptor interceptor,
Function<BoundTransportAddress, DiscoveryNode> localNodeFactory,

View File

@ -378,4 +378,16 @@ public class VersionTests extends ESTestCase {
VersionTests.assertUnknownVersion(VERSION_5_1_0_UNRELEASED);
}
public void testDisplayVersion() {
final Version version = randomVersion(random());
{
final String displayVersion = Version.displayVersion(version, true);
assertThat(displayVersion, equalTo(version.toString() + "-SNAPSHOT"));
}
{
final String displayVersion = Version.displayVersion(version, false);
assertThat(displayVersion, equalTo(version.toString()));
}
}
}

View File

@ -63,7 +63,7 @@ public class ElasticsearchCliTests extends ESElasticsearchCliTestCase {
private void runTestThatVersionIsReturned(String... args) throws Exception {
runTestVersion(ExitCodes.OK, output -> {
assertThat(output, containsString("Version: " + Version.CURRENT.toString()));
assertThat(output, containsString("Version: " + Version.displayVersion(Version.CURRENT, Build.CURRENT.isSnapshot())));
assertThat(output, containsString("Build: " + Build.CURRENT.shortHash() + "/" + Build.CURRENT.date()));
assertThat(output, containsString("JVM: " + JvmInfo.jvmInfo().version()));
}, args);