Small shell fixes and cleanup

This commit is contained in:
Christopher Schnick 2022-12-11 03:45:51 +01:00
parent f45123470b
commit ec62a12a20
7 changed files with 27 additions and 53 deletions

View file

@ -58,7 +58,6 @@ public interface CommandProcessControl extends ProcessControl {
@Override
CommandProcessControl start() throws Exception;
@Override
CommandProcessControl exitTimeout(Integer timeout);
String readOnlyStdout() throws Exception;

View file

@ -24,8 +24,6 @@ public interface ProcessControl extends Closeable, AutoCloseable {
void close() throws IOException;
void kill() throws Exception;
ProcessControl exitTimeout(Integer timeout);
ProcessControl start() throws Exception;
InputStream getStdout();

View file

@ -35,12 +35,6 @@ public interface ShellProcessControl extends ProcessControl {
}
}
default void executeSimpleCommand(List<String> command) throws Exception {
try (CommandProcessControl c = command(command).start()) {
c.discardOrThrow();
}
}
default String executeStringSimpleCommand(ShellType type, String command) throws Exception {
try (var sub = subShell(type).start()) {
return sub.executeStringSimpleCommand(command);

View file

@ -355,19 +355,23 @@ public class ShellTypes {
@Override
public String createWhichCommand(String executable) {
return "cmd /C where \"" + executable + "\"";
return "$LASTEXITCODE=(1 - (Get-Command -erroraction \"silentlycontinue\" \"" + executable + "\").Length)";
}
@Override
public Charset determineCharset(ShellProcessControl control) throws Exception {
control.writeLine("chcp");
var r = new BufferedReader(new InputStreamReader(control.getStdout(), StandardCharsets.US_ASCII));
control.writeLine("If (Get-Command -erroraction 'silentlycontinue' chcp) {chcp} Else {echo \"Not Windows\"}");
// Read echo of command
r.readLine();
// Read actual output
var line = r.readLine();
if (line.equals("Not Windows")) {
return StandardCharsets.UTF_8;
}
var matcher = Pattern.compile("\\d+").matcher(line);
matcher.find();
return Charset.forName("ibm" + matcher.group());

View file

@ -5,6 +5,7 @@ import lombok.SneakyThrows;
public abstract class SimpleProxyFunction<T> extends ProxyFunction {
@SneakyThrows
@SuppressWarnings("unchecked")
public T getResult() {
var fields = getClass().getDeclaredFields();
var last = fields[fields.length - 1];
@ -13,6 +14,7 @@ public abstract class SimpleProxyFunction<T> extends ProxyFunction {
}
@SneakyThrows
@SuppressWarnings("unchecked")
public T callAndGet() {
var result = callAndCopy();
return ((SimpleProxyFunction<T>) result).getResult();

View file

@ -0,0 +1,18 @@
package io.xpipe.extension.util;
import io.xpipe.core.process.ShellProcessControl;
import java.io.IOException;
public class ApplicationHelper {
public static boolean isInPath(ShellProcessControl processControl, String executable) throws Exception {
return processControl.executeBooleanSimpleCommand(processControl.getShellType().createWhichCommand(executable));
}
public static void checkSupport(ShellProcessControl processControl, String executable, String displayName) throws Exception {
if (!isInPath(processControl, executable)) {
throw new IOException(displayName + " executable " + executable + " not found in PATH");
}
}
}

View file

@ -1,41 +0,0 @@
package io.xpipe.extension.util;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.List;
import java.util.stream.Collectors;
public class ExpectHelper {
private static Path extractedExecutable = null;
private static Path getExtractedExecutable() {
if (extractedExecutable == null) {
XPipeDaemon.getInstance().withResource("io.xpipe.extension", "bin/expect.exe", path -> {
extractedExecutable = Files.createTempFile(null, ".exe");
Files.copy(path, extractedExecutable, StandardCopyOption.REPLACE_EXISTING);
});
}
return extractedExecutable;
}
public static List<String> executeExpect(List<String> command, String password) throws IOException {
var file = Files.createTempFile(null, ".lua");
Files.writeString(file, expectFile(command, password));
return List.of(getExtractedExecutable().toString(), file.toString());
}
private static String expectFile(List<String> command, String password) {
return String.format("""
echo(false)
if spawn(%s) then
expect(":")
sendln("%s")
echo(true)
end
""", command.stream().map(s -> "\"" + s + "\"").collect(Collectors.joining(", ")), password);
}
}