Skip to content

Commit 668ddb4

Browse files
committed
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
1 parent 7298b2c commit 668ddb4

File tree

17 files changed

+1408
-22
lines changed

17 files changed

+1408
-22
lines changed

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
<properties>
2525
<jline.version>3.21.0</jline.version>
2626
<jcommander.version>1.81</jcommander.version>
27+
<antlr-st4.version>4.3.1</antlr-st4.version>
2728
</properties>
2829

2930
<modules>
@@ -98,6 +99,11 @@
9899
<version>${jcommander.version}</version>
99100
<optional>true</optional>
100101
</dependency>
102+
<dependency>
103+
<groupId>org.antlr</groupId>
104+
<artifactId>ST4</artifactId>
105+
<version>${antlr-st4.version}</version>
106+
</dependency>
101107
</dependencies>
102108
</dependencyManagement>
103109

spring-shell-autoconfigure/src/main/java/org/springframework/shell/boot/SpringShellProperties.java

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2021 the original author or authors.
2+
* Copyright 2021-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -179,6 +179,28 @@ public void setEnabled(boolean enabled) {
179179
}
180180
}
181181

182+
public static class CompletionCommand {
183+
184+
private boolean enabled = true;
185+
private String rootCommand;
186+
187+
public boolean isEnabled() {
188+
return enabled;
189+
}
190+
191+
public void setEnabled(boolean enabled) {
192+
this.enabled = enabled;
193+
}
194+
195+
public String getRootCommand() {
196+
return rootCommand;
197+
}
198+
199+
public void setRootCommand(String rootCommand) {
200+
this.rootCommand = rootCommand;
201+
}
202+
}
203+
182204
public static class Command {
183205

184206
private HelpCommand help = new HelpCommand();
@@ -187,6 +209,7 @@ public static class Command {
187209
private StacktraceCommand stacktrace = new StacktraceCommand();
188210
private ScriptCommand script = new ScriptCommand();
189211
private HistoryCommand history = new HistoryCommand();
212+
private CompletionCommand completion = new CompletionCommand();
190213

191214
public void setHelp(HelpCommand help) {
192215
this.help = help;
@@ -235,5 +258,13 @@ public HistoryCommand getHistory() {
235258
public void setHistory(HistoryCommand history) {
236259
this.history = history;
237260
}
261+
262+
public CompletionCommand getCompletion() {
263+
return completion;
264+
}
265+
266+
public void setCompletion(CompletionCommand completion) {
267+
this.completion = completion;
268+
}
238269
}
239270
}

spring-shell-autoconfigure/src/main/java/org/springframework/shell/boot/StandardCommandsAutoConfiguration.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017-2021 the original author or authors.
2+
* Copyright 2017-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -22,10 +22,14 @@
2222
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
2323
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
2424
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
25+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
2526
import org.springframework.context.annotation.Bean;
27+
import org.springframework.context.annotation.Conditional;
2628
import org.springframework.context.annotation.Configuration;
29+
import org.springframework.shell.boot.condition.OnCompletionCommandCondition;
2730
import org.springframework.shell.result.ThrowableResultHandler;
2831
import org.springframework.shell.standard.commands.Clear;
32+
import org.springframework.shell.standard.commands.Completion;
2933
import org.springframework.shell.standard.commands.Help;
3034
import org.springframework.shell.standard.commands.History;
3135
import org.springframework.shell.standard.commands.Quit;
@@ -39,6 +43,7 @@
3943
*/
4044
@Configuration(proxyBeanMethods = false)
4145
@ConditionalOnClass({ Help.Command.class })
46+
@EnableConfigurationProperties(SpringShellProperties.class)
4247
public class StandardCommandsAutoConfiguration {
4348

4449
@Bean
@@ -82,4 +87,11 @@ public Script script(Parser parser) {
8287
public History historyCommand(org.jline.reader.History jLineHistory) {
8388
return new History(jLineHistory);
8489
}
90+
91+
@Bean
92+
@ConditionalOnMissingBean(Completion.Command.class)
93+
@Conditional(OnCompletionCommandCondition.class)
94+
public Completion completion(SpringShellProperties properties) {
95+
return new Completion(properties.getCommand().getCompletion().getRootCommand());
96+
}
8597
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright 2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.shell.boot.condition;
17+
18+
import org.springframework.boot.autoconfigure.condition.AllNestedConditions;
19+
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
20+
21+
public class OnCompletionCommandCondition extends AllNestedConditions {
22+
23+
public OnCompletionCommandCondition() {
24+
super(ConfigurationPhase.REGISTER_BEAN);
25+
}
26+
27+
@ConditionalOnProperty(prefix = "spring.shell.command.completion", value = "root-command")
28+
static class RootNameCondition {
29+
}
30+
31+
@ConditionalOnProperty(prefix = "spring.shell.command.completion", value = "enabled", havingValue = "true", matchIfMissing = true)
32+
static class EnabledCondition {
33+
}
34+
}

spring-shell-autoconfigure/src/test/java/org/springframework/shell/boot/SpringShellPropertiesTests.java

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2021 the original author or authors.
2+
* Copyright 2021-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,15 +15,10 @@
1515
*/
1616
package org.springframework.shell.boot;
1717

18-
import java.util.HashMap;
19-
import java.util.Map;
20-
2118
import org.junit.jupiter.api.Test;
2219

2320
import org.springframework.boot.context.properties.EnableConfigurationProperties;
2421
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
25-
import org.springframework.core.env.StandardEnvironment;
26-
import org.springframework.core.env.SystemEnvironmentPropertySource;
2722

2823
import static org.assertj.core.api.Assertions.assertThat;
2924

@@ -46,26 +41,25 @@ public void defaultNoPropertiesSet() {
4641
assertThat(properties.getCommand().getQuit().isEnabled()).isTrue();
4742
assertThat(properties.getCommand().getScript().isEnabled()).isTrue();
4843
assertThat(properties.getCommand().getStacktrace().isEnabled()).isTrue();
44+
assertThat(properties.getCommand().getCompletion().isEnabled()).isTrue();
45+
assertThat(properties.getCommand().getCompletion().getRootCommand()).isNull();
4946
});
5047
}
5148

5249
@Test
5350
public void setProperties() {
5451
this.contextRunner
55-
.withInitializer(context -> {
56-
Map<String, Object> map = new HashMap<>();
57-
map.put("spring.shell.script.enabled", "false");
58-
map.put("spring.shell.interactive.enabled", "false");
59-
map.put("spring.shell.noninteractive.enabled", "false");
60-
map.put("spring.shell.command.clear.enabled", "false");
61-
map.put("spring.shell.command.help.enabled", "false");
62-
map.put("spring.shell.command.history.enabled", "false");
63-
map.put("spring.shell.command.quit.enabled", "false");
64-
map.put("spring.shell.command.script.enabled", "false");
65-
map.put("spring.shell.command.stacktrace.enabled", "false");
66-
context.getEnvironment().getPropertySources().addLast(new SystemEnvironmentPropertySource(
67-
StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, map));
68-
})
52+
.withPropertyValues("spring.shell.script.enabled=false")
53+
.withPropertyValues("spring.shell.interactive.enabled=false")
54+
.withPropertyValues("spring.shell.noninteractive.enabled=false")
55+
.withPropertyValues("spring.shell.command.clear.enabled=false")
56+
.withPropertyValues("spring.shell.command.help.enabled=false")
57+
.withPropertyValues("spring.shell.command.history.enabled=false")
58+
.withPropertyValues("spring.shell.command.quit.enabled=false")
59+
.withPropertyValues("spring.shell.command.script.enabled=false")
60+
.withPropertyValues("spring.shell.command.stacktrace.enabled=false")
61+
.withPropertyValues("spring.shell.command.completion.enabled=false")
62+
.withPropertyValues("spring.shell.command.completion.root-command=fake")
6963
.withUserConfiguration(Config1.class)
7064
.run((context) -> {
7165
SpringShellProperties properties = context.getBean(SpringShellProperties.class);
@@ -78,6 +72,8 @@ public void setProperties() {
7872
assertThat(properties.getCommand().getQuit().isEnabled()).isFalse();
7973
assertThat(properties.getCommand().getScript().isEnabled()).isFalse();
8074
assertThat(properties.getCommand().getStacktrace().isEnabled()).isFalse();
75+
assertThat(properties.getCommand().getCompletion().isEnabled()).isFalse();
76+
assertThat(properties.getCommand().getCompletion().getRootCommand()).isEqualTo("fake");
8177
});
8278
}
8379

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright 2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.shell.boot;
17+
18+
import java.util.function.Function;
19+
20+
import org.junit.jupiter.api.Test;
21+
22+
import org.springframework.boot.autoconfigure.AutoConfigurations;
23+
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
24+
import org.springframework.shell.standard.commands.Completion;
25+
26+
import static org.assertj.core.api.Assertions.assertThat;
27+
28+
public class StandardCommandsAutoConfigurationTests {
29+
30+
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
31+
.withConfiguration(AutoConfigurations.of(StandardCommandsAutoConfiguration.class));
32+
33+
@Test
34+
public void testCompletionCommand() {
35+
this.contextRunner
36+
.with(disableCommands("help", "clear", "quit", "stacktrace", "script", "history"))
37+
.run((context) -> {assertThat(context).doesNotHaveBean(Completion.class);
38+
});
39+
this.contextRunner
40+
.with(disableCommands("help", "clear", "quit", "stacktrace", "script", "history", "completion"))
41+
.withPropertyValues("spring.shell.command.completion.root-command=fake")
42+
.run((context) -> {assertThat(context).doesNotHaveBean(Completion.class);
43+
});
44+
this.contextRunner
45+
.with(disableCommands("help", "clear", "quit", "stacktrace", "script", "history"))
46+
.withPropertyValues("spring.shell.command.completion.root-command=fake")
47+
.run((context) -> {assertThat(context).hasSingleBean(Completion.class);
48+
});
49+
}
50+
51+
private static Function<ApplicationContextRunner, ApplicationContextRunner> disableCommands(String... commands) {
52+
return (cr) -> {
53+
for (String command : commands) {
54+
cr = cr.withPropertyValues(String.format("spring.shell.command.%s.enabled=false", command));
55+
}
56+
return cr;
57+
};
58+
}
59+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
11
logging:
22
level:
33
root: error
4+
spring:
5+
main:
6+
banner-mode: off
7+
shell:
8+
command:
9+
completion:
10+
root-command: spring-shell-samples
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright 2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.shell.standard.commands;
17+
18+
import java.util.stream.Collectors;
19+
20+
import org.springframework.context.ResourceLoaderAware;
21+
import org.springframework.core.io.ResourceLoader;
22+
import org.springframework.shell.standard.AbstractShellComponent;
23+
import org.springframework.shell.standard.ShellComponent;
24+
import org.springframework.shell.standard.ShellMethod;
25+
import org.springframework.shell.standard.completion.BashCompletions;
26+
27+
/**
28+
* Command to create a shell completion files, i.e. for {@code bash}.
29+
*
30+
* @author Janne Valkealahti
31+
*/
32+
@ShellComponent
33+
public class Completion extends AbstractShellComponent implements ResourceLoaderAware {
34+
35+
/**
36+
* Marker interface used in auto-config.
37+
*/
38+
public interface Command {
39+
}
40+
41+
private ResourceLoader resourceLoader;
42+
private String rootCommand;
43+
44+
public Completion(String rootCommand) {
45+
this.rootCommand = rootCommand;
46+
}
47+
48+
@Override
49+
public void setResourceLoader(ResourceLoader resourceLoader) {
50+
this.resourceLoader = resourceLoader;
51+
}
52+
53+
@ShellMethod(key = "completion bash", value = "Generate bash completion script")
54+
public String bash() {
55+
BashCompletions bashCompletions = new BashCompletions(resourceLoader, getCommandRegistry(),
56+
getParameterResolver().collect(Collectors.toList()));
57+
return bashCompletions.generate(rootCommand);
58+
}
59+
}

spring-shell-standard/pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
<groupId>org.springframework.shell</groupId>
1919
<artifactId>spring-shell-core</artifactId>
2020
</dependency>
21+
<dependency>
22+
<groupId>org.antlr</groupId>
23+
<artifactId>ST4</artifactId>
24+
</dependency>
2125
<dependency>
2226
<groupId>org.springframework.boot</groupId>
2327
<artifactId>spring-boot-starter-test</artifactId>

0 commit comments

Comments
 (0)