You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2022/05/08 06:09:25 UTC

[camel] branch main updated (b8749028c3c -> 8e3b7246bd5)

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

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


    from b8749028c3c CAMEL-18055: enqueuedSequenceNumber is of type long
     new b70c82f9742 CAMEL-18077: camel-github - Add gist resource resolver
     new b929b1b6986 CAMEL-18077: camel-jbang - Run from gist
     new cccc1dba599 CAMEL-18077: camel-jbang - Run from gist
     new 8e3b7246bd5 CAMEL-18077: camel-jbang - Run from gist

The 4 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../org/apache/camel/resource-resolver/gist}       |   2 +-
 .../src/main/docs/resourceresolver-github.adoc     |  19 ++++
 .../{GitHubResource.java => GistResource.java}     |   6 +-
 ...urceResolver.java => GistResourceResolver.java} |  56 ++++------
 .../camel/github/GistResourceResolverTest.java}    |  28 ++---
 .../modules/ROOT/pages/camel-jbang.adoc            |  30 +++++
 .../camel/dsl/jbang/core/commands/GistHelper.java  | 123 +++++++++++++++++++++
 .../apache/camel/dsl/jbang/core/commands/Init.java |  33 +++++-
 .../apache/camel/dsl/jbang/core/commands/Run.java  |  25 +++++
 9 files changed, 264 insertions(+), 58 deletions(-)
 copy components/{camel-as2/camel-as2-component/src/generated/resources/META-INF/services/org/apache/camel/component/as2 => camel-resourceresolver-github/src/generated/resources/META-INF/services/org/apache/camel/resource-resolver/gist} (53%)
 copy components/camel-resourceresolver-github/src/main/java/org/apache/camel/github/{GitHubResource.java => GistResource.java} (91%)
 copy components/camel-resourceresolver-github/src/main/java/org/apache/camel/github/{GitHubResourceResolver.java => GistResourceResolver.java} (50%)
 copy components/{camel-spring-main/src/test/java/org/apache/camel/spring/MainDummyTest.java => camel-resourceresolver-github/src/test/java/org/apache/camel/github/GistResourceResolverTest.java} (58%)
 create mode 100644 dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/GistHelper.java


[camel] 01/04: CAMEL-18077: camel-github - Add gist resource resolver

Posted by da...@apache.org.
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 b70c82f974247695e06fc6f4044c18d76861bed4
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Sat May 7 10:15:59 2022 +0200

    CAMEL-18077: camel-github - Add gist resource resolver
---
 .../org/apache/camel/resource-resolver/gist        |  2 +
 .../java/org/apache/camel/github/GistResource.java | 62 ++++++++++++++++++++
 .../apache/camel/github/GistResourceResolver.java  | 66 ++++++++++++++++++++++
 .../camel/github/GistResourceResolverTest.java     | 39 +++++++++++++
 4 files changed, 169 insertions(+)

