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

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

commit 8e3b7246bd525bedfe1fc6c09f643af7ec2c2753
Author: Claus Ibsen <claus.ib...@gmail.com>
AuthorDate: Sun May 8 08:08:21 2022 +0200

    CAMEL-18077: camel-jbang - Run from gist
---
 .../modules/ROOT/pages/camel-jbang.adoc            | 16 +++++++++++
 .../camel/dsl/jbang/core/commands/GistHelper.java  |  4 +++
 .../apache/camel/dsl/jbang/core/commands/Init.java | 33 +++++++++++++++++++++-
 3 files changed, 52 insertions(+), 1 deletion(-)

diff --git a/docs/user-manual/modules/ROOT/pages/camel-jbang.adoc 
b/docs/user-manual/modules/ROOT/pages/camel-jbang.adoc
index 2d6cf4e818c..0d8914836f6 100644
--- a/docs/user-manual/modules/ROOT/pages/camel-jbang.adoc
+++ b/docs/user-manual/modules/ROOT/pages/camel-jbang.adoc
@@ -215,6 +215,22 @@ You can also use Camel JBang to try local Kamelets, 
without the need to publish
 camel run --local-kamelet-dir=/path/to/local/kamelets earthquake.yaml
 ----
 
+==== Downloading routes form GitHub gists
+
+You can also download files from gists easily as shown:
+
+[source,bash]
+----
+camel init https://gist.github.com/davsclaus/477ddff5cdeb1ae03619aa544ce47e92
+----
+
+This will then download the files to local disk, which you can run afterwards:
+
+[source,bash]
+----
+camel run *
+----
+
 === Running Camel K integrations or bindings
 
 Camel also supports running Camel K integrations and binding files, which are 
in CRD format (Kubernetes Custom Resource Definitions).
diff --git 
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/GistHelper.java
 
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/GistHelper.java
index bba81f4cc16..33dd9d598f0 100644
--- 
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/GistHelper.java
+++ 
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/GistHelper.java
@@ -47,6 +47,10 @@ public final class GistHelper {
         return "gist:" + url;
     }
 
+    public static void fetchGistUrls(String url, StringJoiner all) throws 
Exception {
+        doFetchGistUrls(url, null, null, null, all);
+    }
+
     public static void fetchGistUrls(String url, StringJoiner routes, 
StringJoiner kamelets, StringJoiner properties)
             throws Exception {
         doFetchGistUrls(url, routes, kamelets, properties, null);
diff --git 
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Init.java
 
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Init.java
index 1445ed1052d..11d3d1a487f 100644
--- 
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Init.java
+++ 
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Init.java
@@ -23,6 +23,7 @@ import java.util.concurrent.Callable;
 
 import org.apache.camel.CamelContext;
 import org.apache.camel.dsl.jbang.core.common.exceptions.ResourceDoesNotExist;
+import org.apache.camel.github.GistResourceResolver;
 import org.apache.camel.github.GitHubResourceResolver;
 import org.apache.camel.impl.lw.LightweightCamelContext;
 import org.apache.camel.spi.Resource;
@@ -33,6 +34,7 @@ import picocli.CommandLine;
 import picocli.CommandLine.Command;
 import picocli.CommandLine.Option;
 
+import static 
org.apache.camel.dsl.jbang.core.commands.GistHelper.fetchGistUrls;
 import static 
org.apache.camel.dsl.jbang.core.commands.GitHubHelper.asGithubSingleUrl;
 import static 
org.apache.camel.dsl.jbang.core.commands.GitHubHelper.fetchGithubUrls;
 
@@ -54,10 +56,12 @@ class Init implements Callable<Integer> {
     @Override
     public Integer call() throws Exception {
 
-        // is the file referring to an existing file on github
+        // is the file referring to an existing file on github/gist
         // then we should download the file to local for use
         if (file.startsWith("https://github.com/";)) {
             return downloadFromGithub();
+        } else if (file.startsWith("https://gist.github.com/";)) {
+            return downloadFromGist();
         }
 
         String ext = FileUtil.onlyExt(file, false);
@@ -114,4 +118,31 @@ class Init implements Callable<Integer> {
         return 0;
     }
 
+    private Integer downloadFromGist() throws Exception {
+        StringJoiner all = new StringJoiner(",");
+
+        fetchGistUrls(file, all);
+
+        if (all.length() > 0) {
+            CamelContext tiny = new LightweightCamelContext();
+            GistResourceResolver resolver = new GistResourceResolver();
+            resolver.setCamelContext(tiny);
+            for (String u : all.toString().split(",")) {
+                Resource resource = resolver.resolve(u);
+                if (!resource.exists()) {
+                    throw new ResourceDoesNotExist(resource);
+                }
+
+                String loc = resource.getLocation();
+                String name = FileUtil.stripPath(loc);
+
+                try (FileOutputStream fo = new FileOutputStream(name)) {
+                    IOUtils.copy(resource.getInputStream(), fo);
+                }
+            }
+        }
+
+        return 0;
+    }
+
 }

Reply via email to