Separate interactive and non-interactive commands

- Add new ShellContext concept which now is just a way
  to stash info about interaction mode where ShellRunner
  can update supported mode.
- ShellMethod has a new field interactionMode which user
  can use to define commands between interactive/non-interactive
  modes which then prevents CommandRegistry to show
  commands at runtime.
- Fixes #345
This commit is contained in:
Janne Valkealahti
2022-01-09 14:10:15 +00:00
parent 3cb3309d68
commit dbe8a8b408
21 changed files with 284 additions and 32 deletions

View File

@@ -21,14 +21,16 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.shell.CommandRegistry;
import org.springframework.shell.ConfigurableCommandRegistry;
import org.springframework.shell.MethodTargetRegistrar;
import org.springframework.shell.context.ShellContext;
@Configuration(proxyBeanMethods = false)
public class CommandRegistryAutoConfiguration {
@Bean
public CommandRegistry commandRegistry(
ObjectProvider<MethodTargetRegistrar> methodTargetRegistrars) {
ConfigurableCommandRegistry registry = new ConfigurableCommandRegistry();
ObjectProvider<MethodTargetRegistrar> methodTargetRegistrars,
ShellContext shellContext) {
ConfigurableCommandRegistry registry = new ConfigurableCommandRegistry(shellContext);
methodTargetRegistrars.orderedStream().forEach(resolver -> {
resolver.register(registry);
});

View File

@@ -0,0 +1,15 @@
package org.springframework.shell.boot;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.shell.context.DefaultShellContext;
import org.springframework.shell.context.ShellContext;
@Configuration(proxyBeanMethods = false)
public class ShellContextAutoConfiguration {
@Bean
public ShellContext shellContext() {
return new DefaultShellContext();
}
}

View File

@@ -22,6 +22,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.shell.Shell;
import org.springframework.shell.context.ShellContext;
import org.springframework.shell.jline.InteractiveShellRunner;
import org.springframework.shell.jline.NonInteractiveShellRunner;
import org.springframework.shell.jline.PromptProvider;
@@ -34,24 +35,27 @@ public class ShellRunnerAutoConfiguration {
private PromptProvider promptProvider;
private LineReader lineReader;
private Parser parser;
private ShellContext shellContext;
public ShellRunnerAutoConfiguration(Shell shell, PromptProvider promptProvider, LineReader lineReader, Parser parser) {
public ShellRunnerAutoConfiguration(Shell shell, PromptProvider promptProvider, LineReader lineReader,
Parser parser, ShellContext shellContext) {
this.shell = shell;
this.promptProvider = promptProvider;
this.lineReader = lineReader;
this.parser = parser;
this.shellContext = shellContext;
}
@Bean
@ConditionalOnProperty(prefix = "spring.shell.interactive", value = "enabled", havingValue = "true", matchIfMissing = true)
public InteractiveShellRunner interactiveApplicationRunner() {
return new InteractiveShellRunner(lineReader, promptProvider, shell);
return new InteractiveShellRunner(lineReader, promptProvider, shell, shellContext);
}
@Bean
@ConditionalOnProperty(prefix = "spring.shell.noninteractive", value = "enabled", havingValue = "true", matchIfMissing = true)
public NonInteractiveShellRunner nonInteractiveApplicationRunner() {
return new NonInteractiveShellRunner(shell);
return new NonInteractiveShellRunner(shell, shellContext);
}
@Bean

View File

@@ -1,4 +1,5 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.shell.boot.ShellContextAutoConfiguration,\
org.springframework.shell.boot.SpringShellAutoConfiguration,\
org.springframework.shell.boot.ShellRunnerAutoConfiguration,\
org.springframework.shell.boot.ApplicationRunnerAutoConfiguration,\