Skip to content

Commit

Permalink
Merge pull request #676 from nsacyber/verison-helper-update
Browse files Browse the repository at this point in the history
Verison helper update
  • Loading branch information
cyrus-dev authored Jan 22, 2024
2 parents 49eb275 + 8dee0a9 commit 11dea55
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 11 deletions.
79 changes: 71 additions & 8 deletions HIRS_Utils/src/main/java/hirs/utils/VersionHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,54 +2,117 @@

import com.google.common.base.Charsets;
import com.google.common.io.Resources;
import lombok.extern.log4j.Log4j2;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;

/**
* Utility class to get the current version from the VERSION file.
*/
@Log4j2
public final class VersionHelper {

private static final String VERSION_FILENAME = "VERSION";
private static final String OPT_PREFIX = "/opt";
private static final String ETC_PREFIX = "/etc";
private static final String VERSION = "VERSION";

private VersionHelper() {
// intentionally blank, should never be instantiated
}

/**
* Get the current version of HIRS_Portal that is installed.
* Get the current version of HIRS_AttestationPortal that is installed.
*
* @return A string representing the current version.
*/
public static String getVersion() {
return getVersion(VERSION_FILENAME);
if (Files.exists(FileSystems.getDefault().getPath(OPT_PREFIX,
"hirs", "aca", VERSION))) {
return getVersion(FileSystems.getDefault().getPath(OPT_PREFIX,
"hirs", "aca", VERSION));
} else if (Files.exists(FileSystems.getDefault().getPath(ETC_PREFIX,
"hirs", "aca", VERSION))) {
return getVersion(FileSystems.getDefault().getPath(ETC_PREFIX,
"hirs", "aca", VERSION));
}

return getVersion(VERSION);
}

/**
* Get the current version of HIRS_AttestationCAPortal that is installed.
*
* @param filename that contains the version
* @return A string representing the current version.
*/
public static String getVersion(final Path filename) {
String version;
try {
version = getFileContents(filename.toString());
} catch (IOException ioEx) {
log.error(ioEx.getMessage());
version = "";
}

return version;
}

/**
* Get the current version of HIRS_Portal that is installed.
* Get the current version of HIRS_AttestationCAPortal that is installed.
*
* @param filename
* that contains the version
* @param filename that contains the version
* @return A string representing the current version.
*/
public static String getVersion(final String filename) {
String version;
try {
version = getFileContents(filename);
} catch (Exception e) {
version = getResourceContents(filename);
} catch (Exception ex) {
version = "";
log.error(ex.getMessage());
}

return version;
}

/**
* Read the symbolic link to VERSION in the top level HIRS directory.
*
* @param filename "VERSION"
* @return the version number from the file
* @throws IOException
*/
private static String getFileContents(final String filename) throws IOException {
final char[] buffer = new char[8192];
final StringBuilder result = new StringBuilder();
InputStream inputStream = new FileInputStream(filename);

try (Reader reader = new InputStreamReader(inputStream, Charsets.UTF_8)) {
int charsRead;
while ((charsRead = reader.read(buffer, 0, buffer.length)) > 0) {
result.append(buffer, 0, charsRead);
}
}

return result.toString();
}

/**
* Read the symbolic link to VERSION in the top level HIRS directory.
*
* @param filename "VERSION"
* @return the version number from the file
* @throws IOException
*/
private static String getResourceContents(final String filename) throws IOException {
URL url = Resources.getResource(filename);
return Resources.toString(url, Charsets.UTF_8).trim();
}
Expand Down
5 changes: 2 additions & 3 deletions HIRS_Utils/src/test/java/hirs/utils/VersionHelperTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package hirs.utils;

import org.junit.jupiter.api.Test;

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

Expand All @@ -14,7 +15,6 @@ public class VersionHelperTest {
*/
@Test
public void testGetVersionFail() {

String actual = VersionHelper.getVersion("somefile");
assertTrue(actual.startsWith(""));
}
Expand All @@ -24,9 +24,8 @@ public void testGetVersionFail() {
*/
@Test
public void testGetVersionDefault() {

String expected = "Test.Version";
String actual = VersionHelper.getVersion();
String actual = VersionHelper.getVersion("VERSION");
assertEquals(expected, actual);
}
}

0 comments on commit 11dea55

Please sign in to comment.