mirror of
https://github.com/xpipe-io/xpipe.git
synced 2024-11-21 23:20:23 +00:00
Small shell fixes and cleanup
This commit is contained in:
parent
87d0830f3c
commit
e9340c40a8
6 changed files with 20 additions and 50 deletions
|
@ -58,7 +58,6 @@ public interface CommandProcessControl extends ProcessControl {
|
|||
@Override
|
||||
CommandProcessControl start() throws Exception;
|
||||
|
||||
@Override
|
||||
CommandProcessControl exitTimeout(Integer timeout);
|
||||
|
||||
String readOnlyStdout() throws Exception;
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue