ConfirmationInput should have resultValue

- Add flow hooks to ConfirmationInput so that user
  is able to pass a flag from an options so that
  component can bypass interactive mode.
- Fixes #445
This commit is contained in:
Janne Valkealahti
2022-06-13 09:27:16 +01:00
parent 073cd8562c
commit 25721dd63d
5 changed files with 114 additions and 2 deletions

View File

@@ -35,6 +35,8 @@ public abstract class BaseConfirmationInput extends BaseInput<ConfirmationInputS
private String name;
private Boolean defaultValue;
private Boolean resultValue;
private ResultMode resultMode;
private Function<ConfirmationInputContext, List<AttributedString>> renderer;
private List<Consumer<ConfirmationInputContext>> preHandlers = new ArrayList<>();
private List<Consumer<ConfirmationInputContext>> postHandlers = new ArrayList<>();
@@ -52,6 +54,18 @@ public abstract class BaseConfirmationInput extends BaseInput<ConfirmationInputS
return this;
}
@Override
public ConfirmationInputSpec resultValue(Boolean resultValue) {
this.resultValue = resultValue;
return this;
}
@Override
public ConfirmationInputSpec resultMode(ResultMode resultMode) {
this.resultMode = resultMode;
return this;
}
@Override
public ConfirmationInputSpec defaultValue(Boolean defaultValue) {
this.defaultValue = defaultValue;
@@ -113,6 +127,14 @@ public abstract class BaseConfirmationInput extends BaseInput<ConfirmationInputS
return defaultValue != null ? defaultValue : true;
}
public Boolean getResultValue() {
return resultValue;
}
public ResultMode getResultMode() {
return resultMode;
}
public Function<ConfirmationInputContext, List<AttributedString>> getRenderer() {
return renderer;
}

View File

@@ -521,6 +521,11 @@ public interface ComponentFlow {
return confirmationInputs.stream().map(input -> {
ConfirmationInput selector = new ConfirmationInput(terminal, input.getName(), input.getDefaultValue());
Function<ComponentContext<?>, ComponentContext<?>> operation = (context) -> {
if (input.getResultMode() == ResultMode.ACCEPT && input.isStoreResult()
&& input.getResultValue() != null) {
context.put(input.getId(), input.getResultValue());
return context;
}
selector.setResourceLoader(resourceLoader);
selector.setTemplateExecutor(templateExecutor);
if (StringUtils.hasText(input.getTemplateLocation())) {

View File

@@ -39,6 +39,21 @@ public interface ConfirmationInputSpec extends BaseInputSpec<ConfirmationInputSp
* @return a builder
*/
ConfirmationInputSpec name(String name);
/**
* Sets a result value.
*
* @param resultValue the result value
* @return a builder
*/
ConfirmationInputSpec resultValue(Boolean resultValue);
/**
* Sets a result mode.
*
* @param resultMode the result mode
* @return a builder
*/
ConfirmationInputSpec resultMode(ResultMode resultMode);
/**
* Sets a default value.

View File

@@ -128,6 +128,10 @@ public class ComponentFlowTests extends AbstractShellTests {
.resultValues(Arrays.asList("value4"))
.resultMode(ResultMode.ACCEPT)
.and()
.withConfirmationInput("id5")
.resultValue(false)
.resultMode(ResultMode.ACCEPT)
.and()
.build();
ExecutorService service = Executors.newFixedThreadPool(1);
@@ -147,11 +151,13 @@ public class ComponentFlowTests extends AbstractShellTests {
Path id2 = inputWizardResult.getContext().get("id2");
String id3 = inputWizardResult.getContext().get("id3");
List<String> id4 = inputWizardResult.getContext().get("id4");
Boolean id5 = inputWizardResult.getContext().get("id5");
assertThat(id1).isEqualTo("value1");
assertThat(id2.toString()).contains("value2");
assertThat(id3).isEqualTo("value3");
assertThat(id4).containsExactlyInAnyOrder("value4");
assertThat(id5).isFalse();
}
@Test

View File

@@ -15,6 +15,7 @@
*/
package org.springframework.shell.samples.standard;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
@@ -24,7 +25,9 @@ import java.util.stream.IntStream;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.shell.component.flow.ComponentFlow;
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;
@@ -36,8 +39,8 @@ public class ComponentFlowCommands extends AbstractShellComponent {
@Autowired
private ComponentFlow.Builder componentFlowBuilder;
@ShellMethod(key = "flow showcase", value = "Showcase", group = "Flow")
public void showcase() {
@ShellMethod(key = "flow showcase1", value = "Showcase", group = "Flow")
public void showcase1() {
Map<String, String> single1SelectItems = new HashMap<>();
single1SelectItems.put("key1", "value1");
single1SelectItems.put("key2", "value2");
@@ -69,6 +72,67 @@ public class ComponentFlowCommands extends AbstractShellComponent {
flow.run();
}
@ShellMethod(key = "flow showcase2", value = "Showcase with options", group = "Flow")
public String showcase2(
@ShellOption(help = "Field1 value", defaultValue = ShellOption.NULL) String field1,
@ShellOption(help = "Field2 value", defaultValue = ShellOption.NULL) String field2,
@ShellOption(help = "Confirmation1 value", defaultValue = ShellOption.NULL) Boolean confirmation1,
@ShellOption(help = "Path1 value", defaultValue = ShellOption.NULL) String path1,
@ShellOption(help = "Single1 value", defaultValue = ShellOption.NULL) String single1,
@ShellOption(help = "Multi1 value", defaultValue = ShellOption.NULL) List<String> multi1
) {
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"));
List<String> multi1ResultValues = multi1 != null ? multi1 : new ArrayList<>();
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(multi1ResultValues)
.resultMode(ResultMode.ACCEPT)
.and()
.build();
ComponentFlowResult result = flow.run();
StringBuilder buf = new StringBuilder();
result.getContext().stream().forEach(e -> {
buf.append(e.getKey());
buf.append(" = ");
buf.append(e.getValue());
buf.append("\n");
});
return buf.toString();
}
@ShellMethod(key = "flow conditional", value = "Second component based on first", group = "Flow")
public void conditional() {
Map<String, String> single1SelectItems = new HashMap<>();