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

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
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?
  • Loading branch information
pesse committed May 31, 2018
commit fbbef6d8684ceaf5c539c75f65783da68eb204c5
36 changes: 36 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<maven.compiler.target>1.8</maven.compiler.target>
<junit.platform.version>1.0.3</junit.platform.version>
<junit.jupiter.version>5.0.3</junit.jupiter.version>
<travisBuildNumber>local</travisBuildNumber>
</properties>

<dependencies>
Expand Down Expand Up @@ -88,6 +89,41 @@
</programs>
</configuration>
</plugin>
<plugin>
<groupId>com.google.code.maven-replacer-plugin</groupId>
<artifactId>replacer</artifactId>
<version>1.5.3</version>
<executions>
<execution>
<id>replace-version-number</id>
<phase>generate-sources</phase>
<goals>
<goal>replace</goal>
</goals>
</execution>
</executions>
<configuration>
<basedir>${project.basedir}/src/main/java</basedir>
<includes>
<include>**/CliVersionInfo.java</include>
</includes>
<preserveDir>true</preserveDir>
<replacements>
<replacement>
<token>MAVEN_PROJECT_NAME = ".*"</token>
<value>MAVEN_PROJECT_NAME = "${project.name}"</value>
</replacement>
<replacement>
<token>MAVEN_PROJECT_VERSION = ".*"</token>
<value>MAVEN_PROJECT_VERSION = "${project.version}"</value>
</replacement>
<replacement>
<token>BUILD_NO = ".*"</token>
<value>BUILD_NO = "${travisBuildNumber}"</value>
</replacement>
</replacements>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
Expand Down
10 changes: 8 additions & 2 deletions src/main/java/org/utplsql/cli/Cli.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public class Cli {

static final String HELP_CMD = "-h";
private static final String RUN_CMD = "run";
private static final String VERSION_CMD = "info";

public static void main(String[] args) {

Expand All @@ -22,16 +23,21 @@ public static void main(String[] args) {
jc.setProgramName("utplsql");
// jc.addCommand(HELP_CMD, new HelpCommand());
RunCommand runCmd = new RunCommand();
VersionInfoCommand infoCmd = new VersionInfoCommand();
jc.addCommand(RUN_CMD, runCmd);

jc.addCommand(VERSION_CMD, infoCmd);
int exitCode = DEFAULT_ERROR_CODE;

try {
jc.parse(args);

if (RUN_CMD.equals(jc.getParsedCommand())) {
exitCode = runCmd.run();
} else {
}
else if ( VERSION_CMD.equals(jc.getParsedCommand()) ) {
exitCode = infoCmd.run();
}
else {
throw new ParameterException("Command not specified.");
}
} catch (ParameterException e) {
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/org/utplsql/cli/CliVersionInfo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.utplsql.cli;

/** This class is getting updated automatically by the build process.
* Please do not update its constants manually cause they will be overwritten.
*
* @author pesse
*/
public class CliVersionInfo {

private static final String BUILD_NO = "local";
private static final String MAVEN_PROJECT_NAME = "cli";
private static final String MAVEN_PROJECT_VERSION = "3.1.1-SNAPSHOT";

public static String getVersion() {
return MAVEN_PROJECT_VERSION + "." + BUILD_NO;
}

public static String getInfo() { return MAVEN_PROJECT_NAME + " " + getVersion(); }

}
55 changes: 55 additions & 0 deletions src/main/java/org/utplsql/cli/VersionInfoCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package org.utplsql.cli;


import com.beust.jcommander.Parameter;
import com.beust.jcommander.Parameters;
import org.utplsql.api.DBHelper;
import org.utplsql.api.JavaApiVersionInfo;
import org.utplsql.api.Version;
import org.utplsql.api.exception.UtPLSQLNotInstalledException;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

@Parameters(separators = "=", commandDescription = "prints version information of cli, java-api and - if connection is given - database utPLSQL framework")
public class VersionInfoCommand {

@Parameter(
converter = ConnectionInfo.ConnectionStringConverter.class,
variableArity = true,
description = "<user>/<password>@//<host>[:<port>]/<service> OR <user>/<password>@<TNSName> OR <user>/<password>@<host>:<port>:<SID>")
private List<ConnectionInfo> connectionInfoList = new ArrayList<>();

public ConnectionInfo getConnectionInfo() {
if ( connectionInfoList != null && connectionInfoList.size() > 0 )
return connectionInfoList.get(0);
else
return null;
}

public int run() throws Exception {

System.out.println(CliVersionInfo.getInfo());
System.out.println("Java-API " + JavaApiVersionInfo.getVersion());

ConnectionInfo ci = getConnectionInfo();
if ( ci != null ) {
// TODO: Ora-check
ci.setMaxConnections(1);
try (Connection con = ci.getConnection()) {
Version v = DBHelper.getDatabaseFrameworkVersion( con );
System.out.println("utPLSQL " + v.getNormalizedString());
}
catch ( UtPLSQLNotInstalledException e ) {
System.out.println("utPLSQL framework is not installed in database.");
}
catch ( Exception e ) {
e.printStackTrace();
return 1;
}
}

return 0;
}
}
43 changes: 43 additions & 0 deletions src/test/java/org/utplsql/cli/VersionInfoCommandIT.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package org.utplsql.cli;

import com.beust.jcommander.JCommander;
import org.junit.jupiter.api.Test;
import org.utplsql.api.EnvironmentVariableUtil;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class VersionInfoCommandIT {

@Test
public void infoCommandRunsWithoutConnection() throws Exception {

VersionInfoCommand infoCmd = new VersionInfoCommand();

JCommander.newBuilder()
.addObject(infoCmd)
.args(new String[]{})
.build();

int result = infoCmd.run();

assertEquals(0, result);
}
@Test
public void infoCommandRunsWithConnection() throws Exception {

String Url = EnvironmentVariableUtil.getEnvValue("DB_URL", "192.168.99.100:1521:XE");
String sUser = EnvironmentVariableUtil.getEnvValue("DB_USER", "app");
String sPass = EnvironmentVariableUtil.getEnvValue("DB_PASS", "app");

VersionInfoCommand infoCmd = new VersionInfoCommand();

JCommander.newBuilder()
.addObject(infoCmd)
.args(new String[]{sUser + "/" + sPass + "@" + Url})
.build();

int result = infoCmd.run();

assertEquals(0, result);
}
}