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 {
}
}

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.
@@ -15,15 +15,10 @@
*/
package org.springframework.shell.boot;
import java.util.HashMap;
import java.util.Map;
import org.junit.jupiter.api.Test;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.core.env.StandardEnvironment;
import org.springframework.core.env.SystemEnvironmentPropertySource;
import static org.assertj.core.api.Assertions.assertThat;
@@ -46,26 +41,25 @@ public class SpringShellPropertiesTests {
assertThat(properties.getCommand().getQuit().isEnabled()).isTrue();
assertThat(properties.getCommand().getScript().isEnabled()).isTrue();
assertThat(properties.getCommand().getStacktrace().isEnabled()).isTrue();
assertThat(properties.getCommand().getCompletion().isEnabled()).isTrue();
assertThat(properties.getCommand().getCompletion().getRootCommand()).isNull();
});
}
@Test
public void setProperties() {
this.contextRunner
.withInitializer(context -> {
Map<String, Object> map = new HashMap<>();
map.put("spring.shell.script.enabled", "false");
map.put("spring.shell.interactive.enabled", "false");
map.put("spring.shell.noninteractive.enabled", "false");
map.put("spring.shell.command.clear.enabled", "false");
map.put("spring.shell.command.help.enabled", "false");
map.put("spring.shell.command.history.enabled", "false");
map.put("spring.shell.command.quit.enabled", "false");
map.put("spring.shell.command.script.enabled", "false");
map.put("spring.shell.command.stacktrace.enabled", "false");
context.getEnvironment().getPropertySources().addLast(new SystemEnvironmentPropertySource(
StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, map));
})
.withPropertyValues("spring.shell.script.enabled=false")
.withPropertyValues("spring.shell.interactive.enabled=false")
.withPropertyValues("spring.shell.noninteractive.enabled=false")
.withPropertyValues("spring.shell.command.clear.enabled=false")
.withPropertyValues("spring.shell.command.help.enabled=false")
.withPropertyValues("spring.shell.command.history.enabled=false")
.withPropertyValues("spring.shell.command.quit.enabled=false")
.withPropertyValues("spring.shell.command.script.enabled=false")
.withPropertyValues("spring.shell.command.stacktrace.enabled=false")
.withPropertyValues("spring.shell.command.completion.enabled=false")
.withPropertyValues("spring.shell.command.completion.root-command=fake")
.withUserConfiguration(Config1.class)
.run((context) -> {
SpringShellProperties properties = context.getBean(SpringShellProperties.class);
@@ -78,6 +72,8 @@ public class SpringShellPropertiesTests {
assertThat(properties.getCommand().getQuit().isEnabled()).isFalse();
assertThat(properties.getCommand().getScript().isEnabled()).isFalse();
assertThat(properties.getCommand().getStacktrace().isEnabled()).isFalse();
assertThat(properties.getCommand().getCompletion().isEnabled()).isFalse();
assertThat(properties.getCommand().getCompletion().getRootCommand()).isEqualTo("fake");
});
}

View File

@@ -0,0 +1,59 @@
/*
* 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;
import java.util.function.Function;
import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.shell.standard.commands.Completion;
import static org.assertj.core.api.Assertions.assertThat;
public class StandardCommandsAutoConfigurationTests {
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(StandardCommandsAutoConfiguration.class));
@Test
public void testCompletionCommand() {
this.contextRunner
.with(disableCommands("help", "clear", "quit", "stacktrace", "script", "history"))
.run((context) -> {assertThat(context).doesNotHaveBean(Completion.class);
});
this.contextRunner
.with(disableCommands("help", "clear", "quit", "stacktrace", "script", "history", "completion"))
.withPropertyValues("spring.shell.command.completion.root-command=fake")
.run((context) -> {assertThat(context).doesNotHaveBean(Completion.class);
});
this.contextRunner
.with(disableCommands("help", "clear", "quit", "stacktrace", "script", "history"))
.withPropertyValues("spring.shell.command.completion.root-command=fake")
.run((context) -> {assertThat(context).hasSingleBean(Completion.class);
});
}
private static Function<ApplicationContextRunner, ApplicationContextRunner> disableCommands(String... commands) {
return (cr) -> {
for (String command : commands) {
cr = cr.withPropertyValues(String.format("spring.shell.command.%s.enabled=false", command));
}
return cr;
};
}
}