Fixes for shells

This commit is contained in:
Christopher Schnick 2022-12-07 00:13:16 +01:00
parent 936095aa24
commit c15c1204f0
5 changed files with 41 additions and 35 deletions

View file

@ -71,6 +71,7 @@ public class BeaconProxyImpl extends ProxyProvider {
} }
@Override @Override
@SuppressWarnings("unchecked")
public <T extends DataSourceReadConnection> T createRemoteReadConnection(DataSource<?> source, ShellStore proxy) throws Exception { public <T extends DataSourceReadConnection> T createRemoteReadConnection(DataSource<?> source, ShellStore proxy) throws Exception {
var downstream = downstreamTransform(source, proxy); var downstream = downstreamTransform(source, proxy);
@ -99,6 +100,7 @@ public class BeaconProxyImpl extends ProxyProvider {
} }
@Override @Override
@SuppressWarnings("unchecked")
public <T extends DataSourceConnection> T createRemoteWriteConnection(DataSource<?> source, WriteMode mode, ShellStore proxy) throws Exception { public <T extends DataSourceConnection> T createRemoteWriteConnection(DataSource<?> source, WriteMode mode, ShellStore proxy) throws Exception {
var downstream = downstreamTransform(source, proxy); var downstream = downstreamTransform(source, proxy);

View file

@ -11,8 +11,6 @@ public interface OsType {
Linux LINUX = new Linux(); Linux LINUX = new Linux();
Mac MAC = new Mac(); Mac MAC = new Mac();
String getScriptFileEnding();
String getName(); String getName();
String getTempDirectory(ShellProcessControl pc) throws Exception; String getTempDirectory(ShellProcessControl pc) throws Exception;
@ -42,11 +40,6 @@ public interface OsType {
static class Windows implements OsType { static class Windows implements OsType {
@Override
public String getScriptFileEnding() {
return "bat";
}
@Override @Override
public String getName() { public String getName() {
return "Windows"; return "Windows";
@ -54,7 +47,7 @@ public interface OsType {
@Override @Override
public String getTempDirectory(ShellProcessControl pc) throws Exception { public String getTempDirectory(ShellProcessControl pc) throws Exception {
return pc.executeSimpleCommand(pc.getShellType().getPrintVariableCommand("TEMP")); return pc.executeSimpleCommand(ShellTypes.CMD, pc.getShellType().getPrintVariableCommand("TEMP"));
} }
@Override @Override
@ -65,7 +58,7 @@ public interface OsType {
@Override @Override
public Map<String, String> getProperties(ShellProcessControl pc) throws Exception { public Map<String, String> getProperties(ShellProcessControl pc) throws Exception {
try (CommandProcessControl c = try (CommandProcessControl c =
pc.shell(ShellTypes.CMD).command("systeminfo").start()) { pc.subShell(ShellTypes.CMD).command("systeminfo").start()) {
var text = c.readOrThrow(); var text = c.readOrThrow();
return PropertiesFormatsParser.parse(text, ":"); return PropertiesFormatsParser.parse(text, ":");
} }
@ -105,11 +98,6 @@ public interface OsType {
return String.join("/", file.split("[\\\\/]+")); return String.join("/", file.split("[\\\\/]+"));
} }
@Override
public String getScriptFileEnding() {
return "sh";
}
@Override @Override
public String getName() { public String getName() {
return "Linux"; return "Linux";
@ -180,11 +168,6 @@ public interface OsType {
return String.join("/", file.split("[\\\\/]+")); return String.join("/", file.split("[\\\\/]+"));
} }
@Override
public String getScriptFileEnding() {
return "sh";
}
@Override @Override
public String getName() { public String getName() {
return "Mac"; return "Mac";

View file

@ -8,6 +8,8 @@ import java.nio.charset.Charset;
public interface ProcessControl extends Closeable, AutoCloseable { public interface ProcessControl extends Closeable, AutoCloseable {
String prepareConsoleOpen(boolean keepOpen) throws Exception;
void closeStdin() throws IOException; void closeStdin() throws IOException;
boolean isStdinClosed(); boolean isStdinClosed();

View file

@ -11,11 +11,11 @@ import java.util.stream.Collectors;
public interface ShellProcessControl extends ProcessControl { public interface ShellProcessControl extends ProcessControl {
default String prepareOpen() throws Exception { default String prepareConsoleOpen(boolean keepOpen) throws Exception {
return prepareOpen(null); return prepareConsoleOpen(null, keepOpen);
} }
String prepareOpen(String content) throws Exception; String prepareConsoleOpen(String content, boolean keepOpen) throws Exception;
default String executeSimpleCommand(String command) throws Exception { default String executeSimpleCommand(String command) throws Exception {
try (CommandProcessControl c = command(command).start()) { try (CommandProcessControl c = command(command).start()) {
@ -33,10 +33,7 @@ public interface ShellProcessControl extends ProcessControl {
return executeSimpleCommand(type.switchTo(command)); return executeSimpleCommand(type.switchTo(command));
} }
default void restart() throws Exception { void restart() throws Exception;
exitAndWait();
start();
}
boolean isLocal(); boolean isLocal();
@ -52,24 +49,30 @@ public interface ShellProcessControl extends ProcessControl {
SecretValue getElevationPassword(); SecretValue getElevationPassword();
default ShellProcessControl shell(@NonNull ShellType type) { default ShellProcessControl subShell(@NonNull ShellType type) {
return shell(type.openCommand()).elevation(getElevationPassword()); return subShell(type.openCommand()).elevation(getElevationPassword());
} }
default CommandProcessControl command(@NonNull ShellType type, String command) { default CommandProcessControl command(@NonNull ShellType type, String command) {
return command(type.switchTo(command)); return command(type.switchTo(command));
} }
default ShellProcessControl shell(@NonNull List<String> command) { default ShellProcessControl subShell(@NonNull List<String> command) {
return shell( return subShell(
command.stream().map(s -> s.contains(" ") ? "\"" + s + "\"" : s).collect(Collectors.joining(" "))); command.stream().map(s -> s.contains(" ") ? "\"" + s + "\"" : s).collect(Collectors.joining(" ")));
} }
default ShellProcessControl shell(@NonNull String command) { default ShellProcessControl subShell(@NonNull String command) {
return shell(processControl -> command); return subShell(processControl -> command);
} }
ShellProcessControl shell(@NonNull Function<ShellProcessControl, String> command); ShellProcessControl subShell(@NonNull Function<ShellProcessControl, String> command);
default ShellProcessControl consoleCommand(@NonNull String command) {
return consoleCommand(shellProcessControl -> command);
}
ShellProcessControl consoleCommand(@NonNull Function<ShellProcessControl, String> command);
void executeCommand(String command) throws Exception; void executeCommand(String command) throws Exception;

View file

@ -10,12 +10,20 @@ import java.util.stream.Collectors;
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type") @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type")
public interface ShellType { public interface ShellType {
String getScriptFileEnding();
default String commandWithVariable(String key, String value, String command) {
return joinCommands(getSetVariableCommand(key, value), command);
}
String getPauseCommand();
String createInitFileContent(String command); String createInitFileContent(String command);
List<String> getOpenWithInitFileCommand(String file); String getOpenWithInitFileCommand(String file);
default String flatten(List<String> command) { default String flatten(List<String> command) {
return command.stream().map(s -> s.contains(" ") ? "\"" + s + "\"" : s).collect(Collectors.joining(" ")); return command.stream().map(s -> s.contains(" ") && !(s.startsWith("\"") && s.endsWith("\"")) ? "\"" + s + "\"" : s).collect(Collectors.joining(" "));
} }
@ -41,6 +49,14 @@ public interface ShellType {
return "&&"; return "&&";
} }
default String getOrConcatenationOperator() {
return "||";
}
String getMakeExecutableCommand(String file);
String elevateConsoleCommand(ShellProcessControl control, String command);
String getEchoCommand(String s, boolean toErrorStream); String getEchoCommand(String s, boolean toErrorStream);
String queryShellProcessId(ShellProcessControl control) throws Exception; String queryShellProcessId(ShellProcessControl control) throws Exception;