This is an automated email from the ASF dual-hosted git repository. gnodet pushed a commit to branch copy-shroud in repository https://gitbox.apache.org/repos/asf/camel.git
commit 79203b189771356d1c71692654de0fbb54756612 Author: Guillaume Nodet <[email protected]> AuthorDate: Tue Mar 24 07:54:41 2026 +0100 CAMEL-23236: Add camel run --example for zero-to-running experience Allow running built-in examples without creating any files first: camel run --example timer-log camel run --example rest-api camel run --example cron-log Use --example-list to show available examples. Examples are bundled YAML route files extracted to a temp directory at runtime. Co-Authored-By: Claude Opus 4.6 <[email protected]> --- .../apache/camel/dsl/jbang/core/commands/Run.java | 63 ++++++++++++++++++++++ .../src/main/resources/examples/cron-log.yaml | 10 ++++ .../src/main/resources/examples/rest-api.yaml | 21 ++++++++ .../src/main/resources/examples/timer-log.yaml | 10 ++++ 4 files changed, 104 insertions(+) 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 653b99ea8541..cefeb6046b1b 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 @@ -325,6 +325,15 @@ public class Run extends CamelCommand { description = "Skip resolving plugin dependencies") boolean skipPlugins; + @Option(names = { "--example" }, + description = "Run a built-in example by name (e.g., timer-log, rest-api). Use --example --list to show available examples.", + arity = "0..1", fallbackValue = "") + String example; + + @Option(names = { "--example-list" }, + description = "List available built-in examples") + boolean exampleList; + public Run(CamelJBangMain main) { super(main); } @@ -344,6 +353,14 @@ public class Run extends CamelCommand { @Override public Integer doCall() throws Exception { + // handle --example + if (exampleList || (example != null && example.isEmpty())) { + return listExamples(); + } + if (example != null) { + return runExample(); + } + if (!exportRun) { printConfigurationValues("Running integration with the following configuration:"); } @@ -351,6 +368,52 @@ public class Run extends CamelCommand { return run(); } + private int listExamples() { + printer().println("Available built-in examples:"); + printer().println(); + printer().printf(" %-20s %s%n", "timer-log", "Simple timer that logs messages every second"); + printer().printf(" %-20s %s%n", "rest-api", "REST API with hello endpoints"); + printer().printf(" %-20s %s%n", "cron-log", "Scheduled task that logs every 5 seconds"); + printer().println(); + printer().println("Usage: camel run --example <name>"); + printer().println(" camel run --example <name> --dev"); + return 0; + } + + private int runExample() throws Exception { + String resourcePath = "examples/" + example + ".yaml"; + InputStream is = Run.class.getClassLoader().getResourceAsStream(resourcePath); + if (is == null) { + printer().printErr("Unknown example: " + example); + printer().printErr("Run 'camel run --example-list' to see available examples."); + return 1; + } + + // extract example to a temp file and run it + Path tempDir = Files.createTempDirectory("camel-example-"); + Path exampleFile = tempDir.resolve(example + ".yaml"); + try { + String content = IOHelper.loadText(is); + IOHelper.close(is); + Files.writeString(exampleFile, content); + + printer().println("Running example: " + example); + files.add(exampleFile.toString()); + if ("CamelJBang".equals(name)) { + name = example; + } + + if (!exportRun) { + printConfigurationValues("Running integration with the following configuration:"); + } + return run(); + } finally { + // clean up temp files on JVM exit + exampleFile.toFile().deleteOnExit(); + tempDir.toFile().deleteOnExit(); + } + } + public Integer runExport() throws Exception { return runExport(false); } diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/resources/examples/cron-log.yaml b/dsl/camel-jbang/camel-jbang-core/src/main/resources/examples/cron-log.yaml new file mode 100644 index 000000000000..c31bdb1cec3d --- /dev/null +++ b/dsl/camel-jbang/camel-jbang-core/src/main/resources/examples/cron-log.yaml @@ -0,0 +1,10 @@ +- route: + id: cron-log + from: + uri: timer:cron + parameters: + period: "5000" + steps: + - setBody: + simple: "Scheduled task running at ${date:now:HH:mm:ss}" + - log: "${body}" diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/resources/examples/rest-api.yaml b/dsl/camel-jbang/camel-jbang-core/src/main/resources/examples/rest-api.yaml new file mode 100644 index 000000000000..2e1035cf1f0f --- /dev/null +++ b/dsl/camel-jbang/camel-jbang-core/src/main/resources/examples/rest-api.yaml @@ -0,0 +1,21 @@ +- rest: + path: /api + get: + - path: /hello + to: direct:hello + - path: /hello/{name} + to: direct:hello-name +- route: + id: hello + from: + uri: direct:hello + steps: + - setBody: + constant: "Hello from Camel REST API!" +- route: + id: hello-name + from: + uri: direct:hello-name + steps: + - setBody: + simple: "Hello ${header.name} from Camel REST API!" diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/resources/examples/timer-log.yaml b/dsl/camel-jbang/camel-jbang-core/src/main/resources/examples/timer-log.yaml new file mode 100644 index 000000000000..cdff71b4d71f --- /dev/null +++ b/dsl/camel-jbang/camel-jbang-core/src/main/resources/examples/timer-log.yaml @@ -0,0 +1,10 @@ +- route: + id: timer-log + from: + uri: timer:tick + parameters: + period: "1000" + steps: + - setBody: + simple: "Hello Camel! (message #${exchangeProperty.CamelTimerCounter})" + - log: "${body}"
