Skip to content

Commit

Permalink
refactor: refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
iseki0 committed Mar 24, 2024
1 parent 4d205dd commit 8667924
Showing 1 changed file with 43 additions and 41 deletions.
84 changes: 43 additions & 41 deletions src/main/java/space/iseki/cmdpipe/Cmd.java
Original file line number Diff line number Diff line change
Expand Up @@ -241,37 +241,6 @@ class Builder {
private ProcessBuilder[] pbs;
private boolean autoGrantExecutablePerm = false;

static void closeIgnoreIOException(Closeable closeable) {
try {
closeable.close();
} catch (IOException ignored) {
}
}

static void killTree(Process process, boolean force) {
try {
process.descendants().forEach(p -> killTree(p, force));
} catch (UnsupportedOperationException ignored) {
}
if (force) {
process.destroyForcibly();
} else {
process.destroy();
}
}

static void killTree(ProcessHandle ph, boolean force) {
ph.descendants().forEach(p -> killTree(p, force));
kill(ph, force);
}

static void kill(ProcessHandle ph, boolean force) {
if (force) {
ph.destroyForcibly();
} else {
ph.destroy();
}
}

static boolean isPermissionProblem(IOException e) {
return OSNameUtils.IS_UNIX_LIKE && Optional.ofNullable(e.getMessage()).map(s -> s.contains("error=13")).orElse(false);
Expand Down Expand Up @@ -589,6 +558,49 @@ class CmdImpl implements Cmd {
this.executor = executor;
}

private static void closeIgnoreIOException(Closeable closeable) {
try {
closeable.close();
} catch (IOException ignored) {
}
}

private static void killTree(Process process, boolean force) {
try {
process.descendants().forEach(p -> killTree(p, force));
} catch (UnsupportedOperationException ignored) {
}
if (force) {
process.destroyForcibly();
} else {
process.destroy();
}
}

private static void killTree(ProcessHandle ph, boolean force) {
ph.descendants().forEach(p -> killTree(p, force));
kill(ph, force);
}

private static void kill(ProcessHandle ph, boolean force) {
if (force) {
ph.destroyForcibly();
} else {
ph.destroy();
}
}

private void killProcesses(boolean force) {
getProcesses().forEach(p -> killTree(p, force));
}

private void closeAllIO() {
var ps = getProcesses();
Optional.ofNullable(ps.get(0).getInputStream()).ifPresent(CmdImpl::closeIgnoreIOException);
Optional.ofNullable(ps.get(ps.size() - 1).getOutputStream()).ifPresent(CmdImpl::closeIgnoreIOException);
Optional.ofNullable(ps.get(ps.size() - 1).getErrorStream()).ifPresent(CmdImpl::closeIgnoreIOException);
}

@Override
public @NotNull List<@NotNull Process> getProcesses() {
return processes;
Expand All @@ -605,16 +617,6 @@ public void stopAll(boolean force) {
if (force) closeAllIO();
}

private void killProcesses(boolean force) {
getProcesses().forEach(p -> Builder.killTree(p, force));
}

private void closeAllIO() {
var ps = getProcesses();
Optional.ofNullable(ps.get(0).getInputStream()).ifPresent(Builder::closeIgnoreIOException);
Optional.ofNullable(ps.get(ps.size() - 1).getOutputStream()).ifPresent(Builder::closeIgnoreIOException);
Optional.ofNullable(ps.get(ps.size() - 1).getErrorStream()).ifPresent(Builder::closeIgnoreIOException);
}

@Override
public int waitFor() throws InterruptedException {
Expand Down

0 comments on commit 8667924

Please sign in to comment.