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

davsclaus pushed a commit to branch run-example-fix
in repository https://gitbox.apache.org/repos/asf/camel.git

commit fffc0db702a583c6a9ef476ad1b7c8e1ea98a2bb
Author: Claus Ibsen <[email protected]>
AuthorDate: Fri May 29 09:45:44 2026 +0200

    camel-jbang - Download GitHub examples to local temp dir before running
    
    Co-Authored-By: Claude <[email protected]>
---
 .../apache/camel/dsl/jbang/core/commands/Run.java  | 33 ++++++----------------
 .../camel/dsl/jbang/core/common/ExampleHelper.java | 31 ++++++++++++++++++++
 2 files changed, 39 insertions(+), 25 deletions(-)

diff --git 
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Run.java
 
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Run.java
index 0068b0c10523..96ccca6b7ede 100644
--- 
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Run.java
+++ 
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Run.java
@@ -482,48 +482,31 @@ public class Run extends CamelCommand {
 
     private int runGithubExample(JsonObject entry) throws Exception {
         String eName = entry.getString("name");
-        String url = ExampleHelper.getGithubUrl(entry);
+        List<String> exampleFiles = ExampleHelper.getFiles(entry);
 
         printer().println("Fetching example from GitHub: " + eName);
         if (ExampleHelper.requiresDocker(entry)) {
             printer().println("Note: this example requires Docker/Podman");
         }
 
-        StringJoiner routes = new StringJoiner(",");
-        StringJoiner kamelets = new StringJoiner(",");
-        StringJoiner properties = new StringJoiner(",");
+        Path tempDir;
         try {
-            fetchGithubUrls(url, routes, kamelets, properties);
+            tempDir = ExampleHelper.downloadGithubExample(entry);
         } catch (Exception e) {
             printer().printErr("Failed to fetch example from GitHub: " + 
e.getMessage());
             printer().printErr("This example requires an internet 
connection.");
             return 1;
         }
 
-        if (routes.length() == 0 && kamelets.length() == 0 && 
properties.length() == 0) {
-            printer().printErr("No files found for example: " + eName);
-            return 1;
-        }
-
-        if (routes.length() > 0) {
-            for (String r : routes.toString().split(",")) {
-                files.add(r);
-            }
-        }
-        if (kamelets.length() > 0) {
-            for (String k : kamelets.toString().split(",")) {
-                files.add(k);
-            }
-        }
-        if (properties.length() > 0) {
-            for (String p : properties.toString().split(",")) {
-                files.add(p);
-            }
+        for (String f : exampleFiles) {
+            files.add(tempDir.resolve(f).toString());
         }
         if ("CamelJBang".equals(name)) {
             name = eName;
         }
 
+        exportBaseDir = tempDir;
+
         if (!exportRun) {
             printConfigurationValues("Running integration with the following 
configuration:");
         }
@@ -1137,7 +1120,7 @@ public class Run extends CamelCommand {
             });
             StringBuilder locations = new StringBuilder();
             for (String file : names) {
-                if (!file.startsWith("file:")) {
+                if (!file.startsWith("file:") && !file.startsWith("github:") 
&& !file.startsWith("gist:")) {
                     if (!file.startsWith("/")) {
                         file = 
Paths.get(FileSystems.getDefault().getPath("").toAbsolutePath().toString(), 
file).toString();
                     }
diff --git 
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/common/ExampleHelper.java
 
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/common/ExampleHelper.java
index faf401146db3..a48146e74599 100644
--- 
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/common/ExampleHelper.java
+++ 
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/common/ExampleHelper.java
@@ -17,8 +17,13 @@
 package org.apache.camel.dsl.jbang.core.common;
 
 import java.io.InputStream;
+import java.net.URI;
+import java.net.http.HttpClient;
+import java.net.http.HttpRequest;
+import java.net.http.HttpResponse;
 import java.nio.file.Files;
 import java.nio.file.Path;
+import java.time.Duration;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.List;
@@ -33,6 +38,8 @@ public final class ExampleHelper {
     private static final String CATALOG_RESOURCE = 
"examples/camel-jbang-example-catalog.json";
     private static final String GITHUB_EXAMPLES_URL
             = "https://github.com/apache/camel-jbang-examples/tree/main/";;
+    private static final String GITHUB_RAW_URL
+            = 
"https://raw.githubusercontent.com/apache/camel-jbang-examples/main/%s/%s";;
 
     private ExampleHelper() {
     }
@@ -162,6 +169,30 @@ public final class ExampleHelper {
         return tempDir;
     }
 
+    public static Path downloadGithubExample(JsonObject entry) throws 
Exception {
+        String name = entry.getString("name");
+        List<String> fileNames = getFiles(entry);
+        Path tempDir = Files.createTempDirectory("camel-example-");
+
+        HttpClient hc = HttpClient.newHttpClient();
+        for (String fileName : fileNames) {
+            String rawUrl = String.format(GITHUB_RAW_URL, name, fileName);
+            HttpResponse<String> res = hc.send(
+                    HttpRequest.newBuilder(new 
URI(rawUrl)).timeout(Duration.ofSeconds(20)).build(),
+                    HttpResponse.BodyHandlers.ofString());
+            if (res.statusCode() == 200) {
+                Path targetFile = tempDir.resolve(fileName);
+                Files.createDirectories(targetFile.getParent());
+                Files.writeString(targetFile, res.body());
+                targetFile.toFile().deleteOnExit();
+                targetFile.getParent().toFile().deleteOnExit();
+            }
+        }
+
+        tempDir.toFile().deleteOnExit();
+        return tempDir;
+    }
+
     public static String getGithubUrl(JsonObject entry) {
         return GITHUB_EXAMPLES_URL + entry.getString("name");
     }

Reply via email to