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 4dba2a17bf53b1a0c0a09bf142451105fcb11477 Author: Claus Ibsen <[email protected]> AuthorDate: Thu Apr 14 10:45:02 2022 +0200 CAMEL-17967: camel-jbang - Init command like camel-k --- .../jbang/core/commands/AbstractInitKamelet.java | 1 + .../dsl/jbang/core/commands/AbstractSearch.java | 1 + .../dsl/jbang/core/commands/CamelJBangMain.java | 4 +- .../apache/camel/dsl/jbang/core/commands/Init.java | 22 +++- .../camel/dsl/jbang/core/commands/InitBinding.java | 76 ------------- .../camel/dsl/jbang/core/commands/InitKamelet.java | 120 --------------------- .../camel/dsl/jbang/core/commands/Search.java | 1 + .../dsl/jbang/core/commands/SearchComponents.java | 1 + .../dsl/jbang/core/commands/SearchKamelets.java | 1 + .../dsl/jbang/core/commands/SearchLanguages.java | 1 + .../dsl/jbang/core/commands/SearchOthers.java | 1 + .../src/main/resources/templates/groovy.tmpl | 8 ++ .../src/main/resources/templates/java.tmpl | 16 +++ .../src/main/resources/templates/js.tmpl | 8 ++ .../src/main/resources/templates/kamelet.yaml.tmpl | 37 +++++++ .../src/main/resources/templates/kts.tmpl | 8 ++ .../src/main/resources/templates/xml.tmpl | 19 ++++ .../src/main/resources/templates/yaml.tmpl | 11 ++ 18 files changed, 135 insertions(+), 201 deletions(-) diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/AbstractInitKamelet.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/AbstractInitKamelet.java index 2502c141f4d..23daa6c64a0 100644 --- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/AbstractInitKamelet.java +++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/AbstractInitKamelet.java @@ -31,6 +31,7 @@ import org.apache.commons.io.IOUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +@Deprecated public abstract class AbstractInitKamelet { private static final Logger LOG = LoggerFactory.getLogger(AbstractInitKamelet.class); diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/AbstractSearch.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/AbstractSearch.java index 2d9a2bc12b2..43f4f903bb2 100644 --- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/AbstractSearch.java +++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/AbstractSearch.java @@ -31,6 +31,7 @@ import org.apache.camel.main.KameletMain; import org.apache.camel.spi.Resource; import org.apache.commons.io.IOUtils; +@Deprecated public abstract class AbstractSearch { private String resourceLocation; private String branch; diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/CamelJBangMain.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/CamelJBangMain.java index 760eb60fb57..d38e39889c4 100644 --- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/CamelJBangMain.java +++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/CamelJBangMain.java @@ -29,14 +29,12 @@ public class CamelJBangMain implements Callable<Integer> { public static void run(String... args) { commandLine = new CommandLine(new CamelJBangMain()) .addSubcommand("run", new Run()) + .addSubcommand("init", new CommandLine(new Init())) .addSubcommand("search", new CommandLine(new Search()) .addSubcommand("kamelets", new SearchKamelets()) .addSubcommand("components", new SearchComponents()) .addSubcommand("languages", new SearchLanguages()) .addSubcommand("others", new SearchOthers())) - .addSubcommand("init", new CommandLine(new Init()) - .addSubcommand("kamelet", new InitKamelet()) - .addSubcommand("binding", new InitBinding())) .addSubcommand("create", new CommandLine(new Create()) .addSubcommand("project", new Project())); 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 dc4ba5d364b..01d1859ec9c 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 @@ -16,14 +16,22 @@ */ package org.apache.camel.dsl.jbang.core.commands; +import java.io.FileOutputStream; +import java.io.InputStream; import java.util.concurrent.Callable; +import org.apache.camel.util.FileUtil; +import org.apache.camel.util.IOHelper; import picocli.CommandLine; import picocli.CommandLine.Command; import picocli.CommandLine.Option; -@Command(name = "init", description = "Creates Kamelets (use --help)") +@Command(name = "init", description = "Initialize empty Camel integrations") class Init implements Callable<Integer> { + + @CommandLine.Parameters(description = "Name of integration file", arity = "1") + private String file; + //CHECKSTYLE:OFF @Option(names = { "-h", "--help" }, usageHelp = true, description = "Display the help and sub-commands") private boolean helpRequested = false; @@ -31,8 +39,18 @@ class Init implements Callable<Integer> { @Override public Integer call() throws Exception { - new CommandLine(this).execute("--help"); + String ext = FileUtil.onlyExt(file, false); + String name = FileUtil.onlyName(file, false); + InputStream is = Init.class.getClassLoader().getResourceAsStream("templates/" + ext + ".tmpl"); + if (is == null) { + System.out.println("Error: unsupported file type: " + ext); + return 1; + } + String context = IOHelper.loadText(is); + IOHelper.close(is); + context = context.replaceFirst("\\{\\{ \\.Name }}", name); + IOHelper.writeText(context, new FileOutputStream(file, false)); return 0; } } diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/InitBinding.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/InitBinding.java deleted file mode 100644 index 45651b68dce..00000000000 --- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/InitBinding.java +++ /dev/null @@ -1,76 +0,0 @@ -/* - * 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.io.File; -import java.io.IOException; -import java.util.concurrent.Callable; - -import org.apache.camel.CamelException; -import org.apache.camel.dsl.jbang.core.common.exceptions.ResourceAlreadyExists; -import picocli.CommandLine.Command; -import picocli.CommandLine.Option; - -@Command(name = "binding", description = "Create a new Kamelet Binding") -class InitBinding extends AbstractInitKamelet implements Callable<Integer> { - //CHECKSTYLE:OFF - @Option(names = { "-h", "--help" }, usageHelp = true, description = "Display the help and sub-commands") - private boolean helpRequested = false; - //CHECKSTYLE:ON - - @Option(names = { "--base-resource-location" }, defaultValue = "github:apache", hidden = true, - description = "Where to download the resources from (used for development/testing)") - private String baseResourceLocation; - - @Option(names = { "--branch" }, defaultValue = "main", hidden = true, - description = "The branch to use when downloading resources from (used for development/testing)") - private String branch; - - @Option(names = { "--destination" }, defaultValue = "work", - description = "The destination directory where to download the files") - private String destination; - - @Option(names = { "--kamelet" }, defaultValue = "", - description = "The kamelet to create a binding for") - private String kamelet; - - @Option(names = { "--project" }, defaultValue = "camel-k", - description = "The project to create a binding for (either camel-k or core)") - private String project; - - private int downloadSample() throws IOException, CamelException { - setBranch(branch); - - String resourcePath = String.format("camel-kamelets:templates/bindings/%s/%s-binding.yaml", project, kamelet); - - setResourceLocation(baseResourceLocation, resourcePath); - - try { - resolveResource(new File(destination)); - } catch (ResourceAlreadyExists e) { - System.err.println(e.getMessage()); - return 1; - } - - return 0; - } - - @Override - public Integer call() throws Exception { - return downloadSample(); - } -} diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/InitKamelet.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/InitKamelet.java deleted file mode 100644 index edb61f431ed..00000000000 --- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/InitKamelet.java +++ /dev/null @@ -1,120 +0,0 @@ -/* - * 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.io.File; -import java.io.FileWriter; -import java.io.IOException; -import java.util.concurrent.Callable; - -import org.apache.camel.CamelException; -import org.apache.camel.dsl.jbang.core.common.exceptions.ResourceAlreadyExists; -import org.apache.camel.dsl.jbang.core.templates.VelocityTemplateParser; -import picocli.CommandLine; -import picocli.CommandLine.Command; -import picocli.CommandLine.Option; - -@Command(name = "kamelet", description = "Create a new Kamelet") -class InitKamelet extends AbstractInitKamelet implements Callable<Integer> { - //CHECKSTYLE:OFF - @Option(names = { "-h", "--help" }, usageHelp = true, description = "Display the help and sub-commands") - private boolean helpRequested = false; - //CHECKSTYLE:ON - - @CommandLine.ArgGroup(exclusive = true, multiplicity = "1") - private ProcessOptions processOptions; - - static class ProcessOptions { - //CHECKSTYLE:OFF - @Option(names = { "--bootstrap" }, - description = "Bootstrap the Kamelet template generator - download the properties file for editing") - private boolean bootstrap = false; - //CHECKSTYLE:ON - - @Option(names = { "--properties-path" }, defaultValue = "", description = "Kamelet name") - private String propertiesPath; - } - - @Option(names = { "--base-resource-location" }, defaultValue = "github:apache", hidden = true, - description = "Where to download the resources from (used for development/testing)") - private String baseResourceLocation; - - @Option(names = { "--branch" }, defaultValue = "main", hidden = true, - description = "The branch to use when downloading resources from (used for development/testing)") - private String branch; - - @Option(names = { "--destination" }, defaultValue = "work", - description = "The destination directory where to download the files") - private String destination; - - @Override - public Integer call() throws Exception { - if (processOptions.bootstrap) { - bootstrap(); - } else { - generateTemplate(); - } - - return 0; - } - - private int generateTemplate() throws IOException, CamelException { - setBranch(branch); - setResourceLocation(baseResourceLocation, "camel-kamelets:templates/init-template.kamelet.yaml.vm"); - - File workDirectory = new File(destination); - - File localTemplateFile; - try { - localTemplateFile = resolveResource(workDirectory); - } catch (ResourceAlreadyExists e) { - System.err.println(e.getMessage()); - return 1; - } - localTemplateFile.deleteOnExit(); - - VelocityTemplateParser templateParser = new VelocityTemplateParser( - localTemplateFile.getParentFile(), - processOptions.propertiesPath); - - File outputFile; - try { - outputFile = templateParser.getOutputFile(workDirectory); - } catch (ResourceAlreadyExists e) { - System.err.println(e.getMessage()); - return 1; - } - - try (FileWriter fw = new FileWriter(outputFile)) { - templateParser.parse(localTemplateFile.getName(), fw); - System.out.println("Template file was written to " + outputFile); - } - - return 0; - } - - private int bootstrap() throws IOException, CamelException { - try { - super.bootstrap(branch, baseResourceLocation, destination); - return 0; - } catch (ResourceAlreadyExists e) { - System.err.println(e.getMessage()); - - return 1; - } - } -} diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Search.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Search.java index 78598d57e1a..3974a5a7912 100644 --- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Search.java +++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Search.java @@ -23,6 +23,7 @@ import picocli.CommandLine.Command; import picocli.CommandLine.Option; @Command(name = "search", description = "Search for Kamelets, components and patterns (use --help)") +@Deprecated class Search extends AbstractSearch implements Callable<Integer> { //CHECKSTYLE:OFF @Option(names = { "-h", "--help" }, usageHelp = true, description = "Display the help and sub-commands") diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/SearchComponents.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/SearchComponents.java index 53d4ccef594..6bcafb7bc71 100644 --- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/SearchComponents.java +++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/SearchComponents.java @@ -29,6 +29,7 @@ import picocli.CommandLine.Command; import picocli.CommandLine.Option; @Command(name = "components", description = "Search for Camel components") +@Deprecated class SearchComponents extends AbstractSearch implements Callable<Integer> { /* diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/SearchKamelets.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/SearchKamelets.java index 12d33d116af..11e167ff25b 100644 --- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/SearchKamelets.java +++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/SearchKamelets.java @@ -28,6 +28,7 @@ import picocli.CommandLine.Command; import picocli.CommandLine.Option; @Command(name = "kamelets", description = "Search for a Kamelet in the Kamelet catalog") +@Deprecated class SearchKamelets extends AbstractSearch implements Callable<Integer> { /* * Matches the following line. Separate them into groups and pick the last diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/SearchLanguages.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/SearchLanguages.java index 952f205a07f..007e4e32dae 100644 --- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/SearchLanguages.java +++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/SearchLanguages.java @@ -29,6 +29,7 @@ import picocli.CommandLine.Command; import picocli.CommandLine.Option; @Command(name = "languages", description = "Search for Camel expression languages") +@Deprecated class SearchLanguages extends AbstractSearch implements Callable<Integer> { /* * Matches the following line. Separate them into groups and pick the last diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/SearchOthers.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/SearchOthers.java index 1dd647a2081..47b483bb953 100644 --- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/SearchOthers.java +++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/SearchOthers.java @@ -29,6 +29,7 @@ import picocli.CommandLine.Command; import picocli.CommandLine.Option; @Command(name = "others", description = "Search for Camel miscellaneous components") +@Deprecated class SearchOthers extends AbstractSearch implements Callable<Integer> { /* * Matches the following line. Separate them into groups and pick the last diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/resources/templates/groovy.tmpl b/dsl/camel-jbang/camel-jbang-core/src/main/resources/templates/groovy.tmpl new file mode 100644 index 00000000000..41985ab27fc --- /dev/null +++ b/dsl/camel-jbang/camel-jbang-core/src/main/resources/templates/groovy.tmpl @@ -0,0 +1,8 @@ +// camel-k: language=groovy + +// Write your routes here, for example: +from('timer:groovy?period=1000') + .routeId('groovy') + .setBody() + .simple('Hello Camel K from ${routeId}') + .log('${body}') diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/resources/templates/java.tmpl b/dsl/camel-jbang/camel-jbang-core/src/main/resources/templates/java.tmpl new file mode 100644 index 00000000000..1c1d4fd97a8 --- /dev/null +++ b/dsl/camel-jbang/camel-jbang-core/src/main/resources/templates/java.tmpl @@ -0,0 +1,16 @@ +// camel-k: language=java + +import org.apache.camel.builder.RouteBuilder; + +public class {{ .Name }} extends RouteBuilder { + + @Override + public void configure() throws Exception { + + // Write your routes here, for example: + from("timer:java?period=1000").routeId("java") + .setBody() + .simple("Hello Camel K from ${routeId}") + .log("${body}"); + } +} diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/resources/templates/js.tmpl b/dsl/camel-jbang/camel-jbang-core/src/main/resources/templates/js.tmpl new file mode 100644 index 00000000000..79ba4ce25da --- /dev/null +++ b/dsl/camel-jbang/camel-jbang-core/src/main/resources/templates/js.tmpl @@ -0,0 +1,8 @@ +// camel-k: language=js + +// Write your routes here, for example: +from('timer:js?period=1000') + .routeId('js') + .setBody() + .simple('Hello Camel K from ${routeId}') + .log('${body}') diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/resources/templates/kamelet.yaml.tmpl b/dsl/camel-jbang/camel-jbang-core/src/main/resources/templates/kamelet.yaml.tmpl new file mode 100644 index 00000000000..f89270bec31 --- /dev/null +++ b/dsl/camel-jbang/camel-jbang-core/src/main/resources/templates/kamelet.yaml.tmpl @@ -0,0 +1,37 @@ +apiVersion: camel.apache.org/v1alpha1 +kind: Kamelet +metadata: + name: {{ .Name }} + labels: + camel.apache.org/kamelet.type: "source" +spec: + definition: + title: "Timer Example" + description: "Produces periodic events with a custom payload" + required: + - message + properties: + period: + title: Period + description: The time interval between two events + type: integer + default: 1000 + message: + title: Message + description: The message to generate + type: string + types: + out: + mediaType: text/plain + template: + from: + uri: timer:tick + parameters: + period: "{{period}}" + steps: + - setBody: + constant: "{{message}}" + - setHeader: + name: "Content-Type" + constant: "text/plain" + - to: "kamelet:sink" diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/resources/templates/kts.tmpl b/dsl/camel-jbang/camel-jbang-core/src/main/resources/templates/kts.tmpl new file mode 100644 index 00000000000..97727259a15 --- /dev/null +++ b/dsl/camel-jbang/camel-jbang-core/src/main/resources/templates/kts.tmpl @@ -0,0 +1,8 @@ +// camel-k: language=kotlin + +// Write your routes here, for example: +from("timer:kotlin?period=1000") + .routeId("kotlin") + .setBody() + .simple("Hello Camel K from \${routeId}") + .log("\${body}") diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/resources/templates/xml.tmpl b/dsl/camel-jbang/camel-jbang-core/src/main/resources/templates/xml.tmpl new file mode 100644 index 00000000000..04f4f069461 --- /dev/null +++ b/dsl/camel-jbang/camel-jbang-core/src/main/resources/templates/xml.tmpl @@ -0,0 +1,19 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- camel-k: language=xml --> + +<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xmlns="http://camel.apache.org/schema/spring" + xsi:schemaLocation=" + http://camel.apache.org/schema/spring + https://camel.apache.org/schema/spring/camel-spring.xsd"> + + <!-- Write your routes here, for example: --> + <route id="xml"> + <from uri="timer:xml?period=1000"/> + <setBody> + <simple>Hello Camel K from ${routeId}</simple> + </setBody> + <log message="${body}"/> + </route> + +</routes> diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/resources/templates/yaml.tmpl b/dsl/camel-jbang/camel-jbang-core/src/main/resources/templates/yaml.tmpl new file mode 100644 index 00000000000..2c31e18ccfd --- /dev/null +++ b/dsl/camel-jbang/camel-jbang-core/src/main/resources/templates/yaml.tmpl @@ -0,0 +1,11 @@ +# camel-k: language=yaml + +# Write your routes here, for example: +- from: + uri: "timer:yaml" + parameters: + period: "1000" + steps: + - setBody: + constant: "Hello Camel K from yaml" + - log: "${body}"
