Skip to content

Commit f7b57d3

Browse files
committed
add shell cmd util
1 parent daa5a21 commit f7b57d3

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright 2017 flow.ci
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.flow.platform.plugin.util;
18+
19+
import com.flow.platform.plugin.exception.PluginException;
20+
import com.flow.platform.util.Logger;
21+
import java.io.BufferedReader;
22+
import java.io.InputStreamReader;
23+
24+
/**
25+
* @author yh@firim
26+
*/
27+
public class ShellUtil {
28+
29+
private final static Logger LOGGER = new Logger(ShellUtil.class);
30+
31+
public static void exeCmd(String shell) {
32+
BufferedReader br;
33+
try {
34+
Process process;
35+
ProcessBuilder pb = new ProcessBuilder("/bin/bash", "-c", shell);
36+
pb.environment();
37+
pb.redirectErrorStream(true); // merge error stream into standard stream
38+
process = pb.start();
39+
if (process != null) {
40+
br = new BufferedReader(
41+
new InputStreamReader(process.getInputStream()), 1024);
42+
process.waitFor();
43+
} else {
44+
throw new PluginException("Plugin running process is not start");
45+
}
46+
String line;
47+
while (br != null && (line = br.readLine()) != null) {
48+
LOGGER.debug(line);
49+
}
50+
} catch (Exception e) {
51+
LOGGER.error("Exec cmd error", e);
52+
} finally {
53+
}
54+
}
55+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.flow.platform.plugin.test.util;
2+
3+
import com.flow.platform.plugin.test.TestBase;
4+
import com.flow.platform.plugin.util.ShellUtil;
5+
import org.junit.Test;
6+
7+
/**
8+
* @author yh@firim
9+
*/
10+
public class ShellUtilTest extends TestBase{
11+
12+
@Test
13+
public void should_exec_cmd_success() {
14+
ShellUtil.exeCmd("echo helloworld");
15+
ShellUtil.exeCmd("pwd");
16+
ShellUtil.exeCmd("mvn");
17+
}
18+
}

0 commit comments

Comments
 (0)