SLING-10294: CLI Status Code (#8)
* SLING-10294: Updating the commands to return a status code to the shell
* Spaces v Tabs
* Removing unnecessary throws
* Whitespace difference
* Using Exit Code constants from Picoli
* Updating the tests to use the constants as well
diff --git a/src/main/java/org/apache/sling/cli/impl/Command.java b/src/main/java/org/apache/sling/cli/impl/Command.java
index 57b9e50..b254c69 100644
--- a/src/main/java/org/apache/sling/cli/impl/Command.java
+++ b/src/main/java/org/apache/sling/cli/impl/Command.java
@@ -16,10 +16,12 @@
*/
package org.apache.sling.cli.impl;
+import java.util.concurrent.Callable;
+
/**
* Marker interface for {@code Commands} supported by the Apache Sling Committer CLI.
*/
-public interface Command extends Runnable {
+public interface Command extends Callable<Integer> {
String PROPERTY_NAME_COMMAND_GROUP = "command.group";
String PROPERTY_NAME_COMMAND_NAME = "command.name";
diff --git a/src/main/java/org/apache/sling/cli/impl/release/CreateJiraVersionCommand.java b/src/main/java/org/apache/sling/cli/impl/release/CreateJiraVersionCommand.java
index 063e4ab..906d6b4 100644
--- a/src/main/java/org/apache/sling/cli/impl/release/CreateJiraVersionCommand.java
+++ b/src/main/java/org/apache/sling/cli/impl/release/CreateJiraVersionCommand.java
@@ -67,7 +67,7 @@
private final Logger logger = LoggerFactory.getLogger(getClass());
@Override
- public void run() {
+ public Integer call() {
try {
StagingRepository repo = repositoryService.find(repositoryId);
for (Release release : repositoryService.getReleases(repo)) {
@@ -122,6 +122,8 @@
}
} catch (IOException e) {
logger.warn("Failed executing command", e);
+ return CommandLine.ExitCode.SOFTWARE;
}
+ return CommandLine.ExitCode.OK;
}
}
diff --git a/src/main/java/org/apache/sling/cli/impl/release/ListCommand.java b/src/main/java/org/apache/sling/cli/impl/release/ListCommand.java
index 7773bea..ec15592 100644
--- a/src/main/java/org/apache/sling/cli/impl/release/ListCommand.java
+++ b/src/main/java/org/apache/sling/cli/impl/release/ListCommand.java
@@ -49,11 +49,13 @@
private RepositoryService repositoryService;
@Override
- public void run() {
+ public Integer call() {
try {
repositoryService.list().forEach( r -> logger.info("{}\t{}", r.getRepositoryId(), cleanupNewlines(r.getDescription())));
+ return CommandLine.ExitCode.OK;
} catch (IOException e) {
logger.warn("Failed executing command", e);
+ return CommandLine.ExitCode.SOFTWARE;
}
}
}
diff --git a/src/main/java/org/apache/sling/cli/impl/release/PrepareVoteEmailCommand.java b/src/main/java/org/apache/sling/cli/impl/release/PrepareVoteEmailCommand.java
index 4ade9ca..5c0b09d 100644
--- a/src/main/java/org/apache/sling/cli/impl/release/PrepareVoteEmailCommand.java
+++ b/src/main/java/org/apache/sling/cli/impl/release/PrepareVoteEmailCommand.java
@@ -104,7 +104,7 @@
"https://issues.apache.org/jira/browse/SLING/fixforversion/##VERSION_ID##";
@Override
- public void run() {
+ public Integer call() {
try {
CommandLine commandLine = spec.commandLine();
if (commandLine.isUsageHelpRequested()) {
@@ -171,6 +171,8 @@
}
} catch (IOException e) {
LOGGER.warn("Failed executing command", e);
+ return CommandLine.ExitCode.SOFTWARE;
}
+ return CommandLine.ExitCode.OK;
}
}
diff --git a/src/main/java/org/apache/sling/cli/impl/release/ReleaseJiraVersionCommand.java b/src/main/java/org/apache/sling/cli/impl/release/ReleaseJiraVersionCommand.java
index cbdde72..ea85184 100644
--- a/src/main/java/org/apache/sling/cli/impl/release/ReleaseJiraVersionCommand.java
+++ b/src/main/java/org/apache/sling/cli/impl/release/ReleaseJiraVersionCommand.java
@@ -69,7 +69,7 @@
private ReusableCLIOptions reusableCLIOptions;
@Override
- public void run() {
+ public Integer call() {
try {
StagingRepository repo = repositoryService.find(repositoryId);
Set<Release> releases = repositoryService.getReleases(repo);
@@ -100,6 +100,8 @@
}
} catch (Exception e) {
LOGGER.warn("Failed executing command.", e);
+ return CommandLine.ExitCode.SOFTWARE;
}
+ return CommandLine.ExitCode.OK;
}
}
diff --git a/src/main/java/org/apache/sling/cli/impl/release/TallyVotesCommand.java b/src/main/java/org/apache/sling/cli/impl/release/TallyVotesCommand.java
index 25f43cb..7463882 100644
--- a/src/main/java/org/apache/sling/cli/impl/release/TallyVotesCommand.java
+++ b/src/main/java/org/apache/sling/cli/impl/release/TallyVotesCommand.java
@@ -96,7 +96,7 @@
}
@Override
- public void run() {
+ public Integer call() {
try {
StagingRepository repository = repositoryService.find(repositoryId);
Set<Release> releases = repositoryService.getReleases(repository);
@@ -167,12 +167,15 @@
} else {
LOGGER.info("Release {} does not have at least 3 binding votes.", releaseFullName);
LOGGER.info("Binding votes: {}.", bindingVoters.isEmpty() ? "none" : String.join(", ", bindingVoters));
+ return CommandLine.ExitCode.USAGE;
}
}
} catch (IOException e) {
LOGGER.warn("Command execution failed", e);
+ return CommandLine.ExitCode.SOFTWARE;
}
+ return CommandLine.ExitCode.OK;
}
// TODO - better detection of '+1' votes
diff --git a/src/main/java/org/apache/sling/cli/impl/release/UpdateLocalSiteCommand.java b/src/main/java/org/apache/sling/cli/impl/release/UpdateLocalSiteCommand.java
index d21ea02..dbaf6e6 100644
--- a/src/main/java/org/apache/sling/cli/impl/release/UpdateLocalSiteCommand.java
+++ b/src/main/java/org/apache/sling/cli/impl/release/UpdateLocalSiteCommand.java
@@ -62,7 +62,7 @@
private Integer repositoryId;
@Override
- public void run() {
+ public Integer call() {
try {
ensureRepo();
try ( Git git = Git.open(new File(GIT_CHECKOUT)) ) {
@@ -86,7 +86,9 @@
}
} catch (GitAPIException | IOException e) {
logger.warn("Failed executing command", e);
+ return CommandLine.ExitCode.SOFTWARE;
}
+ return CommandLine.ExitCode.OK;
}
diff --git a/src/main/java/org/apache/sling/cli/impl/release/UpdateReporterCommand.java b/src/main/java/org/apache/sling/cli/impl/release/UpdateReporterCommand.java
index 027eba9..0995b93 100644
--- a/src/main/java/org/apache/sling/cli/impl/release/UpdateReporterCommand.java
+++ b/src/main/java/org/apache/sling/cli/impl/release/UpdateReporterCommand.java
@@ -76,7 +76,7 @@
private ReusableCLIOptions reusableCLIOptions;
@Override
- public void run() {
+ public Integer call() {
try {
StagingRepository repository = repositoryService.find(repositoryId);
Set<Release> releases = repositoryService.getReleases(repository);
@@ -108,7 +108,9 @@
} catch (IOException e) {
LOGGER.error(String.format("Unable to update reporter service; passed command: %s.", repositoryId), e);
+ return CommandLine.ExitCode.SOFTWARE;
}
+ return CommandLine.ExitCode.OK;
}
diff --git a/src/main/java/org/apache/sling/cli/impl/release/VerifyReleasesCommand.java b/src/main/java/org/apache/sling/cli/impl/release/VerifyReleasesCommand.java
index d9614f2..a1c3397 100644
--- a/src/main/java/org/apache/sling/cli/impl/release/VerifyReleasesCommand.java
+++ b/src/main/java/org/apache/sling/cli/impl/release/VerifyReleasesCommand.java
@@ -69,7 +69,7 @@
private ReusableCLIOptions reusableCLIOptions;
@Override
- public void run() {
+ public Integer call() {
int checksRun = 0;
int failedChecks = 0;
try {
@@ -133,15 +133,21 @@
} catch (IOException e) {
LOGGER.error("Command execution failed.", e);
+ return CommandLine.ExitCode.SOFTWARE;
}
-
- LOGGER.info("\n\nRelease Summary: {}\n\n",
- failedChecks == 0 ? String.format("VALID (%d checks executed)", checksRun)
- : String.format("INVALID (%d of %d checks failed)", failedChecks, checksRun));
+ LOGGER.info("\n\nRelease Summary: {}\n\n");
+ if(failedChecks == 0){
+ LOGGER.info(String.format("VALID (%d checks executed)", checksRun));
+ return CommandLine.ExitCode.OK;
+ } else {
+ LOGGER.info(String.format("INVALID (%d of %d checks failed)", failedChecks, checksRun));
+ return CommandLine.ExitCode.USAGE;
+ }
}
private String getKeyUserId(PGPPublicKey key) {
Iterator<String> iterator = key.getUserIDs();
return iterator.hasNext() ? iterator.next() : "unknown";
}
+
}
diff --git a/src/test/java/org/apache/sling/cli/impl/release/PrepareVoteEmailCommandTest.java b/src/test/java/org/apache/sling/cli/impl/release/PrepareVoteEmailCommandTest.java
index 226f0f1..bb49314 100644
--- a/src/test/java/org/apache/sling/cli/impl/release/PrepareVoteEmailCommandTest.java
+++ b/src/test/java/org/apache/sling/cli/impl/release/PrepareVoteEmailCommandTest.java
@@ -42,6 +42,7 @@
import picocli.CommandLine;
+import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
@@ -71,7 +72,7 @@
ServiceReference<?> reference =
osgiContext.bundleContext().getServiceReference(Command.class.getName());
Command command = (Command) osgiContext.bundleContext().getService(reference);
- command.run();
+ assertEquals(CommandLine.ExitCode.OK, (int)command.call());
verify(mailer).send(
"From: John Doe <johndoe@apache.org>\n" +
"To: \"Sling Developers List\" <dev@sling.apache.org>\n" +
diff --git a/src/test/java/org/apache/sling/cli/impl/release/TallyVotesCommandTest.java b/src/test/java/org/apache/sling/cli/impl/release/TallyVotesCommandTest.java
index f9ff9a4..ea07a04 100644
--- a/src/test/java/org/apache/sling/cli/impl/release/TallyVotesCommandTest.java
+++ b/src/test/java/org/apache/sling/cli/impl/release/TallyVotesCommandTest.java
@@ -51,6 +51,9 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import picocli.CommandLine;
+
+import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
@@ -100,7 +103,7 @@
ServiceReference<?> reference =
osgiContext.bundleContext().getServiceReference(Command.class.getName());
Command command = (Command) osgiContext.bundleContext().getService(reference);
- command.run();
+ assertEquals(CommandLine.ExitCode.OK, (int)command.call());
verify(logger).info(
"From: John Doe <johndoe@apache.org>\n" +
"To: \"Sling Developers List\" <dev@sling.apache.org>\n" +
@@ -145,7 +148,7 @@
ServiceReference<?> reference =
osgiContext.bundleContext().getServiceReference(Command.class.getName());
Command command = (Command) osgiContext.bundleContext().getService(reference);
- command.run();
+ assertEquals(CommandLine.ExitCode.USAGE, (int)command.call());
verify(logger).info(
"Release {} does not have at least 3 binding votes.",
"Apache Sling CLI Test 1.0.0"
@@ -174,7 +177,7 @@
ServiceReference<?> reference =
osgiContext.bundleContext().getServiceReference(Command.class.getName());
Command command = (Command) osgiContext.bundleContext().getService(reference);
- command.run();
+ assertEquals(CommandLine.ExitCode.OK, (int)command.call());
verify(mailer).send(
"From: John Doe <johndoe@apache.org>\n" +
"To: \"Sling Developers List\" <dev@sling.apache.org>\n" +
diff --git a/src/test/java/org/apache/sling/cli/impl/release/UpdateReporterCommandTest.java b/src/test/java/org/apache/sling/cli/impl/release/UpdateReporterCommandTest.java
index 64863f1..d8f037e 100644
--- a/src/test/java/org/apache/sling/cli/impl/release/UpdateReporterCommandTest.java
+++ b/src/test/java/org/apache/sling/cli/impl/release/UpdateReporterCommandTest.java
@@ -44,6 +44,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
@@ -87,7 +88,7 @@
@Test
@PrepareForTest({LoggerFactory.class})
- public void testDryRun() {
+ public void testDryRun() throws Exception {
mockStatic(LoggerFactory.class);
Logger logger = mock(Logger.class);
when(LoggerFactory.getLogger(UpdateReporterCommand.class)).thenReturn(logger);
@@ -100,7 +101,7 @@
Command updateReporter = osgiContext.getService(Command.class);
assertTrue("Expected to retrieve the UpdateReporterCommand from the mocked OSGi environment.",
updateReporter instanceof UpdateReporterCommand);
- updateReporter.run();
+ assertEquals(0, (int)updateReporter.call());
verify(logger).info("The following {} would be added to the Apache Reporter System:", "releases");
verify(logger).info(" - {}", "Apache Sling CLI 1");
verify(logger).info(" - {}", "Apache Sling CLI 2");
@@ -128,7 +129,7 @@
when(response.getStatusLine()).thenReturn(statusLine);
when(statusLine.getStatusCode()).thenReturn(200);
when(client.execute(any())).thenReturn(response);
- updateReporter.run();
+ assertEquals(0, (int)updateReporter.call());
verify(client, times(2)).execute(any());
}
@@ -148,7 +149,7 @@
when(response.getStatusLine()).thenReturn(statusLine);
when(statusLine.getStatusCode()).thenReturn(200);
when(client.execute(any())).thenReturn(response);
- updateReporter.run();
+ assertEquals(0, (int)updateReporter.call());
verify(client, times(2)).execute(any());
}