Skip to content

Commit 2f3240a

Browse files
authored
Run command with common exec (#4570)
1 parent 08c425c commit 2f3240a

File tree

3 files changed

+36
-12
lines changed

3 files changed

+36
-12
lines changed

PluginsAndFeatures/azure-toolkit-for-intellij/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ configurations {
8888
apply plugin: 'java'
8989

9090
dependencies {
91+
compile group: 'org.apache.commons', name: 'commons-exec', version: '1.3'
9192
compile 'com.microsoft.sqlserver:mssql-jdbc:6.4.0.jre8'
9293
compile 'commons-io:commons-io:2.7'
9394
compile group: 'org.apache.commons', name: 'commons-text', version: '1.8'

PluginsAndFeatures/azure-toolkit-for-intellij/src/com/microsoft/intellij/runner/functions/localrun/FunctionRunState.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -160,32 +160,32 @@ private void validateFunctionRuntime(RunProcessHandler processHandler) throws Az
160160
if (funcVersion.compareTo(minimumVersion) < 0) {
161161
throw new AzureExecutionException(FUNCTION_CORE_TOOLS_OUT_OF_DATE);
162162
}
163-
} catch (IOException | InterruptedException e) {
163+
} catch (IOException e) {
164164
throw new AzureExecutionException(String.format(FAILED_TO_VALIDATE_FUNCTION_RUNTIME, e.getMessage()));
165165
}
166166
}
167167

168-
private ComparableVersion getFuncVersion() throws IOException, InterruptedException {
168+
private ComparableVersion getFuncVersion() throws IOException {
169169
final File func = new File(functionRunConfiguration.getFuncPath());
170-
final String[] funcVersionResult = CommandUtils.executeMultipleLineOutput(
171-
String.format("%s -v", func.getName()), func.getParentFile());
172-
if (ArrayUtils.isEmpty(funcVersionResult)) {
170+
final String funcVersion = CommandUtils.executeCommandAndGetOutput(func.getAbsolutePath(), new String[]{"-v"}, func.getParentFile());
171+
if (StringUtils.isEmpty(funcVersion)) {
173172
return null;
174173
}
175-
return new ComparableVersion(funcVersionResult[0].trim());
174+
return new ComparableVersion(funcVersion);
176175
}
177176

178177
// Get java runtime version following the strategy of function core tools
179178
// Get java version of JAVA_HOME first, fall back to use PATH if JAVA_HOME not exists
180-
private ComparableVersion getJavaVersion() throws IOException, InterruptedException {
179+
private ComparableVersion getJavaVersion() throws IOException {
181180
final String javaHome = System.getenv("JAVA_HOME");
182-
final File executeFolder = StringUtils.isEmpty(javaHome) ? null : Paths.get(javaHome, "bin").toFile();
183-
final String[] javaVersionResult = CommandUtils.executeMultipleLineOutput(
184-
"java -version", executeFolder, Process::getErrorStream); // java -version will write to std error
185-
if (ArrayUtils.isEmpty(javaVersionResult)) {
181+
final File javaFile = StringUtils.isEmpty(javaHome) ? null : Paths.get(javaHome, "bin", "java").toFile();
182+
final File executeFolder = javaFile == null ? null : javaFile.getParentFile();
183+
final String command = javaFile == null ? "java" : javaFile.getAbsolutePath();
184+
final String javaVersion = CommandUtils.executeCommandAndGetOutput(command, new String[]{"-version"}, executeFolder);
185+
if (StringUtils.isEmpty(javaVersion)) {
186186
return null;
187187
}
188-
final Matcher matcher = JAVA_VERSION_PATTERN.matcher(javaVersionResult[0].trim());
188+
final Matcher matcher = JAVA_VERSION_PATTERN.matcher(javaVersion);
189189
return matcher.find() ? new ComparableVersion(matcher.group(1)) : null;
190190
}
191191

PluginsAndFeatures/azure-toolkit-for-intellij/src/com/microsoft/intellij/util/CommandUtils.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,15 @@
2222

2323
package com.microsoft.intellij.util;
2424

25+
import org.apache.commons.exec.CommandLine;
26+
import org.apache.commons.exec.DefaultExecutor;
27+
import org.apache.commons.exec.ExecuteException;
28+
import org.apache.commons.exec.PumpStreamHandler;
2529
import org.apache.commons.io.IOUtils;
2630
import org.apache.commons.lang3.StringUtils;
2731
import org.apache.commons.lang3.SystemUtils;
2832

33+
import java.io.ByteArrayOutputStream;
2934
import java.io.File;
3035
import java.io.IOException;
3136
import java.io.InputStream;
@@ -54,6 +59,24 @@ public static String[] executeMultipleLineOutput(final String cmd, File cwd, Fun
5459
return StringUtils.split(IOUtils.toString(streamFunction.apply(p), "utf8"), "\n");
5560
}
5661

62+
public static String executeCommandAndGetOutput(final String command, final String[] parameters, final File directory) throws IOException {
63+
final CommandLine commandLine = new CommandLine(command);
64+
commandLine.addArguments(parameters);
65+
final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
66+
final PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream);
67+
final DefaultExecutor executor = new DefaultExecutor();
68+
executor.setWorkingDirectory(directory);
69+
executor.setStreamHandler(streamHandler);
70+
executor.setExitValues(null);
71+
try {
72+
executor.execute(commandLine);
73+
return outputStream.toString();
74+
} catch (ExecuteException e) {
75+
// swallow execute exception and return empty
76+
return StringUtils.EMPTY;
77+
}
78+
}
79+
5780
public static String[] executeMultipleLineOutput(final String cmd, File cwd)
5881
throws IOException, InterruptedException {
5982
return executeMultipleLineOutput(cmd, cwd, Process::getInputStream);

0 commit comments

Comments
 (0)