From 26e06dc78cbb7c32ba4680a19ce5372bd5a192fd Mon Sep 17 00:00:00 2001 From: crschnick Date: Thu, 26 Sep 2024 13:15:56 +0000 Subject: [PATCH] Which command fixes --- .../io/xpipe/app/prefs/ExternalApplicationType.java | 11 +++-------- .../java/io/xpipe/app/terminal/KittyTerminalType.java | 2 +- .../main/java/io/xpipe/app/util/CommandSupport.java | 7 +++++++ .../main/java/io/xpipe/app/util/SshLocalBridge.java | 4 ++-- 4 files changed, 13 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/io/xpipe/app/prefs/ExternalApplicationType.java b/app/src/main/java/io/xpipe/app/prefs/ExternalApplicationType.java index 196fc64f1..f9c6096fc 100644 --- a/app/src/main/java/io/xpipe/app/prefs/ExternalApplicationType.java +++ b/app/src/main/java/io/xpipe/app/prefs/ExternalApplicationType.java @@ -75,7 +75,7 @@ public abstract class ExternalApplicationType implements PrefsChoiceValue { public boolean isAvailable() { try (ShellControl pc = LocalShell.getShell()) { - return pc.executeSimpleBooleanCommand(pc.getShellDialect().getWhichCommand(executable)); + return CommandSupport.findProgram(pc, executable).isPresent(); } catch (Exception e) { ErrorEvent.fromThrowable(e).omit().handle(); return false; @@ -115,14 +115,9 @@ public abstract class ExternalApplicationType implements PrefsChoiceValue { protected Optional determineFromPath() { // Try to locate if it is in the Path try (var sc = LocalShell.getShell().start()) { - var out = sc.command(CommandBuilder.ofFunction( - var1 -> var1.getShellDialect().getWhichCommand(executable))) - .readStdoutIfPossible(); + var out = CommandSupport.findProgram(sc, executable); if (out.isPresent()) { - var first = out.get().lines().findFirst(); - if (first.isPresent()) { - return first.map(String::trim).map(Path::of); - } + return out.map(Path::of); } } catch (Exception ex) { ErrorEvent.fromThrowable(ex).omit().handle(); diff --git a/app/src/main/java/io/xpipe/app/terminal/KittyTerminalType.java b/app/src/main/java/io/xpipe/app/terminal/KittyTerminalType.java index 42e8a93af..0ca65961a 100644 --- a/app/src/main/java/io/xpipe/app/terminal/KittyTerminalType.java +++ b/app/src/main/java/io/xpipe/app/terminal/KittyTerminalType.java @@ -96,7 +96,7 @@ public interface KittyTerminalType extends ExternalTerminalType { public boolean isAvailable() { try (ShellControl pc = LocalShell.getShell()) { - return pc.executeSimpleBooleanCommand(pc.getShellDialect().getWhichCommand("kitty")); + return CommandSupport.findProgram(pc, "kitty").isPresent(); } catch (Exception e) { ErrorEvent.fromThrowable(e).omit().handle(); return false; diff --git a/app/src/main/java/io/xpipe/app/util/CommandSupport.java b/app/src/main/java/io/xpipe/app/util/CommandSupport.java index b1b04b2d6..b59eec5e8 100644 --- a/app/src/main/java/io/xpipe/app/util/CommandSupport.java +++ b/app/src/main/java/io/xpipe/app/util/CommandSupport.java @@ -6,8 +6,15 @@ import io.xpipe.core.process.ShellControl; import io.xpipe.core.util.FailableSupplier; import java.io.IOException; +import java.util.Optional; public class CommandSupport { + + public static Optional findProgram(ShellControl processControl, String name) throws Exception { + var out = processControl.command(processControl.getShellDialect().getWhichCommand(name)).readStdoutIfPossible(); + return out.flatMap(s -> s.lines().findFirst()).map(String::trim); + } + public static boolean isInPath(ShellControl processControl, String executable) throws Exception { return processControl.executeSimpleBooleanCommand( processControl.getShellDialect().getWhichCommand(executable)); diff --git a/app/src/main/java/io/xpipe/app/util/SshLocalBridge.java b/app/src/main/java/io/xpipe/app/util/SshLocalBridge.java index 6a5fa6e03..cb078e231 100644 --- a/app/src/main/java/io/xpipe/app/util/SshLocalBridge.java +++ b/app/src/main/java/io/xpipe/app/util/SshLocalBridge.java @@ -178,12 +178,12 @@ public class SshLocalBridge { .resolve("sshd") .toString(); } else { - var exec = sc.command(sc.getShellDialect().getWhichCommand("sshd")).readStdoutIfPossible(); + var exec = CommandSupport.findProgram(sc, "sshd"); if (exec.isEmpty()) { throw ErrorEvent.expected(new IllegalStateException( "No sshd executable found in PATH. The SSH terminal bridge requires a local ssh server")); } - return exec.get().lines().findFirst().orElseThrow(); + return exec.get(); } }