Skip to content

Commit 50156ab

Browse files
committed
#1: Runtime, AppException, PathOf
1 parent 9049825 commit 50156ab

File tree

13 files changed

+454
-17
lines changed

13 files changed

+454
-17
lines changed

pom.xml

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,9 @@
9595
<code.cvrg.branch>0.50</code.cvrg.branch>
9696
<code.cvrg.line>0.50</code.cvrg.line>
9797
<sonar.coverage.exclusions>
98-
**/*Exception.java, **/*Fake*.java,**/*.Fake*.java,**/**.Fake*.java
98+
**/*Exception.java,
99+
**/*Fake*.java,**/*.Fake*.java,**/**.Fake*.java,
100+
**/RuntimeOf.java
99101
</sonar.coverage.exclusions>
100102
</properties>
101103
<dependencies>
@@ -105,7 +107,7 @@
105107
<version>${cactoos.version}</version>
106108
<scope>provided</scope>
107109
</dependency>
108-
<!-- @todo Upgrade the hamcrest-all library to the latest release of hamcrest-java -->
110+
<!-- @todo #/DEV Upgrade the hamcrest-all library to the latest release of hamcrest-java -->
109111
<dependency>
110112
<groupId>org.hamcrest</groupId>
111113
<artifactId>hamcrest-all</artifactId>
@@ -239,9 +241,10 @@
239241
<configuration>
240242
<output>file</output>
241243
<excludes>
242-
<exclude>org/dgroup/**/*Exception.*</exclude>
243-
<exclude>org/dgroup/**/*/*Fake*.*</exclude>
244-
<exclude>org/dgroup/**/*Envelope.*</exclude>
244+
<exclude>io/github/dgroup/**/*Exception.*</exclude>
245+
<exclude>io/github/dgroup/**/RuntimeOf.*</exclude>
246+
<exclude>io/github/dgroup/**/*/*Fake*.*</exclude>
247+
<exclude>io/github/dgroup/**/*Envelope.*</exclude>
245248
</excludes>
246249
</configuration>
247250
<executions>
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/*
2+
* MIT License
3+
*
4+
* Copyright (c) 2019 Yurii Dubinka
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"),
8+
* to deal in the Software without restriction, including without limitation
9+
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
10+
* and/or sell copies of the Software, and to permit persons to whom
11+
* the Software is furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included
14+
* in all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21+
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
22+
* OR OTHER DEALINGS IN THE SOFTWARE.
23+
*/
24+
package io.github.dgroup.term4j;
25+
26+
import org.cactoos.Text;
27+
import org.cactoos.iterable.IterableOf;
28+
29+
/**
30+
* Application error.
31+
*
32+
* Always has the error code which supposed to be exposed to the external
33+
* parent process like CLI, bash shell, etc.
34+
*
35+
* @since 0.1.0
36+
*/
37+
public final class AppException extends Exception {
38+
39+
/**
40+
* Application exit code.
41+
*/
42+
private final Integer exit;
43+
44+
/**
45+
* Message.
46+
*/
47+
private final Iterable<Text> msg;
48+
49+
/**
50+
* Ctor.
51+
* @param msg The message to be print to the user.
52+
*/
53+
public AppException(final String msg) {
54+
this(-1, msg);
55+
}
56+
57+
/**
58+
* Ctor.
59+
* @param msg The message to be print to the user.
60+
*/
61+
public AppException(final Text msg) {
62+
this(-1, msg);
63+
}
64+
65+
/**
66+
* Ctor.
67+
* @param exit Application exit code to be print to the user.
68+
* @param msg The message to be print to the user.
69+
*/
70+
public AppException(final Integer exit, final Text msg) {
71+
this(exit, new IterableOf<>(msg));
72+
}
73+
74+
/**
75+
* Ctor.
76+
* @param exit Application exit code to be print to the user.
77+
* @param msg The message to be print to the user.
78+
*/
79+
public AppException(final Integer exit, final String msg) {
80+
this(exit, new IterableOf<>(() -> msg));
81+
}
82+
83+
/**
84+
* Ctor.
85+
* @param exit Application exit code to be print to the user.
86+
* @param msg The message to be print to the user.
87+
*/
88+
public AppException(final Integer exit, final Iterable<Text> msg) {
89+
super();
90+
this.exit = exit;
91+
this.msg = msg;
92+
}
93+
94+
/**
95+
* Application exit code to be exposed to the parent OS process.
96+
* @return The code.
97+
*/
98+
public Integer exitCode() {
99+
return this.exit;
100+
}
101+
102+
/**
103+
* Application error message split by lines.
104+
* @return The message.
105+
*/
106+
public Iterable<Text> message() {
107+
return this.msg;
108+
}
109+
}

