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

orpiske pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/main by this push:
     new 2936783  (chores) camel-jbang: code cleanups (#5869)
2936783 is described below

commit 2936783fbebbe970d43eafbbdf0e0eaa9275b869
Author: Otavio Rodolfo Piske <[email protected]>
AuthorDate: Tue Jul 27 19:02:23 2021 +0200

    (chores) camel-jbang: code cleanups (#5869)
    
    - fixed typo in exception
    - improve error handling for when the remote resource does not exist
    - remove unused code
---
 .../dsl/jbang/core/commands/AbstractSearch.java    | 19 ++------
 .../common/exceptions/ResourceAlreadyExists.java   |  2 +-
 ...lreadyExists.java => ResourceDoesNotExist.java} |  9 ++--
 .../camel-jbang-main/dist/CamelJBang.java          | 54 +++++++++++-----------
 .../src/main/jbang/main/CamelJBang.java            | 54 +++++++++++-----------
 5 files changed, 62 insertions(+), 76 deletions(-)

diff --git 
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/AbstractSearch.java
 
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/AbstractSearch.java
index 24a509d..3128e93 100644
--- 
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/AbstractSearch.java
+++ 
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/AbstractSearch.java
@@ -26,6 +26,7 @@ import java.util.regex.Pattern;
 
 import org.apache.camel.CamelContext;
 import org.apache.camel.dsl.jbang.core.api.Extractor;
+import org.apache.camel.dsl.jbang.core.common.exceptions.ResourceDoesNotExist;
 import org.apache.camel.github.GitHubResourceResolver;
 import org.apache.camel.main.KameletMain;
 import org.apache.camel.spi.Resource;
@@ -33,30 +34,20 @@ import org.apache.commons.io.IOUtils;
 
 public abstract class AbstractSearch {
     private String resourceLocation;
-    private Pattern pattern;
 
     // Only used for the search subcommand
     protected AbstractSearch() {
     }
 
-    public AbstractSearch(Pattern pattern) {
-        this.pattern = pattern;
-    }
-
     public AbstractSearch(String resourceLocation, Pattern pattern) {
         this.resourceLocation = resourceLocation;
-        this.pattern = pattern;
     }
 
     protected void setResourceLocation(String baseResourceLocation, String 
resourcePath) {
         this.resourceLocation = baseResourceLocation + ":" + resourcePath;
     }
 
-    protected void setPattern(Pattern pattern) {
-        this.pattern = pattern;
-    }
-
-    protected void downloadResource(File indexFile) throws Exception {
+    protected void downloadResource(File indexFile) throws 
ResourceDoesNotExist, IOException {
         KameletMain main = new KameletMain();
         main.start();
         CamelContext context = main.getCamelContext();
@@ -67,7 +58,7 @@ public abstract class AbstractSearch {
             Resource resource = resolver.resolve(resourceLocation);
 
             if (!resource.exists()) {
-                throw new Exception("The resource does not exist");
+                throw new ResourceDoesNotExist(resource);
             }
 
             try (FileOutputStream fo = new FileOutputStream(indexFile)) {
@@ -96,7 +87,7 @@ public abstract class AbstractSearch {
 
     public abstract void printHeader();
 
-    public void search(Extractor extractor) throws Exception {
+    public void search(Extractor extractor) throws ResourceDoesNotExist, 
IOException {
         File indexFile = getIndexFile();
 
         printHeader();
@@ -104,7 +95,7 @@ public abstract class AbstractSearch {
         readFileByLine(indexFile, extractor);
     }
 
-    private File getIndexFile() throws Exception {
+    private File getIndexFile() throws ResourceDoesNotExist, IOException {
         File indexFile = new File("index");
         indexFile.deleteOnExit();
 
diff --git 
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/common/exceptions/ResourceAlreadyExists.java
 
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/common/exceptions/ResourceAlreadyExists.java
index a5def19..7cdd3bb 100644
--- 
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/common/exceptions/ResourceAlreadyExists.java
+++ 
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/common/exceptions/ResourceAlreadyExists.java
@@ -23,6 +23,6 @@ import org.apache.camel.CamelException;
 
 public class ResourceAlreadyExists extends CamelException {
     public ResourceAlreadyExists(File resource) {
-        super("The destination file already exits: " + 
resource.getAbsolutePath());
+        super("The destination file already exists: " + 
resource.getAbsolutePath());
     }
 }
diff --git 
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/common/exceptions/ResourceAlreadyExists.java
 
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/common/exceptions/ResourceDoesNotExist.java
similarity index 80%
copy from 
dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/common/exceptions/ResourceAlreadyExists.java
copy to 
dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/common/exceptions/ResourceDoesNotExist.java
index a5def19..c996bb1 100644
--- 
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/common/exceptions/ResourceAlreadyExists.java
+++ 
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/common/exceptions/ResourceDoesNotExist.java
@@ -17,12 +17,11 @@
 
 package org.apache.camel.dsl.jbang.core.common.exceptions;
 
-import java.io.File;
-
 import org.apache.camel.CamelException;
+import org.apache.camel.spi.Resource;
 
-public class ResourceAlreadyExists extends CamelException {
-    public ResourceAlreadyExists(File resource) {
-        super("The destination file already exits: " + 
resource.getAbsolutePath());
+public class ResourceDoesNotExist extends CamelException {
+    public ResourceDoesNotExist(Resource resource) {
+        super("The resource does not exist at " + resource.getLocation());
     }
 }
diff --git a/dsl/camel-jbang/camel-jbang-main/dist/CamelJBang.java 
b/dsl/camel-jbang/camel-jbang-main/dist/CamelJBang.java
index 1119563..4c048ad 100755
--- a/dsl/camel-jbang/camel-jbang-main/dist/CamelJBang.java
+++ b/dsl/camel-jbang/camel-jbang-main/dist/CamelJBang.java
@@ -48,6 +48,7 @@ import 
org.apache.camel.dsl.jbang.core.commands.AbstractInitKamelet;
 import org.apache.camel.dsl.jbang.core.commands.AbstractSearch;
 import org.apache.camel.dsl.jbang.core.common.MatchExtractor;
 import org.apache.camel.dsl.jbang.core.common.exceptions.ResourceAlreadyExists;
+import org.apache.camel.dsl.jbang.core.common.exceptions.ResourceDoesNotExist;
 import org.apache.camel.dsl.jbang.core.components.ComponentConverter;
 import org.apache.camel.dsl.jbang.core.components.ComponentDescriptionMatching;
 import org.apache.camel.dsl.jbang.core.components.ComponentPrinter;
@@ -127,7 +128,7 @@ class Run implements Callable<Integer> {
         for (File lockFile : lockFiles) {
             System.out.println("Removing file " + lockFile);
             if (!lockFile.delete()) {
-                System.out.println("Failed to remove lock file " + lockFile);
+                System.err.println("Failed to remove lock file " + lockFile);
             }
         }
 
@@ -202,6 +203,7 @@ class Search extends AbstractSearch implements 
Callable<Integer> {
     }
 
     public void printHeader() {
+        // NO-OP
     }
 
     @Override
@@ -233,10 +235,6 @@ class SearchKamelets extends AbstractSearch implements 
Callable<Integer> {
             description = "Where to download the resources from")
     private String resourceLocation;
 
-    SearchKamelets() {
-        super(PATTERN);
-    }
-
     @Override
     public void printHeader() {
         System.out.printf("%-35s %-45s %s%n", "KAMELET", "DESCRIPTION", 
"LINK");
@@ -251,16 +249,14 @@ class SearchKamelets extends AbstractSearch implements 
Callable<Integer> {
 
         if (searchTerm.isEmpty()) {
             matchExtractor = new MatchExtractor<>(PATTERN, new 
KameletConverter(), new KameletPrinter());
-
-            search(matchExtractor);
         } else {
             matchExtractor = new MatchExtractor<>(
                     PATTERN, new KameletConverter(),
                     new KameletDescriptionMatching(searchTerm));
-
-            search(matchExtractor);
         }
 
+        search(matchExtractor);
+
         return 0;
     }
 }
@@ -286,10 +282,6 @@ class SearchComponents extends AbstractSearch implements 
Callable<Integer> {
             description = "Where to download the resources from")
     private String resourceLocation;
 
-    SearchComponents() {
-        super(PATTERN);
-    }
-
     @Override
     public void printHeader() {
         System.out.printf("%-35s %-45s %s%n", "COMPONENT", "DESCRIPTION", 
"LINK");
@@ -310,9 +302,14 @@ class SearchComponents extends AbstractSearch implements 
Callable<Integer> {
                     new ComponentDescriptionMatching(searchTerm));
 
         }
-        search(matchExtractor);
 
-        return 0;
+        try {
+            search(matchExtractor);
+            return 0;
+        } catch (ResourceDoesNotExist e) {
+            System.err.println(e.getMessage());
+            return 1;
+        }
     }
 }
 
@@ -336,10 +333,6 @@ class SearchLanguages extends AbstractSearch implements 
Callable<Integer> {
             description = "Where to download the resources from")
     private String resourceLocation;
 
-    SearchLanguages() {
-        super(PATTERN);
-    }
-
     @Override
     public void printHeader() {
         System.out.printf("%-35s %-45s %s%n", "LANGUAGE", "DESCRIPTION", 
"LINK");
@@ -360,9 +353,13 @@ class SearchLanguages extends AbstractSearch implements 
Callable<Integer> {
                     new LanguageDescriptionMatching(searchTerm));
 
         }
-        search(matchExtractor);
-
-        return 0;
+        try {
+            search(matchExtractor);
+            return 0;
+        } catch (ResourceDoesNotExist e) {
+            System.err.println(e.getMessage());
+            return 1;
+        }
     }
 }
 
@@ -386,10 +383,6 @@ class SearchOthers extends AbstractSearch implements 
Callable<Integer> {
             description = "Where to download the resources from")
     private String resourceLocation;
 
-    SearchOthers() {
-        super(PATTERN);
-    }
-
     @Override
     public void printHeader() {
         System.out.printf("%-35s %-45s %s%n", "COMPONENT", "DESCRIPTION", 
"LINK");
@@ -410,9 +403,14 @@ class SearchOthers extends AbstractSearch implements 
Callable<Integer> {
                     new OtherDescriptionMatching(searchTerm));
 
         }
-        search(matchExtractor);
 
-        return 0;
+        try {
+            search(matchExtractor);
+            return 0;
+        } catch (ResourceDoesNotExist e) {
+            System.err.println(e.getMessage());
+            return 1;
+        }
     }
 }
 
diff --git 
a/dsl/camel-jbang/camel-jbang-main/src/main/jbang/main/CamelJBang.java 
b/dsl/camel-jbang/camel-jbang-main/src/main/jbang/main/CamelJBang.java
index 8da4635..09dbe3e 100755
--- a/dsl/camel-jbang/camel-jbang-main/src/main/jbang/main/CamelJBang.java
+++ b/dsl/camel-jbang/camel-jbang-main/src/main/jbang/main/CamelJBang.java
@@ -48,6 +48,7 @@ import 
org.apache.camel.dsl.jbang.core.commands.AbstractInitKamelet;
 import org.apache.camel.dsl.jbang.core.commands.AbstractSearch;
 import org.apache.camel.dsl.jbang.core.common.MatchExtractor;
 import org.apache.camel.dsl.jbang.core.common.exceptions.ResourceAlreadyExists;
+import org.apache.camel.dsl.jbang.core.common.exceptions.ResourceDoesNotExist;
 import org.apache.camel.dsl.jbang.core.components.ComponentConverter;
 import org.apache.camel.dsl.jbang.core.components.ComponentDescriptionMatching;
 import org.apache.camel.dsl.jbang.core.components.ComponentPrinter;
@@ -127,7 +128,7 @@ class Run implements Callable<Integer> {
         for (File lockFile : lockFiles) {
             System.out.println("Removing file " + lockFile);
             if (!lockFile.delete()) {
-                System.out.println("Failed to remove lock file " + lockFile);
+                System.err.println("Failed to remove lock file " + lockFile);
             }
         }
 
@@ -202,6 +203,7 @@ class Search extends AbstractSearch implements 
Callable<Integer> {
     }
 
     public void printHeader() {
+        // NO-OP
     }
 
     @Override
@@ -233,10 +235,6 @@ class SearchKamelets extends AbstractSearch implements 
Callable<Integer> {
             description = "Where to download the resources from")
     private String resourceLocation;
 
-    SearchKamelets() {
-        super(PATTERN);
-    }
-
     @Override
     public void printHeader() {
         System.out.printf("%-35s %-45s %s%n", "KAMELET", "DESCRIPTION", 
"LINK");
@@ -251,16 +249,14 @@ class SearchKamelets extends AbstractSearch implements 
Callable<Integer> {
 
         if (searchTerm.isEmpty()) {
             matchExtractor = new MatchExtractor<>(PATTERN, new 
KameletConverter(), new KameletPrinter());
-
-            search(matchExtractor);
         } else {
             matchExtractor = new MatchExtractor<>(
                     PATTERN, new KameletConverter(),
                     new KameletDescriptionMatching(searchTerm));
-
-            search(matchExtractor);
         }
 
+        search(matchExtractor);
+
         return 0;
     }
 }
@@ -286,10 +282,6 @@ class SearchComponents extends AbstractSearch implements 
Callable<Integer> {
             description = "Where to download the resources from")
     private String resourceLocation;
 
-    SearchComponents() {
-        super(PATTERN);
-    }
-
     @Override
     public void printHeader() {
         System.out.printf("%-35s %-45s %s%n", "COMPONENT", "DESCRIPTION", 
"LINK");
@@ -310,9 +302,14 @@ class SearchComponents extends AbstractSearch implements 
Callable<Integer> {
                     new ComponentDescriptionMatching(searchTerm));
 
         }
-        search(matchExtractor);
 
-        return 0;
+        try {
+            search(matchExtractor);
+            return 0;
+        } catch (ResourceDoesNotExist e) {
+            System.err.println(e.getMessage());
+            return 1;
+        }
     }
 }
 
@@ -336,10 +333,6 @@ class SearchLanguages extends AbstractSearch implements 
Callable<Integer> {
             description = "Where to download the resources from")
     private String resourceLocation;
 
-    SearchLanguages() {
-        super(PATTERN);
-    }
-
     @Override
     public void printHeader() {
         System.out.printf("%-35s %-45s %s%n", "LANGUAGE", "DESCRIPTION", 
"LINK");
@@ -360,9 +353,13 @@ class SearchLanguages extends AbstractSearch implements 
Callable<Integer> {
                     new LanguageDescriptionMatching(searchTerm));
 
         }
-        search(matchExtractor);
-
-        return 0;
+        try {
+            search(matchExtractor);
+            return 0;
+        } catch (ResourceDoesNotExist e) {
+            System.err.println(e.getMessage());
+            return 1;
+        }
     }
 }
 
@@ -386,10 +383,6 @@ class SearchOthers extends AbstractSearch implements 
Callable<Integer> {
             description = "Where to download the resources from")
     private String resourceLocation;
 
-    SearchOthers() {
-        super(PATTERN);
-    }
-
     @Override
     public void printHeader() {
         System.out.printf("%-35s %-45s %s%n", "COMPONENT", "DESCRIPTION", 
"LINK");
@@ -410,9 +403,14 @@ class SearchOthers extends AbstractSearch implements 
Callable<Integer> {
                     new OtherDescriptionMatching(searchTerm));
 
         }
-        search(matchExtractor);
 
-        return 0;
+        try {
+            search(matchExtractor);
+            return 0;
+        } catch (ResourceDoesNotExist e) {
+            System.err.println(e.getMessage());
+            return 1;
+        }
     }
 }
 

Reply via email to