Fixes for shells

This commit is contained in:
Christopher Schnick
2022-12-07 21:55:58 +01:00
parent c15c1204f0
commit 1982661aa1
10 changed files with 78 additions and 42 deletions
@@ -47,7 +47,7 @@ public interface OsType {
@Override
public String getTempDirectory(ShellProcessControl pc) throws Exception {
return pc.executeSimpleCommand(ShellTypes.CMD, pc.getShellType().getPrintVariableCommand("TEMP"));
return pc.executeSimpleCommand(ShellTypes.CMD, ShellTypes.CMD.getPrintVariableCommand("TEMP"));
}
@Override
@@ -111,7 +111,7 @@ public interface OsType {
@Override
public String determineOperatingSystemName(ShellProcessControl pc) throws Exception {
try (CommandProcessControl c =
pc.command(ShellTypes.SH, "lsb_release -a").start()) {
pc.command("lsb_release -a").start()) {
var text = c.readOnlyStdout();
if (c.getExitCode() == 0) {
return PropertiesFormatsParser.parse(text, ":").getOrDefault("Description", null);
@@ -119,7 +119,7 @@ public interface OsType {
}
try (CommandProcessControl c =
pc.command(ShellTypes.SH, "cat /etc/*release").start()) {
pc.command("cat /etc/*release").start()) {
var text = c.readOnlyStdout();
if (c.getExitCode() == 0) {
return PropertiesFormatsParser.parse(text, "=").getOrDefault("PRETTY_NAME", null);
@@ -127,7 +127,7 @@ public interface OsType {
}
String type = "Unknown";
try (CommandProcessControl c = pc.command(ShellTypes.SH, "uname -o").start()) {
try (CommandProcessControl c = pc.command("uname -o").start()) {
var text = c.readOnlyStdout();
if (c.getExitCode() == 0) {
type = text.strip();
@@ -135,7 +135,7 @@ public interface OsType {
}
String version = "?";
try (CommandProcessControl c = pc.command(ShellTypes.SH, "uname -r").start()) {
try (CommandProcessControl c = pc.command("uname -r").start()) {
var text = c.readOnlyStdout();
if (c.getExitCode() == 0) {
version = text.strip();
@@ -7,7 +7,6 @@ import java.io.IOException;
import java.util.List;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
public interface ShellProcessControl extends ProcessControl {
@@ -30,7 +29,9 @@ public interface ShellProcessControl extends ProcessControl {
}
default String executeSimpleCommand(ShellType type, String command) throws Exception {
return executeSimpleCommand(type.switchTo(command));
try (var sub = subShell(type).start()) {
return sub.executeSimpleCommand(command);
}
}
void restart() throws Exception;
@@ -53,13 +54,8 @@ public interface ShellProcessControl extends ProcessControl {
return subShell(type.openCommand()).elevation(getElevationPassword());
}
default CommandProcessControl command(@NonNull ShellType type, String command) {
return command(type.switchTo(command));
}
default ShellProcessControl subShell(@NonNull List<String> command) {
return subShell(
command.stream().map(s -> s.contains(" ") ? "\"" + s + "\"" : s).collect(Collectors.joining(" ")));
return subShell(shellProcessControl -> shellProcessControl.getShellType().flatten(command));
}
default ShellProcessControl subShell(@NonNull String command) {
@@ -23,16 +23,15 @@ public interface ShellType {
String getOpenWithInitFileCommand(String file);
default String flatten(List<String> command) {
return command.stream().map(s -> s.contains(" ") && !(s.startsWith("\"") && s.endsWith("\"")) ? "\"" + s + "\"" : s).collect(Collectors.joining(" "));
return command.stream()
.map(s -> s.contains(" ") && !(s.startsWith("\"") && s.endsWith("\"")) ? "\"" + s + "\"" : s)
.collect(Collectors.joining(" "));
}
default String joinCommands(String... s) {
return String.join(getConcatenationOperator(), s);
}
String escape(String input);
void elevate(ShellProcessControl control, String command, String displayCommand) throws Exception;
default String getExitCommand() {
@@ -57,13 +56,21 @@ public interface ShellType {
String elevateConsoleCommand(ShellProcessControl control, String command);
default String getScriptEchoCommand(String s) {
return getEchoCommand(s, false);
}
String getEchoCommand(String s, boolean toErrorStream);
String queryShellProcessId(ShellProcessControl control) throws Exception;
String getSetVariableCommand(String variableName, String value);
String getPrintVariableCommand(String name);
default String getPrintVariableCommand(String name) {
return getPrintVariableCommand("", name);
}
String getPrintVariableCommand(String prefix, String name);
List<String> openCommand();
@@ -1,28 +1,14 @@
package io.xpipe.core.store;
import io.xpipe.core.process.ShellProcessControl;
import java.io.InputStream;
import java.io.OutputStream;
public interface MachineStore extends FileSystemStore, ShellStore {
@Override
default void validate() throws Exception {
try (ShellProcessControl pc = create().start()) {}
}
public default boolean isLocal() {
return false;
}
public default String queryMachineName() throws Exception {
try (var pc = create().start()) {
var operatingSystem = pc.getOsType();
return operatingSystem.determineOperatingSystemName(pc);
}
}
@Override
public default InputStream openInput(String file) throws Exception {
return create().commandListFunction(proc -> proc.getShellType().createFileReadCommand(proc.getOsType().normalizeFileName(file)))
@@ -21,4 +21,15 @@ public interface ShellStore extends DataStore {
return pc.getShellType();
}
}
@Override
default void validate() throws Exception {
try (ShellProcessControl pc = create().start()) {}
}
public default String queryMachineName() throws Exception {
try (var pc = create().start()) {
var operatingSystem = pc.getOsType();
return operatingSystem.determineOperatingSystemName(pc);
}
}
}