Skip to content

Commit 9d5fe73

Browse files
Refactoring: original zt-exec configuring through a separate method
1 parent efe5fd6 commit 9d5fe73

File tree

8 files changed

+134
-113
lines changed

8 files changed

+134
-113
lines changed

README.md

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,17 @@ This library solves this small problem and intended to call each command inside
4040
### Examples
4141
> Execute command
4242
````java
43-
new Cmd().executing().execute("echo", "Hello");
43+
new Cmd().command("echo", "Hello").execute();
4444
````
4545
> Execute script in a Shell
4646
````java
47-
new Cmd().executing().executeInShell("s='Hello'; echo $s;");
47+
new Cmd().script("s='Hello'; echo $s;").execute();
4848
````
4949
> Execute script and read output
5050
````java
51-
String output = new Cmd(e -> e.readOutput(true))
52-
.executing().executeInShell("s='Hello'; echo $s;")
51+
String output = new Cmd()
52+
.configuring(e -> e.readOutput(true))
53+
.script("s='Hello'; echo $s;").execute()
5354
.outputUTF8();
5455
System.out.println(output);
5556

@@ -59,23 +60,23 @@ System.out.println(output);
5960
```java
6061
new Cmd()
6162
.outputFileName("output.txt")
62-
.executing().execute("echo", "Hello");
63+
.command("echo", "Hello").execute();
6364
````
6465
> Create work directory before start and delete after finish
6566
````java
6667
new Cmd()
68+
.configuring(e -> e.directory(new File("./", "foo"))) // specify work directory ./foo
69+
.cleanUp(true)
6770
.listening()
68-
.beforeStart(e -> e.directory(new File("./", "foo"))) // specify work directory ./foo
6971
.afterStop(process -> {
7072
//work directory ./foo will be exists here
7173
})
7274
.back()
73-
.cleanUp(true)
74-
.executing().execute("echo", "hello world"); // work directory ./foo will be created automatically
75+
.command("echo", "hello world").execute(); // work directory ./foo will be created automatically
7576
//work directory ./foo was deleted after execution
7677
````
7778
> Run in a background
7879
````java
79-
StartedProcess startedProcess = new Cmd().executing().start("echo", "Hello");
80+
StartedProcess startedProcess = new Cmd().command("echo", "Hello").start();
8081
startedProcess.getFuture().get(); //wait result
8182
````
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.enot.cmd.core;
2+
3+
import org.zeroturnaround.exec.ProcessExecutor;
4+
import org.zeroturnaround.exec.ProcessResult;
5+
import org.zeroturnaround.exec.StartedProcess;
6+
7+
import java.io.IOException;
8+
import java.util.concurrent.TimeoutException;
9+
10+
final class BaseCommand implements Command {
11+
private final ProcessExecutor processExecutor;
12+
13+
BaseCommand(ProcessExecutor processExecutor) {
14+
this.processExecutor = processExecutor;
15+
}
16+
17+
/**
18+
* See {@link ProcessExecutor#execute()}
19+
*/
20+
@Override
21+
public ProcessResult execute() throws IOException, TimeoutException, InterruptedException {
22+
return processExecutor.execute();
23+
}
24+
25+
/**
26+
* See {@link ProcessExecutor#executeNoTimeout()}
27+
*/
28+
@Override
29+
public ProcessResult executeNoTimeout() throws IOException, InterruptedException {
30+
return processExecutor.executeNoTimeout();
31+
}
32+
33+
/**
34+
* See {@link ProcessExecutor#start()}
35+
*/
36+
@Override
37+
public StartedProcess start() throws IOException {
38+
return processExecutor.start();
39+
}
40+
}

src/main/java/com/enot/cmd/core/BaseExecuting.java

Lines changed: 0 additions & 50 deletions
This file was deleted.

src/main/java/com/enot/cmd/core/Cmd.java

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,20 @@ public final class Cmd implements ICmd {
2323
private final boolean cleanUp;
2424
private final String outputFileName;
2525
private final Listening listening;
26+
private final LambdaListenerAdapter.BeforeStart configuring;
2627

2728
public Cmd() {
28-
this(false, "", new Listening(null, new ArrayAsIterable<>()));
29+
this(false,
30+
"",
31+
new Listening(null, new ArrayAsIterable<>()),
32+
e -> {});
2933
}
3034

31-
public Cmd(LambdaListenerAdapter.BeforeStart configuring) {
32-
this(false, "", new Listening(null, new ArrayAsIterable<>(new LambdaListenerAdapter(configuring))));
33-
}
34-
35-
public Cmd(boolean cleanUp, String outputFileName, Listening listening) {
35+
public Cmd(boolean cleanUp, String outputFileName, Listening listening, LambdaListenerAdapter.BeforeStart configuring) {
3636
this.cleanUp = cleanUp;
3737
this.outputFileName = outputFileName;
3838
this.listening = listening;
39+
this.configuring = configuring;
3940
}
4041

4142
/**
@@ -46,12 +47,17 @@ public Cmd(boolean cleanUp, String outputFileName, Listening listening) {
4647
*/
4748
@Override
4849
public Cmd cleanUp(boolean cleanUp) {
49-
return new Cmd(cleanUp, outputFileName, listening);
50+
return new Cmd(cleanUp, outputFileName, listening, configuring);
5051
}
5152

5253
@Override
5354
public Cmd outputFileName(String outputFileName) {
54-
return new Cmd(cleanUp, outputFileName, listening);
55+
return new Cmd(cleanUp, outputFileName, listening, configuring);
56+
}
57+
58+
@Override
59+
public Cmd configuring(LambdaListenerAdapter.BeforeStart configuring) {
60+
return new Cmd(cleanUp, outputFileName, listening, configuring);
5561
}
5662

5763
@Override
@@ -60,16 +66,21 @@ public CmdListening listening() {
6066
}
6167

6268
@Override
63-
public Executing executing() {
64-
return new BaseExecuting(processExecutor());
69+
public Command script(String script) {
70+
return command("sh", "-c", script);
71+
}
72+
73+
public Command command(String... command) {
74+
return new BaseCommand(processExecutor(command));
6575
}
6676

67-
private ProcessExecutor processExecutor() {
77+
private ProcessExecutor processExecutor(String... command) {
6878
final ProcessExecutor executor = new ProcessExecutor();
79+
configuring.run(executor);
6980
for (LambdaListenerAdapter listener : listening.listeners) {
7081
executor.addListener(listener);
7182
}
72-
return executor.addListener(baseListener(cleanUp, outputFileName));
83+
return executor.command(command).addListener(baseListener(cleanUp, outputFileName));
7384
}
7485

7586
private ProcessListener baseListener(boolean cleanUp, String outputFileName) {
@@ -180,7 +191,7 @@ public CmdListening afterStop(LambdaListenerAdapter.AfterStop... lambdas) {
180191

181192
@Override
182193
public Cmd back() {
183-
return new Cmd(owner.cleanUp, owner.outputFileName, this);
194+
return new Cmd(owner.cleanUp, owner.outputFileName, this, owner.configuring);
184195
}
185196
}
186197
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.enot.cmd.core;
2+
3+
import org.zeroturnaround.exec.ProcessResult;
4+
import org.zeroturnaround.exec.StartedProcess;
5+
6+
import java.io.IOException;
7+
import java.util.concurrent.TimeoutException;
8+
9+
public interface Command {
10+
ProcessResult execute() throws IOException, TimeoutException, InterruptedException;
11+
12+
ProcessResult executeNoTimeout() throws IOException, InterruptedException;
13+
14+
StartedProcess start() throws IOException;
15+
}

src/main/java/com/enot/cmd/core/Executing.java

Lines changed: 0 additions & 17 deletions
This file was deleted.

src/main/java/com/enot/cmd/core/ICmd.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,21 @@ public interface ICmd {
55

66
Cmd outputFileName(String outputFileName);
77

8+
Cmd configuring(LambdaListenerAdapter.BeforeStart configuring);
9+
810
CmdListening listening();
911

10-
Executing executing();
12+
/**
13+
* Create executable shell script
14+
* <p>
15+
* Note: Windows OS doesn't supported, use {@link #command(String...)}} instead
16+
*/
17+
Command script(String script);
18+
19+
/**
20+
* Create executable command
21+
* @param command
22+
* @return
23+
*/
24+
Command command(String... command);
1125
}

src/test/java/com/enot/cmd/CmdTest.java

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,27 +19,30 @@ public class CmdTest {
1919
@Test
2020
public void execute() throws Exception {
2121
Assert.assertEquals("Hello\n",
22-
new Cmd(e -> e.readOutput(true))
23-
.executing()
24-
.execute("echo", "Hello")
22+
new Cmd()
23+
.configuring(e -> e.readOutput(true))
24+
.command("echo", "Hello")
25+
.execute()
2526
.outputUTF8());
2627
}
2728

2829
@Test
2930
public void executeNoTimeout() throws Exception {
3031
Assert.assertEquals("Hello\n",
31-
new Cmd(e -> e.readOutput(true))
32-
.executing()
33-
.executeNoTimeout("echo", "Hello")
32+
new Cmd()
33+
.configuring(e -> e.readOutput(true))
34+
.command("echo", "Hello")
35+
.executeNoTimeout()
3436
.outputUTF8());
3537
}
3638

3739
@Test
3840
public void start() throws Exception {
3941
Assert.assertEquals("Hello\n",
40-
new Cmd(e -> e.readOutput(true))
41-
.executing()
42-
.start("echo", "Hello")
42+
new Cmd()
43+
.configuring(e -> e.readOutput(true))
44+
.command("echo", "Hello")
45+
.start()
4346
.getFuture()
4447
.get()
4548
.outputUTF8());
@@ -48,9 +51,10 @@ public void start() throws Exception {
4851
@Test
4952
public void executeScript() throws Exception {
5053
Assert.assertEquals("Hello\n",
51-
new Cmd(e -> e.readOutput(true))
52-
.executing()
53-
.executeInShell("s='Hello'; echo $s;")
54+
new Cmd()
55+
.configuring(e -> e.readOutput(true))
56+
.script("s='Hello'; echo $s;")
57+
.execute()
5458
.outputUTF8());
5559
}
5660

@@ -60,9 +64,10 @@ public void createWorkDir() throws Exception {
6064

6165
assertFalse(workDir.exists());
6266

63-
new Cmd(e -> e.directory(workDir))
64-
.executing()
65-
.execute("echo", "hello world");
67+
new Cmd()
68+
.configuring(e -> e.directory(workDir))
69+
.command("echo", "hello world")
70+
.execute();
6671

6772
assertTrue(workDir.exists());
6873
}
@@ -73,10 +78,11 @@ public void cleanUp() throws Exception {
7378

7479
assertFalse(workDir.exists());
7580

76-
new Cmd(e -> e.directory(workDir))
81+
new Cmd()
82+
.configuring(e -> e.directory(workDir))
7783
.cleanUp(true)
78-
.executing()
79-
.execute("echo", "hello world");
84+
.command("echo", "hello world")
85+
.execute();
8086

8187
assertFalse("Directory should be deleted", workDir.exists());
8288
}
@@ -85,10 +91,11 @@ public void cleanUp() throws Exception {
8591
public void outputFile() throws Exception {
8692
Path execDir = generateRandomPath();
8793
String outputFileName = "test.output";
88-
new Cmd(e -> e.directory(execDir.toFile()))
94+
new Cmd()
95+
.configuring(e -> e.directory(execDir.toFile()))
8996
.outputFileName(outputFileName)
90-
.executing()
91-
.execute("echo", "hello world");
97+
.command("echo", "hello world")
98+
.execute();
9299
assertTrue("Output file should be exists", Paths.get(execDir.toString(), outputFileName).toFile().exists());
93100
}
94101

@@ -105,8 +112,8 @@ protected void processLine(String line) {
105112
}
106113
}))
107114
.back()
108-
.executing()
109-
.execute("echo", arg);
115+
.command("echo", arg)
116+
.execute();
110117

111118
assertEquals(1, lines.size());
112119
assertEquals(arg, lines.get(0));

0 commit comments

Comments
 (0)