Components can't use interactive mode without tty

- Adding a concept of no-tty which in this commit simply
  tracks DumbTerminal as jline creates that if there nothing
  better.
- For components without tty don't go to interaction loop.
- For new sample show that we can at least manually handle
  required option with a flow while command option is not
  required.
- Fixes #444
This commit is contained in:
Janne Valkealahti
2022-06-15 07:10:56 +01:00
parent a019934169
commit 195d1d00ab
18 changed files with 349 additions and 9 deletions

View File

@@ -23,15 +23,23 @@ import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import org.jline.terminal.impl.DumbTerminal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.shell.command.CommandExecution.CommandParserExceptionsException;
import org.springframework.shell.command.CommandParser;
import org.springframework.shell.command.CommandParser.CommandParserException;
import org.springframework.shell.command.CommandRegistration;
import org.springframework.shell.component.flow.ComponentFlow;
import org.springframework.shell.component.flow.ComponentFlow.ComponentFlowResult;
import org.springframework.shell.component.flow.ResultMode;
import org.springframework.shell.component.flow.SelectItem;
import org.springframework.shell.component.flow.ComponentFlow.ComponentFlowResult;
import org.springframework.shell.standard.AbstractShellComponent;
import org.springframework.shell.standard.ShellComponent;
import org.springframework.shell.standard.ShellMethod;
import org.springframework.shell.standard.ShellOption;
import org.springframework.util.StringUtils;
@ShellComponent
public class ComponentFlowCommands extends AbstractShellComponent {
@@ -133,6 +141,117 @@ public class ComponentFlowCommands extends AbstractShellComponent {
return buf.toString();
}
@Bean
public CommandRegistration showcaseRegistration() {
return CommandRegistration.builder()
.command("flow", "showcase3")
.description("Showcase")
.withOption()
.longNames("field1")
.and()
.withOption()
.longNames("field2")
.and()
.withOption()
.longNames("confirmation1")
.type(Boolean.class)
.and()
.withOption()
.longNames("path1")
.and()
.withOption()
.longNames("single1")
.and()
.withOption()
.longNames("multi1")
.and()
.withTarget()
.consumer(ctx -> {
String field1 = ctx.getOptionValue("field1");
String field2 = ctx.getOptionValue("field2");
Boolean confirmation1 = ctx.getOptionValue("confirmation1");
String path1 = ctx.getOptionValue("path1");
String single1 = ctx.getOptionValue("single1");
String asdf = ctx.getOptionValue("multi1");
List<String> multi1 = new ArrayList<>();
if (StringUtils.hasText(asdf)) {
multi1.add(asdf);
}
Map<String, String> single1SelectItems = new HashMap<>();
single1SelectItems.put("key1", "value1");
single1SelectItems.put("key2", "value2");
List<SelectItem> multi1SelectItems = Arrays.asList(SelectItem.of("key1", "value1"),
SelectItem.of("key2", "value2"), SelectItem.of("key3", "value3"));
ComponentFlow flow = componentFlowBuilder.clone().reset()
.withStringInput("field1")
.name("Field1")
.defaultValue("defaultField1Value")
.resultValue(field1)
.resultMode(ResultMode.ACCEPT)
.and()
.withStringInput("field2")
.name("Field2")
.resultValue(field2)
.resultMode(ResultMode.ACCEPT)
.and()
.withConfirmationInput("confirmation1")
.name("Confirmation1")
.resultValue(confirmation1)
.resultMode(ResultMode.ACCEPT)
.and()
.withPathInput("path1")
.name("Path1")
.resultValue(path1)
.resultMode(ResultMode.ACCEPT)
.and()
.withSingleItemSelector("single1")
.name("Single1")
.selectItems(single1SelectItems)
.resultValue(single1)
.resultMode(ResultMode.ACCEPT)
.and()
.withMultiItemSelector("multi1")
.name("Multi1")
.selectItems(multi1SelectItems)
.resultValues(multi1)
.resultMode(ResultMode.ACCEPT)
.and()
.build();
ComponentFlowResult result = flow.run();
boolean hasTty = !((ctx.getTerminal() instanceof DumbTerminal) && ctx.getTerminal().getSize().getRows() == 0);
if (hasTty) {
StringBuilder buf = new StringBuilder();
result.getContext().stream().forEach(e -> {
buf.append(e.getKey());
buf.append(" = ");
buf.append(e.getValue());
buf.append("\n");
});
ctx.getTerminal().writer().print(buf.toString());
ctx.getTerminal().writer().flush();
}
else {
List<CommandParser.CommandParserException> errors = new ArrayList<>();
result.getContext().stream().forEach(e -> {
if (e.getValue() == null) {
errors.add(CommandParserException.of(String.format("Missing option, longnames='%s'", e.getKey())));
}
});
if (!result.getContext().containsKey("single1")) {
errors.add(CommandParserException.of("Missing option, longnames='single'"));
}
if (!errors.isEmpty()) {
throw CommandParserExceptionsException.of("Missing options", errors);
}
}
})
.and()
.build();
}
@ShellMethod(key = "flow conditional", value = "Second component based on first", group = "Flow")
public void conditional() {
Map<String, String> single1SelectItems = new HashMap<>();