Completion command for bash

- Add basic support of defining a command `completion bash` which
  outputs a generic bash script which can be used in a user environment.
- Idea for completion is copied from go's cobra library what comes for
  a bash dance itself.
- Goes through command registry, builds a model for command structure
  and uses antlr st4 for templating bash.
- Should give foundation to create other completions just like in cobra.
- Currently as we don't know a root-command in a generic way, option
  `spring.shell.command.completion.root-command` is required user to set.
- Fixes #343
This commit is contained in:
Janne Valkealahti
2022-01-07 09:58:21 +00:00
parent 7298b2ce78
commit 668ddb458e
17 changed files with 1408 additions and 22 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2021 the original author or authors.
* Copyright 2021-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -179,6 +179,28 @@ public class SpringShellProperties {
}
}
public static class CompletionCommand {
private boolean enabled = true;
private String rootCommand;
public boolean isEnabled() {
return enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
public String getRootCommand() {
return rootCommand;
}
public void setRootCommand(String rootCommand) {
this.rootCommand = rootCommand;
}
}
public static class Command {
private HelpCommand help = new HelpCommand();
@@ -187,6 +209,7 @@ public class SpringShellProperties {
private StacktraceCommand stacktrace = new StacktraceCommand();
private ScriptCommand script = new ScriptCommand();
private HistoryCommand history = new HistoryCommand();
private CompletionCommand completion = new CompletionCommand();
public void setHelp(HelpCommand help) {
this.help = help;
@@ -235,5 +258,13 @@ public class SpringShellProperties {
public void setHistory(HistoryCommand history) {
this.history = history;
}
public CompletionCommand getCompletion() {
return completion;
}
public void setCompletion(CompletionCommand completion) {
this.completion = completion;
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2017-2021 the original author or authors.
* Copyright 2017-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -22,10 +22,14 @@ import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.shell.boot.condition.OnCompletionCommandCondition;
import org.springframework.shell.result.ThrowableResultHandler;
import org.springframework.shell.standard.commands.Clear;
import org.springframework.shell.standard.commands.Completion;
import org.springframework.shell.standard.commands.Help;
import org.springframework.shell.standard.commands.History;
import org.springframework.shell.standard.commands.Quit;
@@ -39,6 +43,7 @@ import org.springframework.shell.standard.commands.Stacktrace;
*/
@Configuration(proxyBeanMethods = false)
@ConditionalOnClass({ Help.Command.class })
@EnableConfigurationProperties(SpringShellProperties.class)
public class StandardCommandsAutoConfiguration {
@Bean
@@ -82,4 +87,11 @@ public class StandardCommandsAutoConfiguration {
public History historyCommand(org.jline.reader.History jLineHistory) {
return new History(jLineHistory);
}
@Bean
@ConditionalOnMissingBean(Completion.Command.class)
@Conditional(OnCompletionCommandCondition.class)
public Completion completion(SpringShellProperties properties) {
return new Completion(properties.getCommand().getCompletion().getRootCommand());
}
}

View File

@@ -0,0 +1,34 @@
/*
* Copyright 2022 the original author or authors.
*
* Licensed 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
*
* https://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.springframework.shell.boot.condition;
import org.springframework.boot.autoconfigure.condition.AllNestedConditions;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
public class OnCompletionCommandCondition extends AllNestedConditions {
public OnCompletionCommandCondition() {
super(ConfigurationPhase.REGISTER_BEAN);
}
@ConditionalOnProperty(prefix = "spring.shell.command.completion", value = "root-command")
static class RootNameCondition {
}
@ConditionalOnProperty(prefix = "spring.shell.command.completion", value = "enabled", havingValue = "true", matchIfMissing = true)
static class EnabledCondition {
}
}