Skip to content

Commit

Permalink
Update code to pull from 2 different locations
Browse files Browse the repository at this point in the history
  • Loading branch information
cyrus-dev committed Jan 18, 2024
1 parent 3134a16 commit 8dee0a9
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 13 deletions.
41 changes: 29 additions & 12 deletions HIRS_Utils/src/main/java/hirs/utils/VersionHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.io.Reader;
import java.net.URL;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;

/**
Expand All @@ -19,20 +20,31 @@
@Log4j2
public final class VersionHelper {

private static final Path VERSION_PATH = FileSystems.getDefault().getPath(
"/opt", "hirs", "aca", "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_PATH);
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);
}

/**
Expand All @@ -42,7 +54,15 @@ public static String getVersion() {
* @return A string representing the current version.
*/
public static String getVersion(final Path filename) {
return getVersion(filename.toString());
String version;
try {
version = getFileContents(filename.toString());
} catch (IOException ioEx) {
log.error(ioEx.getMessage());
version = "";
}

return version;
}

/**
Expand All @@ -54,15 +74,12 @@ public static String getVersion(final Path filename) {
public static String getVersion(final String filename) {
String version;
try {
version = getFileContents(filename);
version = getResourceContents(filename);
} catch (Exception ex) {
try {
version = getResourceContents(filename);
} catch (Exception e) {
version = "";
log.error(e.getMessage());
}
version = "";
log.error(ex.getMessage());
}

return version;
}

Expand Down
1 change: 0 additions & 1 deletion HIRS_Utils/src/test/java/hirs/utils/VersionHelperTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ public class VersionHelperTest {
*/
@Test
public void testGetVersionFail() {

String actual = VersionHelper.getVersion("somefile");
assertTrue(actual.startsWith(""));
}
Expand Down

0 comments on commit 8dee0a9

Please sign in to comment.