diff --git a/components/camel-resourceresolver-github/src/generated/resources/META-INF/services/org/apache/camel/resource-resolver/gist b/components/camel-resourceresolver-github/src/generated/resources/META-INF/services/org/apache/camel/resource-resolver/gist
new file mode 100644
index 00000000000..51ad8664f7b
--- /dev/null
+++ b/components/camel-resourceresolver-github/src/generated/resources/META-INF/services/org/apache/camel/resource-resolver/gist
@@ -0,0 +1,2 @@
+# Generated by camel build tools - do NOT edit this file!
+class=org.apache.camel.github.GistResourceResolver
diff --git a/components/camel-resourceresolver-github/src/main/java/org/apache/camel/github/GistResource.java b/components/camel-resourceresolver-github/src/main/java/org/apache/camel/github/GistResource.java
new file mode 100644
index 00000000000..ce20cc2322e
--- /dev/null
+++ b/components/camel-resourceresolver-github/src/main/java/org/apache/camel/github/GistResource.java
@@ -0,0 +1,62 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.github;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.support.ResourceSupport;
+
+public final class GistResource extends ResourceSupport {
+
+    private final CamelContext camelContext;
+    private byte[] cache;
+    private boolean init;
+
+    public GistResource(CamelContext camelContext, String location) {
+        super("gist", location);
+        this.camelContext = camelContext;
+    }
+
+    @Override
+    public boolean exists() {
+        if (!init) {
+            try {
+                URL u = new URL(getLocation());
+                try (InputStream is = u.openStream()) {
+                    cache = camelContext.getTypeConverter().tryConvertTo(byte[].class, is);
+                }
+            } catch (Exception e) {
+                // ignore
+            }
+            init = true;
+        }
+        return cache != null;
+    }
+
+    @Override
+    public InputStream getInputStream() throws IOException {
+        if (exists()) {
+            return new ByteArrayInputStream(cache);
+        } else {
+            return null;
+        }
+    }
+}
diff --git a/components/camel-resourceresolver-github/src/main/java/org/apache/camel/github/GistResourceResolver.java b/components/camel-resourceresolver-github/src/main/java/org/apache/camel/github/GistResourceResolver.java
new file mode 100644
index 00000000000..b1a3f51eaad
--- /dev/null
+++ b/components/camel-resourceresolver-github/src/main/java/org/apache/camel/github/GistResourceResolver.java
@@ -0,0 +1,66 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.github;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.spi.Resource;
+import org.apache.camel.spi.annotations.ResourceResolver;
+import org.apache.camel.support.service.ServiceSupport;
+
+@ResourceResolver("gist")
+public class GistResourceResolver extends ServiceSupport implements org.apache.camel.spi.ResourceResolver {
+
+    // gist:davsclaus:477ddff5cdeb1ae03619aa544ce47e92
+    // https://gist.githubusercontent.com/davsclaus/477ddff5cdeb1ae03619aa544ce47e92/raw
+    private static final String GIST_URL = "https://gist.githubusercontent.com/%s/%s/raw";
+
+    private CamelContext camelContext;
+
+    @Override
+    public CamelContext getCamelContext() {
+        return camelContext;
+    }
+
+    @Override
+    public void setCamelContext(CamelContext camelContext) {
+        this.camelContext = camelContext;
+    }
+
+    @Override
+    public String getSupportedScheme() {
+        return "gist";
+    }
+
+    @Override
+    public Resource resolve(String location) {
+        String[] parts = location.split(":");
+        // scheme not in use as its gist
+        String user = null;
+        String gid = null;
+
+        if (parts.length == 3) {
+            user = parts[1];
+            gid = parts[2];
+        }
+        if (user == null || gid == null) {
+            throw new IllegalArgumentException(location);
+        }
+
+        String target = String.format(GIST_URL, user, gid);
+        return new GistResource(camelContext, target);
+    }
+}
diff --git a/components/camel-resourceresolver-github/src/test/java/org/apache/camel/github/GistResourceResolverTest.java b/components/camel-resourceresolver-github/src/test/java/org/apache/camel/github/GistResourceResolverTest.java
new file mode 100644
index 00000000000..8f84784927f
--- /dev/null
+++ b/components/camel-resourceresolver-github/src/test/java/org/apache/camel/github/GistResourceResolverTest.java
@@ -0,0 +1,39 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.github;
+
+import org.apache.camel.ExtendedCamelContext;
+import org.apache.camel.spi.Resource;
+import org.apache.camel.test.junit5.CamelTestSupport;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+
+public class GistResourceResolverTest extends CamelTestSupport {
+
+    @Test
+    public void testGist() throws Exception {
+        ExtendedCamelContext ecc = context.adapt(ExtendedCamelContext.class);
+        Resource res = ecc.getResourceLoader().resolveResource("gist:davsclaus:123");
+        assertNotNull(res);
+        assertFalse(res.exists());
+        assertEquals("https://gist.githubusercontent.com/davsclaus/123/raw", res.getLocation());
+    }
+
+}


[camel] 04/04: CAMEL-18077: camel-jbang - Run from gist

Posted by da...@apache.org.
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 <cl...@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;
+    }
+
 }


[camel] 02/04: CAMEL-18077: camel-jbang - Run from gist

Posted by da...@apache.org.
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 b929b1b6986d088e0f15959c41ce25a046a21aa5
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Sun May 8 07:48:23 2022 +0200

    CAMEL-18077: camel-jbang - Run from gist
