Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fbbef6d

Browse files
committed
Minimal viable (and ugly) solution to add version info to cli
Refactoring needed. Tonight is a tough night for concentration. Turning on programming/concentration music didn't help either. Guess what they played? Command&Conquer Soundtrack. Who could concentrate with so many nice and positive memories triggered?
1 parent e8251c7 commit fbbef6d

5 files changed

Lines changed: 162 additions & 2 deletions

File tree

pom.xml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
<maven.compiler.target>1.8</maven.compiler.target>
1717
<junit.platform.version>1.0.3</junit.platform.version>
1818
<junit.jupiter.version>5.0.3</junit.jupiter.version>
19+
<travisBuildNumber>local</travisBuildNumber>
1920
</properties>
2021

2122
<dependencies>
@@ -88,6 +89,41 @@
8889
</programs>
8990
</configuration>
9091
</plugin>
92+
<plugin>
93+
<groupId>com.google.code.maven-replacer-plugin</groupId>
94+
<artifactId>replacer</artifactId>
95+
<version>1.5.3</version>
96+
<executions>
97+
<execution>
98+
<id>replace-version-number</id>
99+
<phase>generate-sources</phase>
100+
<goals>
101+
<goal>replace</goal>
102+
</goals>
103+
</execution>
104+
</executions>
105+
<configuration>
106+
<basedir>${project.basedir}/src/main/java</basedir>
107+
<includes>
108+
<include>**/CliVersionInfo.java</include>
109+
</includes>
110+
<preserveDir>true</preserveDir>
111+
<replacements>
112+
<replacement>
113+
<token>MAVEN_PROJECT_NAME = ".*"</token>
114+
<value>MAVEN_PROJECT_NAME = "${project.name}"</value>
115+
</replacement>
116+
<replacement>
117+
<token>MAVEN_PROJECT_VERSION = ".*"</token>
118+
<value>MAVEN_PROJECT_VERSION = "${project.version}"</value>
119+
</replacement>
120+
<replacement>
121+
<token>BUILD_NO = ".*"</token>
122+
<value>BUILD_NO = "${travisBuildNumber}"</value>
123+
</replacement>
124+
</replacements>
125+
</configuration>
126+
</plugin>
91127
<plugin>
92128
<groupId>org.apache.maven.plugins</groupId>
93129
<artifactId>maven-surefire-plugin</artifactId>

