-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
new test class ShellExecutor created
- Loading branch information
1 parent
96a2312
commit 65ced32
Showing
3 changed files
with
63 additions
and
1 deletion.
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 |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package io.github.astrapi69.net; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
|
||
public class ShellExecutor | ||
{ | ||
|
||
public static String execute(String shellPath, String executionPath, String command) | ||
throws IOException, InterruptedException | ||
{ | ||
if(executionPath.contains("~")){ | ||
executionPath = executionPath.replace("~", System.getProperty("user.home")); | ||
} | ||
File executionDirectory = new File(executionPath); | ||
if (!executionDirectory.exists()) | ||
{ | ||
throw new IllegalArgumentException("Execution directory does not exist"); | ||
} | ||
String cdToExecutionPath = "cd " + executionPath; | ||
String commands = cdToExecutionPath + " && " + command; | ||
ProcessBuilder processBuilder = new ProcessBuilder(); | ||
processBuilder.command(shellPath, "-c", commands); | ||
|
||
Process process = processBuilder.start(); | ||
StringBuilder stringBuilder = new StringBuilder(); | ||
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); | ||
|
||
String line; | ||
while ((line = reader.readLine()) != null) | ||
{ | ||
stringBuilder.append(line + "\n"); | ||
} | ||
int waitFor = process.waitFor(); | ||
|
||
if (waitFor != 0) | ||
stringBuilder.append("Exit code: ").append(waitFor).append("\n"); | ||
return stringBuilder.toString(); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
src/test/java/io/github/astrapi69/net/ShellExecutorTest.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,20 @@ | ||
package io.github.astrapi69.net; | ||
|
||
import org.testng.annotations.Test; | ||
|
||
import java.io.IOException; | ||
|
||
public class ShellExecutorTest { | ||
|
||
@Test | ||
public void testExecute() throws IOException, InterruptedException { | ||
|
||
String shellPath = "/usr/bin/zsh"; | ||
String executionPath = "~/dev/tmp"; | ||
String command = "touch foo.txt"; | ||
|
||
String shellOutput = ShellExecutor.execute(shellPath, executionPath, command); | ||
System.out.println(shellOutput); | ||
|
||
} | ||
} |
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