This is an automated email from the ASF dual-hosted git repository. tsato pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/camel.git
commit 65a66914202cbc7f9e73cdaa6267744c2e5d7d87 Author: Tadayoshi Sato <[email protected]> AuthorDate: Fri Nov 11 12:43:41 2022 +0900 CAMEL-18673: camel-jbang - Provide shell completion for camel CLI --- .../dsl/jbang/core/commands/CamelCommand.java | 3 ++ .../dsl/jbang/core/commands/CamelJBangMain.java | 3 +- .../camel/dsl/jbang/core/commands/Complete.java | 46 ++++++++++++++++++++++ 3 files changed, 51 insertions(+), 1 deletion(-) diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/CamelCommand.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/CamelCommand.java index 5653768a46b..acc543d0d41 100644 --- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/CamelCommand.java +++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/CamelCommand.java @@ -23,6 +23,9 @@ import picocli.CommandLine; public abstract class CamelCommand implements Callable<Integer> { + @CommandLine.Spec + CommandLine.Model.CommandSpec spec; + private final CamelJBangMain main; private File camelDir; 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 aeb3527cc74..36549b2cad1 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 @@ -115,7 +115,8 @@ public class CamelJBangMain implements Callable<Integer> { .addSubcommand("bind", new CommandLine(new Bind(main))) .addSubcommand("pipe", new CommandLine(new Pipe(main))) .addSubcommand("dependencies", new CommandLine(new DependencyTree(main))) - .addSubcommand("export", new CommandLine(new Export(main))); + .addSubcommand("export", new CommandLine(new Export(main))) + .addSubcommand("completion", new CommandLine(new Complete(main))); commandLine.getCommandSpec().versionProvider(() -> { CamelCatalog catalog = new DefaultCamelCatalog(); diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Complete.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Complete.java new file mode 100644 index 00000000000..e958ff1efa1 --- /dev/null +++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Complete.java @@ -0,0 +1,46 @@ +/* + * 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.PrintStream; + +import picocli.AutoComplete; +import picocli.CommandLine; + [email protected](name = "complete", description = "Generate completion script for bash/zsh") +class Complete extends CamelCommand { + + public Complete(CamelJBangMain main) { + super(main); + } + + @Override + public Integer call() throws Exception { + String script = AutoComplete.bash( + spec.parent().name(), + spec.parent().commandLine()); + + // not PrintWriter.println: scripts with Windows line separators fail in strange + // ways! + PrintStream out = System.out; + out.print(script); + out.print('\n'); + out.flush(); + return 0; + } + +}