src/main/java/org/utplsql/cli/Cli.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public class Cli {
1313

1414
static final String HELP_CMD = "-h";
1515
private static final String RUN_CMD = "run";
16+
private static final String VERSION_CMD = "info";
1617

1718
public static void main(String[] args) {
1819

@@ -22,16 +23,21 @@ public static void main(String[] args) {
2223
jc.setProgramName("utplsql");
2324
// jc.addCommand(HELP_CMD, new HelpCommand());
2425
RunCommand runCmd = new RunCommand();
26+
VersionInfoCommand infoCmd = new VersionInfoCommand();
2527
jc.addCommand(RUN_CMD, runCmd);
26-
28+
jc.addCommand(VERSION_CMD, infoCmd);
2729
int exitCode = DEFAULT_ERROR_CODE;
2830

2931
try {
3032
jc.parse(args);
3133

3234
if (RUN_CMD.equals(jc.getParsedCommand())) {
3335
exitCode = runCmd.run();
34-
} else {
36+
}
37+
else if ( VERSION_CMD.equals(jc.getParsedCommand()) ) {
38+
exitCode = infoCmd.run();
39+
}
40+
else {
3541
throw new ParameterException("Command not specified.");
3642
}
3743
} catch (ParameterException e) {
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package org.utplsql.cli;
2+
3+
/** This class is getting updated automatically by the build process.
4+
* Please do not update its constants manually cause they will be overwritten.
5+
*
6+
* @author pesse
7+
*/
8+
public class CliVersionInfo {
9+
10+
private static final String BUILD_NO = "local";
11+
private static final String MAVEN_PROJECT_NAME = "cli";
12+
private static final String MAVEN_PROJECT_VERSION = "3.1.1-SNAPSHOT";
13+
14+
public static String getVersion() {
15+
return MAVEN_PROJECT_VERSION + "." + BUILD_NO;
16+
}
17+
18+
public static String getInfo() { return MAVEN_PROJECT_NAME + " " + getVersion(); }
19+
20+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package org.utplsql.cli;
2+
3+
4+
import com.beust.jcommander.Parameter;
5+
import com.beust.jcommander.Parameters;
6+
import org.utplsql.api.DBHelper;
7+
import org.utplsql.api.JavaApiVersionInfo;
8+
import org.utplsql.api.Version;
9+
import org.utplsql.api.exception.UtPLSQLNotInstalledException;
10+
11+
import java.sql.Connection;
12+
import java.util.ArrayList;
13+
import java.util.List;
14+
15+
@Parameters(separators = "=", commandDescription = "prints version information of cli, java-api and - if connection is given - database utPLSQL framework")
16+
public class VersionInfoCommand {
17+
18+
@Parameter(
19+
converter = ConnectionInfo.ConnectionStringConverter.class,
20+
variableArity = true,
21+
description = "<user>/<password>@//<host>[:<port>]/<service> OR <user>/<password>@<TNSName> OR <user>/<password>@<host>:<port>:<SID>")
22+
private List<ConnectionInfo> connectionInfoList = new ArrayList<>();
23+
24+
public ConnectionInfo getConnectionInfo() {
25+
if ( connectionInfoList != null && connectionInfoList.size() > 0 )
26+
return connectionInfoList.get(0);
27+
else
28+
return null;
29+
}
30+
31+
public int run() throws Exception {
32+
33+
System.out.println(CliVersionInfo.getInfo());
34+
System.out.println("Java-API " + JavaApiVersionInfo.getVersion());
35+
36+
ConnectionInfo ci = getConnectionInfo();
37+
if ( ci != null ) {
38+
// TODO: Ora-check
39+
ci.setMaxConnections(1);
40+
try (Connection con = ci.getConnection()) {
41+
Version v = DBHelper.getDatabaseFrameworkVersion( con );
42+
System.out.println("utPLSQL " + v.getNormalizedString());
43+
}
44+
catch ( UtPLSQLNotInstalledException e ) {
45+
System.out.println("utPLSQL framework is not installed in database.");
46+
}
47+
catch ( Exception e ) {
48+
e.printStackTrace();
49+
return 1;
50+
}
51+
}
52+
53+
return 0;
54+
}
55+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package org.utplsql.cli;
2+
3+
import com.beust.jcommander.JCommander;
4+
import org.junit.jupiter.api.Test;
5+
import org.utplsql.api.EnvironmentVariableUtil;
6+
7+
import static org.junit.jupiter.api.Assertions.assertEquals;
8+
9+
public class VersionInfoCommandIT {
10+
11+
@Test
12+
public void infoCommandRunsWithoutConnection() throws Exception {
13+
14+
VersionInfoCommand infoCmd = new VersionInfoCommand();
15+
16+
JCommander.newBuilder()
17+
.addObject(infoCmd)
18+
.args(new String[]{})
19+
.build();
20+
21+
int result = infoCmd.run();
22+
23+
assertEquals(0, result);
24+
}
25+
@Test
26+
public void infoCommandRunsWithConnection() throws Exception {
27+
28+
String Url = EnvironmentVariableUtil.getEnvValue("DB_URL", "192.168.99.100:1521:XE");
29+
String sUser = EnvironmentVariableUtil.getEnvValue("DB_USER", "app");
30+
String sPass = EnvironmentVariableUtil.getEnvValue("DB_PASS", "app");
31+
32+
VersionInfoCommand infoCmd = new VersionInfoCommand();
33+
34+
JCommander.newBuilder()
35+
.addObject(infoCmd)
36+
.args(new String[]{sUser + "/" + sPass + "@" + Url})
37+
.build();
38+
39+
int result = infoCmd.run();
40+
41+
assertEquals(0, result);
42+
}
43+
}

0 commit comments

Comments
 (0)