Which command fixes

This commit is contained in:
crschnick 2024-09-26 13:15:56 +00:00
parent ee4fb93290
commit 26e06dc78c
4 changed files with 13 additions and 11 deletions

View file

@ -75,7 +75,7 @@ public abstract class ExternalApplicationType implements PrefsChoiceValue {
public boolean isAvailable() { public boolean isAvailable() {
try (ShellControl pc = LocalShell.getShell()) { try (ShellControl pc = LocalShell.getShell()) {
return pc.executeSimpleBooleanCommand(pc.getShellDialect().getWhichCommand(executable)); return CommandSupport.findProgram(pc, executable).isPresent();
} catch (Exception e) { } catch (Exception e) {
ErrorEvent.fromThrowable(e).omit().handle(); ErrorEvent.fromThrowable(e).omit().handle();
return false; return false;
@ -115,14 +115,9 @@ public abstract class ExternalApplicationType implements PrefsChoiceValue {
protected Optional<Path> determineFromPath() { protected Optional<Path> determineFromPath() {
// Try to locate if it is in the Path // Try to locate if it is in the Path
try (var sc = LocalShell.getShell().start()) { try (var sc = LocalShell.getShell().start()) {
var out = sc.command(CommandBuilder.ofFunction( var out = CommandSupport.findProgram(sc, executable);
var1 -> var1.getShellDialect().getWhichCommand(executable)))
.readStdoutIfPossible();
if (out.isPresent()) { if (out.isPresent()) {
var first = out.get().lines().findFirst(); return out.map(Path::of);
if (first.isPresent()) {
return first.map(String::trim).map(Path::of);
}
} }
} catch (Exception ex) { } catch (Exception ex) {
ErrorEvent.fromThrowable(ex).omit().handle(); ErrorEvent.fromThrowable(ex).omit().handle();

View file

@ -96,7 +96,7 @@ public interface KittyTerminalType extends ExternalTerminalType {
public boolean isAvailable() { public boolean isAvailable() {
try (ShellControl pc = LocalShell.getShell()) { try (ShellControl pc = LocalShell.getShell()) {
return pc.executeSimpleBooleanCommand(pc.getShellDialect().getWhichCommand("kitty")); return CommandSupport.findProgram(pc, "kitty").isPresent();
} catch (Exception e) { } catch (Exception e) {
ErrorEvent.fromThrowable(e).omit().handle(); ErrorEvent.fromThrowable(e).omit().handle();
return false; return false;

View file

@ -6,8 +6,15 @@ import io.xpipe.core.process.ShellControl;
import io.xpipe.core.util.FailableSupplier; import io.xpipe.core.util.FailableSupplier;
import java.io.IOException; import java.io.IOException;
import java.util.Optional;
public class CommandSupport { public class CommandSupport {
public static Optional<String> 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 { public static boolean isInPath(ShellControl processControl, String executable) throws Exception {
return processControl.executeSimpleBooleanCommand( return processControl.executeSimpleBooleanCommand(
processControl.getShellDialect().getWhichCommand(executable)); processControl.getShellDialect().getWhichCommand(executable));

View file

@ -178,12 +178,12 @@ public class SshLocalBridge {
.resolve("sshd") .resolve("sshd")
.toString(); .toString();
} else { } else {
var exec = sc.command(sc.getShellDialect().getWhichCommand("sshd")).readStdoutIfPossible(); var exec = CommandSupport.findProgram(sc, "sshd");
if (exec.isEmpty()) { if (exec.isEmpty()) {
throw ErrorEvent.expected(new IllegalStateException( throw ErrorEvent.expected(new IllegalStateException(
"No sshd executable found in PATH. The SSH terminal bridge requires a local ssh server")); "No sshd executable found in PATH. The SSH terminal bridge requires a local ssh server"));
} }
return exec.get().lines().findFirst().orElseThrow(); return exec.get();
} }
} }