Small fixes for shells

This commit is contained in:
Christopher Schnick 2022-12-10 00:16:05 +01:00
parent f9b9808dd2
commit bad71d3db6
5 changed files with 37 additions and 35 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

@ -22,9 +22,9 @@ public class ShellTypes {
public static ShellType getRecommendedDefault() {
if (System.getProperty("os.name").startsWith("Windows")) {
return POWERSHELL;
return CMD;
} else {
return SH;
return BASH;
}
}
@ -139,13 +139,13 @@ public class ShellTypes {
}
@Override
public List<String> openCommand() {
return List.of("cmd");
public String openCommand() {
return "cmd";
}
@Override
public String switchTo(String cmd) {
return "cmd.exe /V:on /c '" + cmd + "'";
return "cmd.exe /C " + cmd + "";
}
@Override
@ -319,8 +319,8 @@ public class ShellTypes {
}
@Override
public List<String> openCommand() {
return List.of("powershell", "/nologo");
public String openCommand() {
return "powershell /nologo";
}
@Override
@ -490,13 +490,13 @@ public class ShellTypes {
}
@Override
public List<String> openCommand() {
return List.of(getName(), "-i", "-l");
public String openCommand() {
return getName() + " -i -l";
}
@Override
public String switchTo(String cmd) {
return getName() + " -c '" + cmd + "'";
return getName() + " -i -l -c '" + cmd + "'";
}
@Override

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())