forked from utPLSQL/utPLSQL-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReportersCommand.java
More file actions
76 lines (59 loc) · 2.76 KB
/
ReportersCommand.java
File metadata and controls
76 lines (59 loc) · 2.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package org.utplsql.cli;
import org.utplsql.api.exception.DatabaseNotCompatibleException;
import org.utplsql.api.exception.UtPLSQLNotInstalledException;
import org.utplsql.api.reporter.ReporterFactory;
import org.utplsql.api.reporter.inspect.ReporterInfo;
import org.utplsql.api.reporter.inspect.ReporterInspector;
import org.utplsql.cli.exception.DatabaseConnectionFailed;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;
import javax.sql.DataSource;
import java.io.PrintStream;
import java.sql.Connection;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
@Command(name = "reporters", description = "prints a list of reporters available in the specified database")
public class ReportersCommand implements ICommand {
@Parameters(description = UtplsqlPicocliCommand.COMMANDLINE_PARAM_DESCRIPTION, arity = "1")
private String connectionString;
@Option(names = "-h", usageHelp = true, description = "display this help and exit")
boolean help;
@Override
public int run() {
LoggerConfiguration.configure(LoggerConfiguration.ConfigLevel.NONE);
try {
DataSource ds = DataSourceProvider.getDataSource(connectionString, 1);
try (Connection con = ds.getConnection()) {
ReporterFactory reporterFactory = ReporterFactoryProvider.createReporterFactory(con);
writeReporters(ReporterInspector.create(reporterFactory, con).getReporterInfos(), System.out);
}
} catch (DatabaseNotCompatibleException | UtPLSQLNotInstalledException | DatabaseConnectionFailed | IllegalArgumentException e) {
System.out.println(e.getMessage());
return 1;
} catch (Exception e) {
e.printStackTrace();
return 1;
}
return 0;
}
private void writeReporters(List<ReporterInfo> reporterInfos, PrintStream out) {
reporterInfos.stream()
.sorted(Comparator.comparing(ReporterInfo::getName))
.forEach(info -> writeReporter(info, 4, out));
}
private void writeReporter(ReporterInfo info, int padding, PrintStream out) {
writeReporterName(info, padding, out);
writeReporterDescription(info, padding, out);
out.println();
}
private void writeReporterName(ReporterInfo info, int paddingRight, PrintStream out) {
out.println(info.getName() + ":");
}
private void writeReporterDescription(ReporterInfo info, int paddingLeft, PrintStream out) {
String[] lines = info.getDescription().split("\n");
String paddingLeftStr = String.format("%1$" + paddingLeft + "s", "");
Arrays.stream(lines).forEach(line -> out.println(paddingLeftStr + line.trim()));
}
}