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/13 16:05:03 UTC

[sling-org-apache-sling-committer-cli] branch master updated: SLING-10294: CLI Status Code (#8)

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

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


The following commit(s) were added to refs/heads/master by this push:
     new ecbe0d7  SLING-10294: CLI Status Code (#8)
ecbe0d7 is described below

commit ecbe0d7aef0c6a15a1f1a5b9bda144d18473801e
Author: Dan Klco <kl...@users.noreply.github.com>
AuthorDate: Tue Apr 13 12:04:56 2021 -0400

    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
---
 src/main/java/org/apache/sling/cli/impl/Command.java     |  4 +++-
 .../sling/cli/impl/release/CreateJiraVersionCommand.java |  4 +++-
 .../org/apache/sling/cli/impl/release/ListCommand.java   |  4 +++-
 .../sling/cli/impl/release/PrepareVoteEmailCommand.java  |  4 +++-
 .../cli/impl/release/ReleaseJiraVersionCommand.java      |  4 +++-
 .../apache/sling/cli/impl/release/TallyVotesCommand.java |  5 ++++-
 .../sling/cli/impl/release/UpdateLocalSiteCommand.java   |  4 +++-
 .../sling/cli/impl/release/UpdateReporterCommand.java    |  4 +++-
 .../sling/cli/impl/release/VerifyReleasesCommand.java    | 16 +++++++++++-----
 .../cli/impl/release/PrepareVoteEmailCommandTest.java    |  3 ++-
 .../sling/cli/impl/release/TallyVotesCommandTest.java    |  9 ++++++---
 .../cli/impl/release/UpdateReporterCommandTest.java      |  9 +++++----
 12 files changed, 49 insertions(+), 21 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..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 @@ public class CreateJiraVersionCommand implements Command {
     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 @@ public class CreateJiraVersionCommand implements Command {
             }
         } 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 @@ public class ListCommand implements Command {
     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 @@ public class PrepareVoteEmailCommand implements Command {
             "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 @@ public class PrepareVoteEmailCommand implements Command {
             }
         } 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 @@ public class ReleaseJiraVersionCommand implements Command {
     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 @@ public class ReleaseJiraVersionCommand implements Command {
             }
         } 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 @@ public class TallyVotesCommand implements Command {
     }
 
     @Override
-    public void run() {
+    public Integer call() {
         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 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 @@ public class UpdateLocalSiteCommand implements Command {
     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 @@ public class UpdateLocalSiteCommand implements Command {
             }
         } 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 @@ public class UpdateReporterCommand implements Command {
     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 @@ public class UpdateReporterCommand implements Command {
 
         } 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 @@ public class VerifyReleasesCommand implements Command {
     private ReusableCLIOptions reusableCLIOptions;
 
     @Override
-    public void run() {
+    public Integer call() {
         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 CommandLine.ExitCode.SOFTWARE;
+        }
+        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;
         }
-
-        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..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 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(CommandLine.ExitCode.OK, (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..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.powermock.reflect.Whitebox;
 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 @@ public class TallyVotesCommandTest {
         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 <jo...@apache.org>\n" +
                 "To: \"Sling Developers List\" <de...@sling.apache.org>\n" +
@@ -145,7 +148,7 @@ public class TallyVotesCommandTest {
         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 @@ public class TallyVotesCommandTest {
         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 <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());
     }