-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
74 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,5 @@ | |
.gradle | ||
build | ||
|
||
# Project files | ||
CommandTest.java |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
src/main/java/io/eroshenkoam/xcresults/util/ProcessUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package io.eroshenkoam.xcresults.util; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import org.apache.commons.io.IOUtils; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.Objects; | ||
|
||
public final class ProcessUtil { | ||
|
||
private ProcessUtil() { | ||
} | ||
|
||
public static String readProcessOutputAsString(final ProcessBuilder builder) { | ||
return readProcessOutput(builder, input -> IOUtils.toString(input, StandardCharsets.UTF_8)); | ||
} | ||
|
||
public static JsonNode readProcessOutputAsJson(final ProcessBuilder builder, | ||
final ObjectMapper mapper) { | ||
return readProcessOutput(builder, mapper::readTree); | ||
} | ||
|
||
public static <T> T readProcessOutput(final ProcessBuilder builder, | ||
final ThrowableFunction<InputStream, T> reader) { | ||
try { | ||
final Process process = builder.start(); | ||
try (InputStream input = process.getInputStream()) { | ||
if (Objects.nonNull(input)) { | ||
return reader.apply(input); | ||
} else { | ||
return null; | ||
} | ||
} | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
@FunctionalInterface | ||
public interface ThrowableFunction<T, R> { | ||
R apply(T t) throws IOException; | ||
} | ||
|
||
} |