Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Command line option for version #227

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 2 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ jobs:
run: |
echo CHANGELIST= >> $GITHUB_ENV

- name: Add git hash to snapshot release
- name: Add git hash and to snapshot release
if: startsWith(github.ref, 'refs/tags/') && !startsWith(github.ref, 'refs/tags/v')
run: |
echo CHANGELIST=${{ env.CHANGELIST }}.${{ env. GIT_HASH_SHORT }} >> $GITHUB_ENV
echo CHANGELIST=${{ env.CHANGELIST }}#$GITHUB_RUN_NUMBER-${{ env.GIT_HASH_SHORT }} >> $GITHUB_ENV

- name: Set up JDK 17
uses: actions/setup-java@v3
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- preconditions which observe mdib changes during the test run
- storing of IP addresses of inbound messages in the database
- a command line parameter to not create subdirectories in the test run directory
- a command line parameter to print the version of the sdccc test tool

### Changed

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ The following command line options are supported by the test tool, the first two
| --test_run_directory | -d | base directory to store test runs in, creates a timestamped SDCcc run | no |
| --no_subdirectories | -ns | if set to "true", no directories are created in the directory configured with test_run_directory. The configured directory must be empty if no_subdirectories is set to "true" | no |
| --file_log_level | -fll | log level to be used for the log file being created, e.g. DEBUG, defaults to INFO | no |
| --version | -v | Print the version of the test tool. Can only be used without any other command line options. | no |

### Enabling Tests

Expand Down
6 changes: 6 additions & 0 deletions sdccc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,12 @@
<artifactId>maven-antrun-plugin</artifactId>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>

<profiles>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,16 @@

import edu.umd.cs.findbugs.annotations.Nullable;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.nio.file.Path;
import java.util.Iterator;
import java.util.Objects;
import java.util.Optional;
import java.util.Properties;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.HelpFormatter;
Expand Down Expand Up @@ -45,6 +49,7 @@ public class CommandLineOptions {
private static final String TEST_RUN_DIRECTORY = "test_run_directory";
private static final String NO_SUBDIRECTORIES = "no_subdirectories";
private static final String FILE_LOG_LEVEL = "file_log_level";
private static final String VERSION = "version";
private final Path configPath;
private final Path testConfigPath;
private final Path testParameterPath;
Expand Down Expand Up @@ -79,14 +84,17 @@ public CommandLineOptions(final String[] commandLineArguments) {
try {
cmd = parser.parse(options, commandLineArguments);
} catch (final ParseException e) {
System.out.println(e.getMessage());
help.printHelp("SDCcc", options);

final var version = setupVersion();
final var versionParser = new DefaultParser();
try {
printNetworkAdapterInformation();
} catch (final SocketException e2) {
LOG.error("Error while printing network adapter info", e2);
throw new RuntimeException(e2);
final CommandLine cmdVersion = versionParser.parse(version, commandLineArguments);
if (cmdVersion.hasOption(VERSION)) {
printVersion();
} else {
printNetworkInfo(e, help, options, version);
}
} catch (final ParseException ex) {
printNetworkInfo(e, help, options, version);
}
System.exit(1);
}
Expand All @@ -109,6 +117,46 @@ public CommandLineOptions(final String[] commandLineArguments) {
this.fileLogLevel = Level.toLevel(cmd.getOptionValue(FILE_LOG_LEVEL), Level.INFO);
}

private void printVersion() {
final Properties properties = new Properties();
try (InputStream input = this.getClass().getResourceAsStream("config.properties")) {
properties.load(input);
final var devVersion = properties.getProperty("project.version");
System.out.println(Objects.requireNonNullElse(devVersion, "No project version information available."));
} catch (IOException ex) {
LOG.error("Error loading project version", ex);
throw new RuntimeException(ex);
}
}

private void printNetworkInfo(
final ParseException e, final HelpFormatter help, final Options options, final Options version) {
System.out.println(e.getMessage());
for (var option : version.getOptions()) {
options.addOption(option);
}
help.printHelp("SDCcc", options);

try {
printNetworkAdapterInformation();
} catch (final SocketException e2) {
LOG.error("Error while printing network adapter info", e2);
throw new RuntimeException(e2);
}
}

private Options setupVersion() {
final var options = new Options();
{
final String description =
"Print the version of the test tool. Can only be used without any other command line options.";
final var version = new Option("v", VERSION, false, description);
version.setRequired(false);
options.addOption(version);
}
return options;
}

private Options setupOptions() {
final var options = new Options();

Expand Down Expand Up @@ -203,7 +251,6 @@ private Options setupOptions() {
fileLogLevelOpt.setRequired(false);
options.addOption(fileLogLevelOpt);
}

return options;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
project.version=@revision@@changelist@
Loading