---
 .../apache/camel/github/GistResourceResolver.java  |  16 +--
 .../camel/github/GistResourceResolverTest.java     |   4 +-
 .../camel/dsl/jbang/core/commands/GistHelper.java  | 119 +++++++++++++++++++++
 .../apache/camel/dsl/jbang/core/commands/Run.java  |  25 +++++
 4 files changed, 156 insertions(+), 8 deletions(-)

diff --git a/components/camel-resourceresolver-github/src/main/java/org/apache/camel/github/GistResourceResolver.java b/components/camel-resourceresolver-github/src/main/java/org/apache/camel/github/GistResourceResolver.java
index b1a3f51eaad..d3e1cf683a5 100644
--- a/components/camel-resourceresolver-github/src/main/java/org/apache/camel/github/GistResourceResolver.java
+++ b/components/camel-resourceresolver-github/src/main/java/org/apache/camel/github/GistResourceResolver.java
@@ -24,9 +24,9 @@ import org.apache.camel.support.service.ServiceSupport;
 @ResourceResolver("gist")
 public class GistResourceResolver extends ServiceSupport implements org.apache.camel.spi.ResourceResolver {
 
-    // gist:davsclaus:477ddff5cdeb1ae03619aa544ce47e92
-    // https://gist.githubusercontent.com/davsclaus/477ddff5cdeb1ae03619aa544ce47e92/raw
-    private static final String GIST_URL = "https://gist.githubusercontent.com/%s/%s/raw";
+    // gist:davsclaus:477ddff5cdeb1ae03619aa544ce47e92:cd1be96034748e42e43879a4d27ed297752b6115:mybeer.xml
+    // https://gist.githubusercontent.com/davsclaus/477ddff5cdeb1ae03619aa544ce47e92/raw/cd1be96034748e42e43879a4d27ed297752b6115/mybeer.xml
+    private static final String GIST_URL = "https://gist.githubusercontent.com/%s/%s/raw/%s/%s";
 
     private CamelContext camelContext;
 
@@ -51,16 +51,20 @@ public class GistResourceResolver extends ServiceSupport implements org.apache.c
         // scheme not in use as its gist
         String user = null;
         String gid = null;
+        String gid2 = null;
+        String fileName = null;
 
-        if (parts.length == 3) {
+        if (parts.length == 5) {
             user = parts[1];
             gid = parts[2];
+            gid2 = parts[3];
+            fileName = parts[4];
         }
-        if (user == null || gid == null) {
+        if (user == null || gid == null || gid2 == null || fileName == null) {
             throw new IllegalArgumentException(location);
         }
 
-        String target = String.format(GIST_URL, user, gid);
+        String target = String.format(GIST_URL, user, gid, gid2, fileName);
         return new GistResource(camelContext, target);
     }
 }
diff --git a/components/camel-resourceresolver-github/src/test/java/org/apache/camel/github/GistResourceResolverTest.java b/components/camel-resourceresolver-github/src/test/java/org/apache/camel/github/GistResourceResolverTest.java
index 8f84784927f..fe24b4a8365 100644
--- a/components/camel-resourceresolver-github/src/test/java/org/apache/camel/github/GistResourceResolverTest.java
+++ b/components/camel-resourceresolver-github/src/test/java/org/apache/camel/github/GistResourceResolverTest.java
@@ -30,10 +30,10 @@ public class GistResourceResolverTest extends CamelTestSupport {
     @Test
     public void testGist() throws Exception {
         ExtendedCamelContext ecc = context.adapt(ExtendedCamelContext.class);
-        Resource res = ecc.getResourceLoader().resolveResource("gist:davsclaus:123");
+        Resource res = ecc.getResourceLoader().resolveResource("gist:davsclaus:123:456:beer.xml");
         assertNotNull(res);
         assertFalse(res.exists());
-        assertEquals("https://gist.githubusercontent.com/davsclaus/123/raw", res.getLocation());
+        assertEquals("https://gist.githubusercontent.com/davsclaus/123/raw/456/beer.xml", res.getLocation());
     }
 
 }
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
new file mode 100644
index 00000000000..bba81f4cc16
--- /dev/null
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/GistHelper.java
@@ -0,0 +1,119 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.dsl.jbang.core.commands;
+
+import java.net.URI;
+import java.net.http.HttpClient;
+import java.net.http.HttpRequest;
+import java.net.http.HttpResponse;
+import java.time.Duration;
+import java.util.StringJoiner;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.apache.camel.util.FileUtil;
+
+public final class GistHelper {
+
+    private GistHelper() {
+    }
+
+    public static String asGistSingleUrl(String url) throws Exception {
+        if (url.startsWith("https://gist.githubusercontent.com/")) {
+            url = url.substring(35);
+        } else if (url.startsWith("https://gist.github.com/")) {
+            url = url.substring(24);
+        }
+
+        // https://gist.github.com/davsclaus/477ddff5cdeb1ae03619aa544ce47e92
+        // https://gist.githubusercontent.com/davsclaus/477ddff5cdeb1ae03619aa544ce47e92/raw/cd1be96034748e42e43879a4d27ed297752b6115/mybeer.xml
+        url = url.replaceFirst("/", ":");
+        url = url.replaceFirst("/raw/", ":");
+        url = url.replaceFirst("/", ":");
+        return "gist:" + url;
+    }
+
+    public static void fetchGistUrls(String url, StringJoiner routes, StringJoiner kamelets, StringJoiner properties)
+            throws Exception {
+        doFetchGistUrls(url, routes, kamelets, properties, null);
+    }
+
+    private static void doFetchGistUrls(
+            String url, StringJoiner routes, StringJoiner kamelets, StringJoiner properties,
+            StringJoiner all)
+            throws Exception {
+
+        // a gist can have one or more files
+        // https://gist.github.com/davsclaus/477ddff5cdeb1ae03619aa544ce47e92
+
+        // strip https://gist.github.com/
+        url = url.substring(24);
+
+        String[] parts = url.split("/");
+        if (parts.length < 2) {
+            return;
+        }
+        String user = parts[0];
+        String gid = parts[1];
+
+        url = "https://api.github.com/gists/" + gid;
+
+        resolveGistAsRawFiles(url, routes, kamelets, properties, all);
+    }
+
+    private static void resolveGistAsRawFiles(
+            String url, StringJoiner routes, StringJoiner kamelets, StringJoiner properties, StringJoiner all)
+            throws Exception {
+
+        // use JDK http client to call github api
+        HttpClient hc = HttpClient.newHttpClient();
+        HttpResponse<String> res = hc.send(HttpRequest.newBuilder(new URI(url)).timeout(Duration.ofSeconds(20)).build(),
+                HttpResponse.BodyHandlers.ofString());
+
+        if (res.statusCode() == 200) {
+            ObjectMapper mapper = new ObjectMapper();
+            JsonNode root = mapper.readTree(res.body());
+            for (JsonNode c : root.get("files")) {
+                String name = c.get("filename").asText();
+                String ext = FileUtil.onlyExt(name, false);
+                if (kamelets != null && "kamelet.yaml".equalsIgnoreCase(ext)) {
+                    String rawUrl = c.get("raw_url").asText();
+                    String u = asGistSingleUrl(rawUrl);
+                    kamelets.add(u);
+                } else if (properties != null && "properties".equalsIgnoreCase(ext)) {
+                    String rawUrl = c.get("raw_url").asText();
+                    String u = asGistSingleUrl(rawUrl);
+                    properties.add(u);
+                } else if (routes != null) {
+                    if ("java".equalsIgnoreCase(ext) || "xml".equalsIgnoreCase(ext)
+                            || "yaml".equalsIgnoreCase(ext)
+                            || "groovy".equalsIgnoreCase(ext) || "js".equalsIgnoreCase(ext) || "jsh".equalsIgnoreCase(ext)
+                            || "kts".equalsIgnoreCase(ext)) {
+                        String rawUrl = c.get("raw_url").asText();
+                        String u = asGistSingleUrl(rawUrl);
+                        routes.add(u);
+                    }
+                } else if (all != null) {
+                    String rawUrl = c.get("raw_url").asText();
+                    String u = asGistSingleUrl(rawUrl);
+                    all.add(u);
+                }
+            }
+        }
+    }
+
+}
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 5a98598343f..3b9e035bae4 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
@@ -59,6 +59,7 @@ import picocli.CommandLine.Command;
 import picocli.CommandLine.Option;
 import picocli.CommandLine.Parameters;
 
+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;
 
@@ -458,6 +459,30 @@ class Run implements Callable<Integer> {
                     }
                 }
 
