Small fixes

This commit is contained in:
Christopher Schnick
2022-10-20 09:11:45 +02:00
parent d6cccd1d5a
commit aa07f88e1d
9 changed files with 80 additions and 34 deletions

View File

@@ -1,14 +1,11 @@
package io.xpipe.core.dialog;
import com.fasterxml.jackson.annotation.JsonCreator;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Value;
import lombok.extern.jackson.Jacksonized;
@Value
@Builder
@Jacksonized
@AllArgsConstructor
@AllArgsConstructor(onConstructor_ = @JsonCreator)
public class Choice {
/**

View File

@@ -30,6 +30,13 @@ public abstract class Dialog {
protected Object eval;
private Supplier<?> evaluation;
/**
* Removes all completion listeners. Intended for internal use only.
*/
public void clearCompletion() {
completion.clear();
}
/**
* Creates an empty dialogue. This dialogue completes immediately and does not handle any questions or answers.
*/

View File

@@ -0,0 +1,52 @@
package io.xpipe.core.dialog;
import java.util.LinkedHashMap;
public class DialogMapper {
private final Dialog dialog;
private final LinkedHashMap<String, String> map = new LinkedHashMap<>();
public DialogMapper(Dialog dialog) {
this.dialog = dialog;
dialog.clearCompletion();
}
public LinkedHashMap<String, String> handle() throws Exception {
var element = dialog.start();
handle(element);
return map;
}
private void handle(DialogElement element) throws Exception {
String response = null;
if (element instanceof ChoiceElement c) {
response = handleChoice(c);
}
if (element instanceof BaseQueryElement q) {
response = handleQuery(q);
}
var newElement = dialog.next(response);
if (element.equals(newElement)) {
throw new IllegalStateException(
"Loop for key " + newElement.toDisplayString());
}
element = newElement;
if (element != null) {
handle(element);
}
}
private String handleQuery(BaseQueryElement q) {
map.put(q.getDescription(), q.getValue());
return q.getValue();
}
private String handleChoice(ChoiceElement c) {
map.put(c.getDescription(), c.getElements().get(c.getSelected()).getDescription());
return String.valueOf(c.getSelected() + 1);
}
}

View File

@@ -11,6 +11,11 @@ import java.util.List;
public interface StandardShellStore extends MachineFileStore, ShellStore {
public default ProcessControl prepareLocalCommand(List<SecretValue> input, List<String> cmd, Integer timeout)
throws Exception {
return prepareCommand(input, cmd, timeout, determineType().determineCharset(this));
}
public default boolean isLocal() {
return false;
}
@@ -19,7 +24,7 @@ public interface StandardShellStore extends MachineFileStore, ShellStore {
return determineType().getNewLine();
}
public abstract ShellType determineType() throws Exception;
ShellType determineType() throws Exception;
public default String querySystemName() throws Exception {
var result = prepareCommand(

View File

@@ -20,8 +20,6 @@ import io.xpipe.core.dialog.BaseQueryElement;
import io.xpipe.core.dialog.BusyElement;
import io.xpipe.core.dialog.ChoiceElement;
import io.xpipe.core.dialog.HeaderElement;
import io.xpipe.core.impl.TextSource;
import io.xpipe.core.impl.XpbtSource;
import io.xpipe.core.source.DataSource;
import io.xpipe.core.source.DataSourceInfo;
import io.xpipe.core.source.DataSourceReference;
@@ -36,14 +34,10 @@ public class CoreJacksonModule extends SimpleModule {
@Override
public void setupModule(SetupContext context) {
context.registerSubtypes(
new NamedType(TextSource.class),
new NamedType(XpbtSource.class),
new NamedType(FileStore.class),
new NamedType(StdinDataStore.class),
new NamedType(StdoutDataStore.class),
new NamedType(LocalDirectoryDataStore.class),
new NamedType(CollectionEntryDataStore.class),
new NamedType(InMemoryStore.class),
new NamedType(LocalStore.class),
new NamedType(NamedStore.class),
new NamedType(ValueType.class),