diff --git a/src/main/java/com/checkmarx/ast/wrapper/CxThinWrapper.java b/src/main/java/com/checkmarx/ast/wrapper/CxThinWrapper.java index 2e51d5d..b195b20 100644 --- a/src/main/java/com/checkmarx/ast/wrapper/CxThinWrapper.java +++ b/src/main/java/com/checkmarx/ast/wrapper/CxThinWrapper.java @@ -6,7 +6,6 @@ import java.io.IOException; import java.util.ArrayList; -import java.util.Arrays; import java.util.List; public class CxThinWrapper { @@ -22,7 +21,7 @@ public CxThinWrapper() throws IOException { public CxThinWrapper(@NonNull Logger logger) throws IOException { this.logger = logger; - this.executable = Execution.getTempBinary(); + this.executable = Execution.getTempBinary(logger); this.logger.info("Executable path: {} ", executable); } diff --git a/src/main/java/com/checkmarx/ast/wrapper/CxWrapper.java b/src/main/java/com/checkmarx/ast/wrapper/CxWrapper.java index 3f20df1..c72e1bd 100644 --- a/src/main/java/com/checkmarx/ast/wrapper/CxWrapper.java +++ b/src/main/java/com/checkmarx/ast/wrapper/CxWrapper.java @@ -49,7 +49,7 @@ public CxWrapper(@NonNull CxConfig cxConfig, @NonNull Logger logger) throws IOEx this.cxConfig = cxConfig; this.logger = logger; this.executable = StringUtils.isBlank(this.cxConfig.getPathToExecutable()) - ? Execution.getTempBinary() + ? Execution.getTempBinary(logger) : this.cxConfig.getPathToExecutable(); this.logger.info("Executable path: {} ", executable); } diff --git a/src/main/java/com/checkmarx/ast/wrapper/Execution.java b/src/main/java/com/checkmarx/ast/wrapper/Execution.java index 060563b..bf4233a 100644 --- a/src/main/java/com/checkmarx/ast/wrapper/Execution.java +++ b/src/main/java/com/checkmarx/ast/wrapper/Execution.java @@ -1,5 +1,6 @@ package com.checkmarx.ast.wrapper; +import lombok.NonNull; import org.slf4j.Logger; import java.io.*; @@ -23,10 +24,10 @@ private Execution() { } - private static final String OS_NAME = System.getProperty("os.name").toLowerCase(Locale.ENGLISH); private static final String OS_LINUX = "linux"; private static final String OS_WINDOWS = "windows"; - private static final List OS_MAC = Arrays.asList("mac os x", "darwin", "osx"); + private static final String OS_MAC = "mac"; + private static final List OS_MAC_NAMES = Arrays.asList("mac os x", "darwin", "osx"); private static final String FILE_NAME_LINUX = "cx-linux"; private static final String FILE_NAME_LINUX_ARM = "cx-linux-arm"; private static final String FILE_NAME_MAC = "cx-mac"; @@ -108,9 +109,9 @@ static String executeCommand(List arguments, StandardCharsets.UTF_8); } - static String getTempBinary() throws IOException { + static String getTempBinary(@NonNull Logger logger) throws IOException { if (executable == null) { - String fileName = detectBinaryName(); + String fileName = detectBinaryName(logger); if (fileName == null) { throw new IOException("Unsupported architecture"); } @@ -143,28 +144,39 @@ private static Process buildProcess(List commands) throws IOException { return lmBuilder.start(); } - private static String detectBinaryName() { - String osName = OS_NAME; + private static String detectBinaryName(@NonNull Logger logger) { + String osName = System.getProperty("os.name").toLowerCase(Locale.ENGLISH); String osArch = System.getProperty("os.arch").toLowerCase(Locale.ENGLISH); String fileName = null; + switch (getOperatingSystemType(osName)) { + case OS_LINUX: + fileName = osArch.contains("arm") || osArch.contains("aarch64") ? FILE_NAME_LINUX_ARM : FILE_NAME_LINUX; + break; + case OS_WINDOWS: + fileName = FILE_NAME_WINDOWS; + break; + case OS_MAC: + fileName = FILE_NAME_MAC; + break; + default: + // Handle unknown OS + logger.error("Unsupported operating system: {} Architecture: {}", osName, osArch); + break; + } + return fileName; + } + + private static String getOperatingSystemType(String osName) { if (osName.contains(OS_LINUX)) { - if (osArch.contains("arm") || osArch.contains("aarch64")) { - fileName = FILE_NAME_LINUX_ARM; - } else { - fileName = FILE_NAME_LINUX; - } + return OS_LINUX; } else if (osName.contains(OS_WINDOWS)) { - fileName = FILE_NAME_WINDOWS; + return OS_WINDOWS; + } else if (OS_MAC_NAMES.stream().anyMatch(osName::contains)) { + return OS_MAC; } else { - for (String macStr : OS_MAC) { - if (osName.contains(macStr)) { - fileName = FILE_NAME_MAC; - break; - } - } + return "UNKNOWN"; // Handle unknown OS (optional) } - return fileName; } private static void copyURLToFile(URL source, File destination) throws IOException {