zhfeng commented on a change in pull request #5770: URL: https://github.com/apache/camel/pull/5770#discussion_r661985484
########## File path: dsl/camel-jbang/CamelJbang.java ########## @@ -0,0 +1,255 @@ +///usr/bin/env jbang "$0" "$@" ; exit $? +/* + * 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. + */ + +//DEPS info.picocli:picocli:4.5.0 +//DEPS org.apache.camel:camel-core:3.12.0-SNAPSHOT +//DEPS org.apache.camel:camel-core-model:3.12.0-SNAPSHOT +//DEPS org.apache.camel:camel-api:3.12.0-SNAPSHOT +//DEPS org.apache.camel:camel-main:3.12.0-SNAPSHOT +//DEPS org.apache.camel:camel-kamelet-main:3.12.0-SNAPSHOT +//DEPS org.apache.camel:camel-file-watch:3.12.0-SNAPSHOT +//DEPS org.apache.camel:camel-resourceresolver-github:3.12.0-SNAPSHOT +//DEPS org.apache.logging.log4j:log4j-api:2.13.3 +//DEPS org.apache.logging.log4j:log4j-core:2.13.3 +//DEPS org.apache.logging.log4j:log4j-slf4j-impl:2.13.3 +//FILES application.properties + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.Reader; +import java.nio.channels.Channels; +import java.nio.channels.FileChannel; +import java.nio.channels.ReadableByteChannel; +import java.util.concurrent.Callable; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import java.util.function.Consumer; + +import org.apache.camel.main.KameletMain; +import org.apache.camel.spi.Resource; +import org.apache.camel.CamelContext; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.github.GitHubResourceResolver; +import org.apache.logging.log4j.Level; +import org.apache.logging.log4j.core.config.Configurator; +import org.apache.logging.log4j.core.config.DefaultConfiguration; + +import picocli.CommandLine; +import picocli.CommandLine.Command; +import picocli.CommandLine.Parameters; +import picocli.CommandLine.Option; + +@Command(name = "run", description = "Run a Kamelet") +class Run implements Callable<Integer> { + private CamelContext context; + + @Parameters(description = "The path to the kamelet binding", defaultValue = "") + private String binding; + + @Option(names = {"--debug-level"}, defaultValue = "info", description = "Default debug level") + private String debugLevel; + + class ShutdownRoute extends RouteBuilder { + private File lockFile; + + public ShutdownRoute(File lockFile) { + this.lockFile = lockFile; + } + + public void configure() { + fromF("file-watch://%s?events=DELETE&antInclude=%s", lockFile.getParent(), lockFile.getName()) + .process(p -> context.shutdown()); + } + } + + @Override + public Integer call() throws Exception { + switch (debugLevel) { + case "trace": Configurator.setRootLevel(Level.TRACE); break; + case "debug": Configurator.setRootLevel(Level.DEBUG); break; + case "info": Configurator.setRootLevel(Level.INFO); break; + case "warn": Configurator.setRootLevel(Level.WARN); break; + case "fatal": Configurator.setRootLevel(Level.FATAL); break; + default: { + System.err.println("Invalid debug level " + debugLevel); + return 1; + } + } + + System.out.println("Starting Camel Jbang!"); + + if (binding.isEmpty()) { + System.err.println("You must provide a binding file (check --help)"); + + return 1; + } + + File bindingFile = new File(binding); + if (!bindingFile.exists()) { + System.err.println("The binding file does not exist"); + + return 1; + } + + System.setProperty("camel.main.routes-include-pattern", "file:" + binding); + System.setProperty("camel.main.name", "CamelJbang"); + + KameletMain main = new KameletMain(); + + main.configure().addRoutesBuilder(new ShutdownRoute(createLockFile())); + main.start(); + context = main.getCamelContext(); + + main.run(); + return 0; + } + + public File createLockFile() throws IOException { + File lockFile = File.createTempFile(".run", ".vicuna.lock", new File(".")); + + System.out.printf("A new lock file was created on %s. Delete this file to stop running%n", + lockFile.getAbsolutePath()); + lockFile.deleteOnExit(); + + return lockFile; + } +} + +@Command(name = "search", description = "Run a Kamelet") Review comment: A typo of description ? it should be "Search a Kamelet" -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: commits-unsubscr...@camel.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org