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
The following commit(s) were added to refs/heads/main by this push: new 7484bf12c7d CAMEL-18794: camel-jbang - Run --code 7484bf12c7d is described below commit 7484bf12c7d79d5027f6dfdc1c05b899be8b929c Author: Claus Ibsen <claus.ib...@gmail.com> AuthorDate: Sun Dec 4 14:24:28 2022 +0100 CAMEL-18794: camel-jbang - Run --code --- .../modules/ROOT/pages/camel-jbang.adoc | 17 +++++++++++++ .../apache/camel/dsl/jbang/core/commands/Run.java | 28 ++++++++++++++++++++++ .../src/main/resources/templates/code-java.tmpl | 14 +++++++++++ 3 files changed, 59 insertions(+) diff --git a/docs/user-manual/modules/ROOT/pages/camel-jbang.adoc b/docs/user-manual/modules/ROOT/pages/camel-jbang.adoc index 18d55842481..3eedfca1044 100644 --- a/docs/user-manual/modules/ROOT/pages/camel-jbang.adoc +++ b/docs/user-manual/modules/ROOT/pages/camel-jbang.adoc @@ -134,6 +134,23 @@ camel run * TIP: The run goal can also detect files that are `properties`, such as `application.properties`. +=== Running Route from input parameter + +For very small Java routes then it is possible to provide the route as CLI argument, as shown below: + +[source,bash] +---- +camel run --code="from('kamelet:beer-source').to('log:beer')" +---- + +This is very limited as the CLI argument is a bit cumbersome to use than files. + +- Java DSL code is only supported +- Use single quote instead of double quotes +- All routes must be defined in a single `--code` parameter. + +NOTE: Using `--code` is only usable for very quick and small prototype. + === Dev mode with live reload You can enable dev mode that comes with live reload of the route(s) when the source file is updated (saved), 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 05b145615d9..67a0cac3d8c 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 @@ -24,6 +24,7 @@ import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; +import java.io.InputStream; import java.nio.charset.StandardCharsets; import java.nio.file.FileSystems; import java.nio.file.Files; @@ -189,6 +190,9 @@ class Run extends CamelCommand { @Option(names = { "--open-api" }, description = "Adds an OpenAPI spec from the given file") String openapi; + @Option(names = { "--code" }, description = "Run the given string as Java DSL route") + String code; + public Run(CamelJBangMain main) { super(main); } @@ -286,6 +290,13 @@ class Run extends CamelCommand { if (openapi != null) { generateOpenApi(); } + // route code as option + if (code != null) { + // store code in temporary file + String codeFile = loadFromCode(code); + // use code as first file + files.add(0, codeFile); + } // if no specific file to run then try to auto-detect if (files.isEmpty()) { @@ -552,6 +563,23 @@ class Run extends CamelCommand { return main.getExitCode(); } + private String loadFromCode(String code) throws IOException { + String fn = WORK_DIR + "/CodeRoute.java"; + InputStream is = Run.class.getClassLoader().getResourceAsStream("templates/code-java.tmpl"); + String content = IOHelper.loadText(is); + IOHelper.close(is); + // need to replace single quote as double quotes and end with semicolon + code = code.replace("'", "\""); + code = code.trim(); + if (!code.endsWith(";")) { + code = code + ";"; + } + content = content.replaceFirst("\\{\\{ \\.Name }}", "CodeRoute"); + content = content.replaceFirst("\\{\\{ \\.Code }}", code); + Files.write(Paths.get(fn), content.getBytes(StandardCharsets.UTF_8)); + return "file:" + fn; + } + private String evalGistSource(KameletMain main, String file) throws Exception { StringJoiner routes = new StringJoiner(","); StringJoiner kamelets = new StringJoiner(","); diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/resources/templates/code-java.tmpl b/dsl/camel-jbang/camel-jbang-core/src/main/resources/templates/code-java.tmpl new file mode 100644 index 00000000000..a5d69c1b7fa --- /dev/null +++ b/dsl/camel-jbang/camel-jbang-core/src/main/resources/templates/code-java.tmpl @@ -0,0 +1,14 @@ +// camel-k: language=java + +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.CamelContext; +import org.apache.camel.Exchange; +import org.apache.camel.Message; + +public class {{ .Name }} extends RouteBuilder { + + @Override + public void configure() throws Exception { + {{ .Code }} + } +}