+                if (file.startsWith("https://gist.github.com/")) {
+                    StringJoiner routes = new StringJoiner(",");
+                    StringJoiner kamelets = new StringJoiner(",");
+                    StringJoiner properties = new StringJoiner(",");
+                    fetchGistUrls(file, routes, kamelets, properties);
+
+                    if (routes.length() > 0) {
+                        file = routes.toString();
+                    }
+                    if (properties.length() > 0) {
+                        main.addInitialProperty("camel.component.properties.location", properties.toString());
+                    }
+                    if (kamelets.length() > 0) {
+                        String loc = main.getInitialProperties().getProperty("camel.component.kamelet.location");
+                        if (loc != null) {
+                            // local kamelets first
+                            loc = kamelets + "," + loc;
+                        } else {
+                            loc = kamelets.toString();
+                        }
+                        main.addInitialProperty("camel.component.kamelet.location", loc);
+                    }
+                }
+
                 js.add(file);
                 if (dev && file.startsWith("file:")) {
                     // we can only reload if file based


[camel] 03/04: CAMEL-18077: camel-jbang - Run from gist

Posted by da...@apache.org.
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 cccc1dba5996ec8f1030041412038776b5d2a2ca
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Sun May 8 07:55:45 2022 +0200

    CAMEL-18077: camel-jbang - Run from gist
---
 .../src/main/docs/resourceresolver-github.adoc        | 19 +++++++++++++++++++
 docs/user-manual/modules/ROOT/pages/camel-jbang.adoc  | 14 ++++++++++++++
 2 files changed, 33 insertions(+)

diff --git a/components/camel-resourceresolver-github/src/main/docs/resourceresolver-github.adoc b/components/camel-resourceresolver-github/src/main/docs/resourceresolver-github.adoc
index 869f140a4b0..c75e77c7593 100644
--- a/components/camel-resourceresolver-github/src/main/docs/resourceresolver-github.adoc
+++ b/components/camel-resourceresolver-github/src/main/docs/resourceresolver-github.adoc
@@ -41,3 +41,22 @@ github:apache:camel-kamelets:kamelets/aws-ddb-streams-source.kamelet.yaml
 
 IMPORTANT: This resource resolver can potentially load any resources from GitHub that are in public repositories.
 It's not recommended for production usage, but is great for development and demo purposes.
+
+== Resolving from gist
+
+You can also load resources from gist
+
+The syntax is
+
+[source,text]
+----
+gist:user:id:cid:fileName
+----
+
+For example:
+
+[source,text]
+----
+gist:davsclaus:477ddff5cdeb1ae03619aa544ce47e92:cd1be96034748e42e43879a4d27ed297752b6115:mybeer.xml
+----
+
diff --git a/docs/user-manual/modules/ROOT/pages/camel-jbang.adoc b/docs/user-manual/modules/ROOT/pages/camel-jbang.adoc
index 52a9b34f6d4..2d6cf4e818c 100644
--- a/docs/user-manual/modules/ROOT/pages/camel-jbang.adoc
+++ b/docs/user-manual/modules/ROOT/pages/camel-jbang.adoc
@@ -150,6 +150,20 @@ Or you can run all files starting with rou*
 camel run https://github.com/apache/camel-k/tree/main/examples/languages/rou*
 ----
 
+==== Running routes from GitHub gists
+
+Using gists from GitHub is a quick way to share small Camel routes that you can easily run.
+
+For example to run a gist you simply do:
+
+[source,bash]
+----
+camel run https://gist.github.com/davsclaus/477ddff5cdeb1ae03619aa544ce47e92
+----
+
+A gist can contain one or more files, and Camel JBang will gather all relevant files, so a gist
+can contain multiple routes, properties files, Java beans, etc.
+
 === Downloading routes hosted on GitHub
 
 We have made it easy for Camel JBang to download existing examples from GitHub to local disk,