You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by dk...@apache.org on 2021/04/12 12:57:37 UTC

[sling-org-apache-sling-committer-cli] 01/01: SLING-10294: Updating the commands to return a status code to the shell

This is an automated email from the ASF dual-hosted git repository.

dklco pushed a commit to branch SLING-10294-status-code
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-committer-cli.git

commit 4cd5b08bb6b7e54a0d812bb011608369d40798db
Author: Dan Klco <kl...@adobe.com>
AuthorDate: Mon Apr 12 08:57:21 2021 -0400

    SLING-10294: Updating the commands to return a status code to the shell
---
 src/main/java/org/apache/sling/cli/impl/Command.java   |  4 +++-
 .../cli/impl/release/CreateJiraVersionCommand.java     |  6 ++++--
 .../org/apache/sling/cli/impl/release/ListCommand.java |  6 ++++--
 .../cli/impl/release/PrepareVoteEmailCommand.java      |  4 +++-
 .../cli/impl/release/ReleaseJiraVersionCommand.java    |  6 ++++--
 .../sling/cli/impl/release/TallyVotesCommand.java      |  7 +++++--
 .../sling/cli/impl/release/UpdateLocalSiteCommand.java |  6 ++++--
 .../sling/cli/impl/release/UpdateReporterCommand.java  |  6 ++++--
 .../sling/cli/impl/release/VerifyReleasesCommand.java  | 18 ++++++++++++------
 .../cli/impl/release/PrepareVoteEmailCommandTest.java  |  3 ++-
 .../sling/cli/impl/release/TallyVotesCommandTest.java  |  7 ++++---
 .../cli/impl/release/UpdateReporterCommandTest.java    |  9 +++++----
 12 files changed, 54 insertions(+), 28 deletions(-)

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..884d032 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
@@ -66,8 +66,8 @@ public class CreateJiraVersionCommand implements Command {
 
     private final Logger logger = LoggerFactory.getLogger(getClass());
 
-    @Override
-    public void run() {
+	@Override
+	public Integer call() throws Exception {
         try {
             StagingRepository repo = repositoryService.find(repositoryId);
             for (Release release : repositoryService.getReleases(repo)) {
@@ -122,6 +122,8 @@ public class CreateJiraVersionCommand implements Command {
             }
         } catch (IOException e) {
             logger.warn("Failed executing command", e);
+            return 1;
         }
+        return 0;
     }
 }
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..45f29e6 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
@@ -48,12 +48,14 @@ public class ListCommand implements Command {
     @Reference
     private RepositoryService repositoryService;
 
-    @Override
-    public void run() {
+	@Override
+	public Integer call() throws Exception {
         try {
             repositoryService.list().forEach( r -> logger.info("{}\t{}", r.getRepositoryId(), cleanupNewlines(r.getDescription())));
+            return 0;
         } catch (IOException e) {
             logger.warn("Failed executing command", e);
+            return 1;
         }
     }
 }
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..5242b9d 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 @@ public class PrepareVoteEmailCommand implements Command {
             "https://issues.apache.org/jira/browse/SLING/fixforversion/##VERSION_ID##";
 
     @Override
-    public void run() {
+    public Integer call() throws Exception {
         try {
             CommandLine commandLine = spec.commandLine();
             if (commandLine.isUsageHelpRequested()) {
@@ -171,6 +171,8 @@ public class PrepareVoteEmailCommand implements Command {
             }
         } catch (IOException e) {
             LOGGER.warn("Failed executing command", e);
+            return 1;
         }
+        return 0;
     }
 }
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..7106fa5 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
@@ -68,8 +68,8 @@ public class ReleaseJiraVersionCommand implements Command {
     @CommandLine.Mixin
     private ReusableCLIOptions reusableCLIOptions;
 
-    @Override
-    public void run() {
+	@Override
+	public Integer call() throws Exception {
         try {
             StagingRepository repo = repositoryService.find(repositoryId);
             Set<Release> releases = repositoryService.getReleases(repo);
@@ -100,6 +100,8 @@ public class ReleaseJiraVersionCommand implements Command {
             }
         } catch (Exception e) {
             LOGGER.warn("Failed executing command.", e);
+            return 1;
         }
+        return 0;
     }
 }
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..4ed053d 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
@@ -95,8 +95,8 @@ public class TallyVotesCommand implements Command {
         }
     }
 
-    @Override
-    public void run() {
+	@Override
+	public Integer call() throws Exception {
         try {
             StagingRepository repository = repositoryService.find(repositoryId);
             Set<Release> releases = repositoryService.getReleases(repository);
@@ -167,12 +167,15 @@ public class TallyVotesCommand implements Command {
                 } else {
                     LOGGER.info("Release {} does not have at least 3 binding votes.", releaseFullName);
                     LOGGER.info("Binding votes: {}.", bindingVoters.isEmpty() ? "none" : String.join(", ", bindingVoters));
+                    return 2;
                 }
             }
             
         } catch (IOException e) {
             LOGGER.warn("Command execution failed", e);
+            return 1;
         }
+        return 0;
     }
 
     // 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..2d8078f 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
@@ -61,8 +61,8 @@ public class UpdateLocalSiteCommand implements Command {
     @CommandLine.Option(names = {"-r", "--repository"}, description = "Nexus repository id", required = true)
     private Integer repositoryId;
 
-    @Override
-    public void run() {
+	@Override
+	public Integer call() throws Exception {
         try {
             ensureRepo();
             try ( Git git = Git.open(new File(GIT_CHECKOUT)) ) {
@@ -86,7 +86,9 @@ public class UpdateLocalSiteCommand implements Command {
             }
         } catch (GitAPIException | IOException e) {
             logger.warn("Failed executing command", e);
+            return 1;
         }
+        return 0;
             
     }
 
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..09511d6 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
@@ -75,8 +75,8 @@ public class UpdateReporterCommand implements Command {
     @CommandLine.Mixin
     private ReusableCLIOptions reusableCLIOptions;
 
-    @Override
-    public void run() {
+	@Override
+	public Integer call() throws Exception {
         try {
             StagingRepository repository = repositoryService.find(repositoryId);
             Set<Release> releases = repositoryService.getReleases(repository);
@@ -108,7 +108,9 @@ public class UpdateReporterCommand implements Command {
 
         } catch (IOException e) {
             LOGGER.error(String.format("Unable to update reporter service; passed command: %s.", repositoryId), e);
+            return 1;
         }
+        return 0;
 
     }
 
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..e028994 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
@@ -68,8 +68,8 @@ public class VerifyReleasesCommand implements Command {
     @CommandLine.Mixin
     private ReusableCLIOptions reusableCLIOptions;
 
-    @Override
-    public void run() {
+	@Override
+	public Integer call() throws Exception {
         int checksRun = 0;
         int failedChecks = 0;
         try {
@@ -133,15 +133,21 @@ public class VerifyReleasesCommand implements Command {
 
         } catch (IOException e) {
             LOGGER.error("Command execution failed.", e);
+            return 1;
+        }
+        LOGGER.info("\n\nRelease Summary: {}\n\n");
+        if(failedChecks == 0){
+            LOGGER.info(String.format("VALID (%d checks executed)", checksRun));
+            return 0;
+        } else {
+            LOGGER.info(String.format("INVALID (%d of %d checks failed)", failedChecks, checksRun));
+            return 2;
         }
-
-        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));
     }
 
     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..6f75cc4 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 org.powermock.reflect.Whitebox;
 
 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 @@ public class PrepareVoteEmailCommandTest {
         ServiceReference<?> reference =
                 osgiContext.bundleContext().getServiceReference(Command.class.getName());
         Command command = (Command) osgiContext.bundleContext().getService(reference);
-        command.run();
+        assertEquals(0, (int)command.call());
         verify(mailer).send(
                 "From: John Doe <jo...@apache.org>\n" +
                         "To: \"Sling Developers List\" <de...@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..4ec378e 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,7 @@ import org.powermock.reflect.Whitebox;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+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 +101,7 @@ public class TallyVotesCommandTest {
         ServiceReference<?> reference =
                 osgiContext.bundleContext().getServiceReference(Command.class.getName());
         Command command = (Command) osgiContext.bundleContext().getService(reference);
-        command.run();
+        assertEquals(0, (int)command.call());
         verify(logger).info(
                 "From: John Doe <jo...@apache.org>\n" +
                 "To: \"Sling Developers List\" <de...@sling.apache.org>\n" +
@@ -145,7 +146,7 @@ public class TallyVotesCommandTest {
         ServiceReference<?> reference =
                 osgiContext.bundleContext().getServiceReference(Command.class.getName());
         Command command = (Command) osgiContext.bundleContext().getService(reference);
-        command.run();
+        assertEquals(2, (int)command.call());
         verify(logger).info(
                 "Release {} does not have at least 3 binding votes.",
                 "Apache Sling CLI Test 1.0.0"
@@ -174,7 +175,7 @@ public class TallyVotesCommandTest {
         ServiceReference<?> reference =
                 osgiContext.bundleContext().getServiceReference(Command.class.getName());
         Command command = (Command) osgiContext.bundleContext().getService(reference);
-        command.run();
+        assertEquals(0, (int)command.call());
         verify(mailer).send(
                 "From: John Doe <jo...@apache.org>\n" +
                         "To: \"Sling Developers List\" <de...@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.powermock.reflect.Whitebox;
 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 @@ public class UpdateReporterCommandTest {
 
     @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 @@ public class UpdateReporterCommandTest {
         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 @@ public class UpdateReporterCommandTest {
         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 @@ public class UpdateReporterCommandTest {
         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());
     }