From 9fbac93a6f9261e20ada5100e124673be061cf2f Mon Sep 17 00:00:00 2001 From: godotg Date: Tue, 7 May 2024 11:18:43 +0800 Subject: [PATCH] ref[exec]: extract execCommand method --- .../java/com/zfoo/monitor/util/OSUtils.java | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/monitor/src/main/java/com/zfoo/monitor/util/OSUtils.java b/monitor/src/main/java/com/zfoo/monitor/util/OSUtils.java index 06f62ad3..1fbb9f09 100644 --- a/monitor/src/main/java/com/zfoo/monitor/util/OSUtils.java +++ b/monitor/src/main/java/com/zfoo/monitor/util/OSUtils.java @@ -259,21 +259,21 @@ public abstract class OSUtils { // ----------------------------------------------------------------------------------------------------------------- public static String execCommand(String command) { - return execCommand(command, null); + logger.info("execCommand [{}]", command); + return doExecCommand(command, null); } public static String execCommand(String command, String workingDirectory) { + logger.info("execCommand [{}] workingDirectory:[{}]", command, workingDirectory); + FileUtils.createDirectory(workingDirectory); + var wd = new File(workingDirectory); + return doExecCommand(command, wd); + } + + public static String doExecCommand(String command, File wd) { Process process = null; InputStream inputStream = null; - File wd = null; - if (StringUtils.isNotBlank(workingDirectory)) { - FileUtils.createDirectory(workingDirectory); - wd = new File(workingDirectory); - logger.info("execCommand:[{}] workingDirectory:[{}]", command, workingDirectory); - } else { - logger.info("execCommand:[{}]", command); - } try { process = new ProcessBuilder(command.split(" ")) .redirectErrorStream(true) @@ -307,5 +307,4 @@ public abstract class OSUtils { return StringUtils.EMPTY; } - }