Small fixes for shells

This commit is contained in:
Christopher Schnick 2022-12-10 00:16:05 +01:00
parent b3cfae5a92
commit 1ca9e2c751
4 changed files with 27 additions and 25 deletions

View file

@ -64,42 +64,41 @@ public interface ShellProcessControl extends ProcessControl {
SecretValue getElevationPassword();
default ShellProcessControl subShell(@NonNull ShellType type) {
return subShell(type.openCommand()).elevation(getElevationPassword());
return subShell(p -> type.openCommand(), (shellProcessControl, s) -> {
return s == null ? type.openCommand() : type.switchTo(s);
})
.elevation(getElevationPassword());
}
default ShellProcessControl subShell(@NonNull List<String> command) {
return subShell(shellProcessControl -> shellProcessControl.getShellType().flatten(command));
return subShell(
shellProcessControl -> shellProcessControl.getShellType().flatten(command), null);
}
default ShellProcessControl subShell(@NonNull String command) {
return subShell(processControl -> command);
return subShell(processControl -> command, null);
}
ShellProcessControl subShell(@NonNull Function<ShellProcessControl, String> command);
default ShellProcessControl consoleCommand(@NonNull String command) {
return consoleCommand((shellProcessControl, c) -> command);
}
ShellProcessControl consoleCommand(@NonNull BiFunction<ShellProcessControl, String, String> command);
ShellProcessControl subShell(
@NonNull Function<ShellProcessControl, String> command,
BiFunction<ShellProcessControl, String, String> terminalCommand);
void executeCommand(String command) throws Exception;
@Override
ShellProcessControl start() throws Exception;
default CommandProcessControl commandListFunction(Function<ShellProcessControl, List<String>> command) {
return commandFunction(shellProcessControl -> shellProcessControl.getShellType().flatten(command.apply(shellProcessControl)));
}
CommandProcessControl command(Function<ShellProcessControl, String> command);
CommandProcessControl commandFunction(Function<ShellProcessControl, String> command);
CommandProcessControl command(
Function<ShellProcessControl, String> command, Function<ShellProcessControl, String> terminalCommand);
default CommandProcessControl command(String command){
return commandFunction(shellProcessControl -> command);
default CommandProcessControl command(String command) {
return command(shellProcessControl -> command);
}
default CommandProcessControl command(List<String> command) {
return commandFunction(shellProcessControl -> shellProcessControl.getShellType().flatten(command));
return command(shellProcessControl -> shellProcessControl.getShellType().flatten(command));
}
void exitAndWait() throws IOException;

View file

@ -24,7 +24,11 @@ public interface ShellType {
default String flatten(List<String> command) {
return command.stream()
.map(s -> s.contains(" ") && !(s.startsWith("\"") && s.endsWith("\"")) ? "\"" + s + "\"" : s)
.map(s -> s.contains(" ")
&& !(s.startsWith("\"") && s.endsWith("\""))
&& !(s.startsWith("'") && s.endsWith("'"))
? "\"" + s + "\""
: s)
.collect(Collectors.joining(" "));
}
@ -70,7 +74,7 @@ public interface ShellType {
String getPrintVariableCommand(String prefix, String name);
List<String> openCommand();
String openCommand();
String switchTo(String cmd);

View file

@ -11,19 +11,19 @@ public interface MachineStore extends FileSystemStore, ShellStore {
@Override
public default InputStream openInput(String file) throws Exception {
return create().commandListFunction(proc -> proc.getShellType().createFileReadCommand(proc.getOsType().normalizeFileName(file)))
return create().command(proc -> proc.getShellType().flatten(proc.getShellType().createFileReadCommand(proc.getOsType().normalizeFileName(file))))
.startExternalStdout();
}
@Override
public default OutputStream openOutput(String file) throws Exception {
return create().commandFunction(proc -> proc.getShellType().createFileWriteCommand(proc.getOsType().normalizeFileName(file)))
return create().command(proc -> proc.getShellType().createFileWriteCommand(proc.getOsType().normalizeFileName(file)))
.startExternalStdin();
}
@Override
public default boolean exists(String file) throws Exception {
try (var pc = create().commandFunction(proc -> proc.getShellType().createFileExistsCommand(proc.getOsType().normalizeFileName(file)))
try (var pc = create().command(proc -> proc.getShellType().createFileExistsCommand(proc.getOsType().normalizeFileName(file)))
.start()) {
return pc.discardAndCheckExit();
}
@ -31,7 +31,7 @@ public interface MachineStore extends FileSystemStore, ShellStore {
@Override
public default boolean mkdirs(String file) throws Exception {
try (var pc = create().commandListFunction(proc -> proc.getShellType().createMkdirsCommand(proc.getOsType().normalizeFileName(file)))
try (var pc = create().command(proc -> proc.getShellType().flatten(proc.getShellType().createMkdirsCommand(proc.getOsType().normalizeFileName(file))))
.start()) {
return pc.discardAndCheckExit();
}

View file

@ -97,7 +97,7 @@ public class CustomComboBoxBuilder<T> {
cb.setButtonCell(new SelectedCell());
SimpleChangeListener.apply(selected, c -> {
var item = nodeMap.entrySet().stream()
.filter(e -> e.getValue() != null && e.getValue().equals(c))
.filter(e -> Objects.equals(c, e.getValue()))
.map(e -> e.getKey())
.findAny()
.orElse(null);
@ -118,7 +118,6 @@ public class CustomComboBoxBuilder<T> {
});
if (filterPredicate != null) {
SimpleChangeListener.apply(filterString, c -> {
var filteredNodes = nodes.stream()
.filter(e -> e.equals(cb.getValue())