src/main/java/io/github/dgroup/term4j/Highlighted.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,18 @@
3030
* Highlighted text.
3131
*
3232
* @since 0.1.0
33+
* @todo #/DEV Use jansi lib in order to print colored text:
34+
* - <em>Green</em>;
35+
* - <em>Red</em>;
36+
* - <em>Yellow</em>;
37+
* - <em>White</em>;
38+
* - <em>Black</em>;
39+
* and <em>Bold</em> to make the text more expressively.
3340
*/
3441
public interface Highlighted extends Text {
3542

3643
/**
37-
* Convert it to the colored string.
44+
* Convert the text to the colored string.
3845
* @return The colored string
3946
*/
4047
String asString();
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* MIT License
3+
*
4+
* Copyright (c) 2019 Yurii Dubinka
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"),
8+
* to deal in the Software without restriction, including without limitation
9+
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
10+
* and/or sell copies of the Software, and to permit persons to whom
11+
* the Software is furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included
14+
* in all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21+
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
22+
* OR OTHER DEALINGS IN THE SOFTWARE.
23+
*/
24+
package io.github.dgroup.term4j;
25+
26+
/**
27+
* The current application process.
28+
*
29+
* @since 0.1.0
30+
*/
31+
public interface Runtime {
32+
33+
/**
34+
* Shutdown current CLI application using particular exit code in order to
35+
* notify the parent system process (for example bash script) about type of
36+
* failure.
37+
* @param code The exit code.
38+
*/
39+
void shutdownWith(int code);
40+
41+
}

src/main/java/io/github/dgroup/term4j/arg/ArgOf.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,21 @@ public final class ArgOf extends Envelope<String> {
3434

3535
/**
3636
* Ctor.
37-
* @param name Cmd argument name.
38-
* @param args All cmd arguments.
37+
* @param lbl The label of command-line argument.
38+
* @param args All command-line arguments.
3939
*/
40-
public ArgOf(final String name, final List<String> args) {
41-
super(name, args, arg -> arg);
40+
public ArgOf(final String lbl, final List<String> args) {
41+
super(lbl, args, arg -> arg);
4242
}
4343

4444
/**
4545
* Ctor.
46-
* @param name Cmd argument name.
47-
* @param args All cmd arguments.
46+
* @param lbl The label of command-line argument.
47+
* @param args All command-line arguments.
4848
* @param msg Error message in case if arguments wasn't specified by user.
4949
*/
50-
public ArgOf(final String name, final List<String> args, final String msg) {
51-
super(name, args, arg -> arg, () -> msg);
50+
public ArgOf(final String lbl, final List<String> args, final String msg) {
51+
super(lbl, args, arg -> arg, () -> msg);
5252
}
5353

5454
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* MIT License
3+
*
4+
* Copyright (c) 2019 Yurii Dubinka
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"),
8+
* to deal in the Software without restriction, including without limitation
9+
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
10+
* and/or sell copies of the Software, and to permit persons to whom
11+
* the Software is furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included
14+
* in all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21+
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
22+
* OR OTHER DEALINGS IN THE SOFTWARE.
23+
*/
24+
package io.github.dgroup.term4j.arg;
25+
26+
import java.nio.file.Path;
27+
import java.nio.file.Paths;
28+
import java.util.List;
29+
30+
/**
31+
* The single file command-line argument.
32+
*
33+
* @since 0.1.0
34+
*/
35+
public final class PathOf extends Envelope<Path> {
36+
37+
/**
38+
* Ctor.
39+
* @param lbl The label of command-line argument.
40+
* @param args All command-line arguments.
41+
*/
42+
public PathOf(final String lbl, final List<String> args) {
43+
super(lbl, args, arg -> Paths.get(arg));
44+
}
45+
46+
}

src/main/java/io/github/dgroup/term4j/arg/package-info.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,11 @@
2626
* The classes related to work with command-line arguments.
2727
*
2828
* @since 0.1.0
29+
* @todo #/DEV FirstIn argument (similar to switch statement) which allows to
30+
* instantiate the first value from the list of the arguments. For example,
31+
* the same parameter might be defined:
32+
* - command-line argument <em>-prop</em>,
33+
* - java property <em>-Dprop</em>
34+
* - env property <em>${PROP}</em>
2935
*/
3036
package io.github.dgroup.term4j.arg;

src/main/java/io/github/dgroup/term4j/package-info.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,7 @@
3131
*
3232
* @since 0.1.0
3333
* @see <a href="https://github.com/dgroup/term4j">GitHub repository</a>
34+
* @todo #/DEV Help: simple mechanism to print the help info from the classpath
35+
* resource.
3436
*/
3537
package io.github.dgroup.term4j;
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* MIT License
3+
*
4+
* Copyright (c) 2019 Yurii Dubinka
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"),
8+
* to deal in the Software without restriction, including without limitation
9+
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
10+
* and/or sell copies of the Software, and to permit persons to whom
11+
* the Software is furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included
14+
* in all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21+
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
22+
* OR OTHER DEALINGS IN THE SOFTWARE.
23+
*/
24+
package io.github.dgroup.term4j.runtime;
25+
26+
import io.github.dgroup.term4j.Runtime;
27+
28+
/**
29+
* Fake implementation of {@link Runtime} for unit-testing purposes.
30+
*
31+
* @since 0.1.0
32+
*/
33+
public final class Fake implements Runtime {
34+
35+
/**
36+
* Application exit code.
37+
*/
38+
private int exit;
39+
40+
@Override
41+
public void shutdownWith(final int code) {
42+
this.exit = code;
43+
}
44+
45+
/**
46+
* Application exit code.
47+
* @return Exit code.
48+
*/
49+
public int exitCode() {
50+
return this.exit;
51+
}
52+
}

0 commit comments

Comments
 (0)