From c461e4a3a328d75ef1c8fd3b44d583a08b4e689e Mon Sep 17 00:00:00 2001 From: kerenr-jfrog Date: Tue, 11 Feb 2025 14:59:43 +0200 Subject: [PATCH 1/4] added CLIUtils class --- .../com/jfrog/ide/eclipse/utils/CLIUtils.java | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 bundle/src/main/java/com/jfrog/ide/eclipse/utils/CLIUtils.java diff --git a/bundle/src/main/java/com/jfrog/ide/eclipse/utils/CLIUtils.java b/bundle/src/main/java/com/jfrog/ide/eclipse/utils/CLIUtils.java new file mode 100644 index 0000000..dc0171e --- /dev/null +++ b/bundle/src/main/java/com/jfrog/ide/eclipse/utils/CLIUtils.java @@ -0,0 +1,64 @@ +package com.jfrog.ide.eclipse.utils; + +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; + +import com.jfrog.ide.eclipse.log.Logger; + +public class CLIUtils { + + public static final String JFROG_CLI_RELEASES_URL = "https://releases.jfrog.io/artifactory/jfrog-cli/v2-jf/"; + public static final String JFROG_CLI_VERSION = "2.73.4"; + + public static Logger logger = Logger.getInstance(); + + public static void downloadCLIIfNeeded() { + String systemFolderPath; + // get OS type and architecture + String osName = System.getProperty("os.name").toLowerCase(); + String osVersion = System.getProperty("os.version"); + String osArch = System.getProperty("os.arch"); + + if (osName.contains("win")) { + // For Windows, use SystemRoot environment variable + systemFolderPath = System.getenv("SystemRoot") + "\\System32"; + } else if (osName.contains("mac")) { + // For macOS, typically system folders are under /System + systemFolderPath = "/System/Library"; + } else if (osName.contains("nix") || osName.contains("nux") || osName.contains("aix")) { + // For Unix/Linux, system folders can be different, typically /usr or /usr/local + systemFolderPath = "/usr/bin"; + } else { + systemFolderPath = "Unsupported Operating System"; + } + + Path jfrogExeFilePath = Paths.get(systemFolderPath, "jf.exe"); + + // check if jfrog.exe file exist in system folder. if not download it + if (!Files.exists(jfrogExeFilePath)) { + logger.info("'jf.exe' file is not cached locally. Downloading it now..."); + downloadCliFromReleases(osName); + } else { + logger.info("'jf.exe' file is cached locally. File location: " + jfrogExeFilePath); + // run jf.exe --version in a process to verify correct CLI version + } + } + + public static String getCLIPath() { + + return ""; + } + + + public static void downloadCliFromReleases(String osName) { + String cliVersion = ""; + + // download the relevant jf.exe version according to the defined version & OS type. + if (osName.contains("win")) { + logger.info("Downloading CLI for windows"); + } else { + logger.info("Downloading CLI for linux/macOS"); + } + } +} \ No newline at end of file From 22ba670e9ad5059de3c04bb1041773d4bd39bc68 Mon Sep 17 00:00:00 2001 From: kerenr-jfrog Date: Tue, 11 Feb 2025 17:38:24 +0200 Subject: [PATCH 2/4] progress --- .../com/jfrog/ide/eclipse/utils/CLIUtils.java | 73 ++++++++++++++----- 1 file changed, 54 insertions(+), 19 deletions(-) diff --git a/bundle/src/main/java/com/jfrog/ide/eclipse/utils/CLIUtils.java b/bundle/src/main/java/com/jfrog/ide/eclipse/utils/CLIUtils.java index dc0171e..55c0dbb 100644 --- a/bundle/src/main/java/com/jfrog/ide/eclipse/utils/CLIUtils.java +++ b/bundle/src/main/java/com/jfrog/ide/eclipse/utils/CLIUtils.java @@ -3,22 +3,23 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; +import java.io.IOException; +import org.apache.commons.lang3.SystemUtils; +import org.apache.commons.lang3.StringUtils; import com.jfrog.ide.eclipse.log.Logger; public class CLIUtils { public static final String JFROG_CLI_RELEASES_URL = "https://releases.jfrog.io/artifactory/jfrog-cli/v2-jf/"; - public static final String JFROG_CLI_VERSION = "2.73.4"; + public static final String JFROG_CLI_FIXED_VERSION = "2.73.3"; public static Logger logger = Logger.getInstance(); - public static void downloadCLIIfNeeded() { + public static void downloadCLIIfNeeded() throws IOException { String systemFolderPath; - // get OS type and architecture + // get OS type String osName = System.getProperty("os.name").toLowerCase(); - String osVersion = System.getProperty("os.version"); - String osArch = System.getProperty("os.arch"); if (osName.contains("win")) { // For Windows, use SystemRoot environment variable @@ -38,27 +39,61 @@ public static void downloadCLIIfNeeded() { // check if jfrog.exe file exist in system folder. if not download it if (!Files.exists(jfrogExeFilePath)) { logger.info("'jf.exe' file is not cached locally. Downloading it now..."); - downloadCliFromReleases(osName); + downloadCliFromReleases(osName, JFROG_CLI_FIXED_VERSION); } else { logger.info("'jf.exe' file is cached locally. File location: " + jfrogExeFilePath); // run jf.exe --version in a process to verify correct CLI version } } - public static String getCLIPath() { + + public static void downloadCliFromReleases(String osName, String cliVersion) throws IOException { + String osAndArch = getOSAndArc(); + String fullCLIPath = JFROG_CLI_RELEASES_URL + cliVersion + "/jfrog-cli-" + osAndArch + "/jf.exe"; + String destinationPath = ""; // where we want to save the file? + + // download jf.exe from 'fullCLIPath' and save it at 'destinationPath' - return ""; } - - public static void downloadCliFromReleases(String osName) { - String cliVersion = ""; - - // download the relevant jf.exe version according to the defined version & OS type. - if (osName.contains("win")) { - logger.info("Downloading CLI for windows"); - } else { - logger.info("Downloading CLI for linux/macOS"); - } - } + public static String getOSAndArc() throws IOException { + String arch = SystemUtils.OS_ARCH; + // Windows + if (SystemUtils.IS_OS_WINDOWS) { + return "windows-amd64"; + } + // Mac + if (SystemUtils.IS_OS_MAC) { + if (StringUtils.equalsAny(arch, "aarch64", "arm64")) { + return "mac-arm64"; + } else { + return "mac-amd64"; + } + } + // Linux + if (SystemUtils.IS_OS_LINUX) { + switch (arch) { + case "i386": + case "i486": + case "i586": + case "i686": + case "i786": + case "x86": + return "linux-386"; + case "amd64": + case "x86_64": + case "x64": + return "linux-amd64"; + case "arm": + case "armv7l": + return "linux-arm"; + case "aarch64": + return "linux-arm64"; + case "ppc64": + case "ppc64le": + return "linux-" + arch; + } + } + throw new IOException(String.format("Unsupported OS: %s-%s", SystemUtils.OS_NAME, arch)); + } } \ No newline at end of file From 0a550441564088c7abe98049a0de98c60d09689e Mon Sep 17 00:00:00 2001 From: kerenr-jfrog Date: Wed, 12 Feb 2025 10:15:06 +0200 Subject: [PATCH 3/4] added todo comments --- .../com/jfrog/ide/eclipse/utils/CLIUtils.java | 31 ++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/bundle/src/main/java/com/jfrog/ide/eclipse/utils/CLIUtils.java b/bundle/src/main/java/com/jfrog/ide/eclipse/utils/CLIUtils.java index 55c0dbb..4056c11 100644 --- a/bundle/src/main/java/com/jfrog/ide/eclipse/utils/CLIUtils.java +++ b/bundle/src/main/java/com/jfrog/ide/eclipse/utils/CLIUtils.java @@ -3,8 +3,14 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; +import java.util.Arrays; +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import java.io.IOException; import org.apache.commons.lang3.SystemUtils; +import org.jfrog.build.extractor.executor.CommandExecutor; +import org.jfrog.build.extractor.executor.CommandResults; import org.apache.commons.lang3.StringUtils; import com.jfrog.ide.eclipse.log.Logger; @@ -43,6 +49,18 @@ public static void downloadCLIIfNeeded() throws IOException { } else { logger.info("'jf.exe' file is cached locally. File location: " + jfrogExeFilePath); // run jf.exe --version in a process to verify correct CLI version + CommandExecutor commandExecutor = new CommandExecutor(jfrogExeFilePath.toString(), null); + List versionCommand = Arrays.asList("--version"); + try { + CommandResults versionCommandOutput = commandExecutor.exeCommand(null, versionCommand, null, logger); + String cliVersion = extractVersion(versionCommandOutput.getRes()); + downloadCliFromReleases(osName, cliVersion); + } catch (InterruptedException | IOException e) { + // TODO: should we fail in case of error or download a new cli exe ? + logger.error("Failed to verify CLI version. Downloading v"+ JFROG_CLI_FIXED_VERSION); + downloadCliFromReleases(osName, JFROG_CLI_FIXED_VERSION); + } + } } @@ -52,7 +70,7 @@ public static void downloadCliFromReleases(String osName, String cliVersion) thr String fullCLIPath = JFROG_CLI_RELEASES_URL + cliVersion + "/jfrog-cli-" + osAndArch + "/jf.exe"; String destinationPath = ""; // where we want to save the file? - // download jf.exe from 'fullCLIPath' and save it at 'destinationPath' + // TODO: download jf.exe from 'fullCLIPath' and save it at 'destinationPath' } @@ -96,4 +114,15 @@ public static String getOSAndArc() throws IOException { } throw new IOException(String.format("Unsupported OS: %s-%s", SystemUtils.OS_NAME, arch)); } + + public static String extractVersion(String input) { + String regex = "\\d+(\\.\\d+)*"; + Pattern pattern = Pattern.compile(regex); + Matcher matcher = pattern.matcher(input); + + if (matcher.find()) { + return matcher.group(); + } + return null; + } } \ No newline at end of file From e0c605eeb9955c331a25bb8a910fa2d4bafd0599 Mon Sep 17 00:00:00 2001 From: kerenr-jfrog Date: Wed, 12 Feb 2025 12:17:09 +0200 Subject: [PATCH 4/4] finished implementation of downloadCliIfNeeded --- .../com/jfrog/ide/eclipse/utils/CLIUtils.java | 47 +++++++++++++------ 1 file changed, 33 insertions(+), 14 deletions(-) diff --git a/bundle/src/main/java/com/jfrog/ide/eclipse/utils/CLIUtils.java b/bundle/src/main/java/com/jfrog/ide/eclipse/utils/CLIUtils.java index 4056c11..9af7ded 100644 --- a/bundle/src/main/java/com/jfrog/ide/eclipse/utils/CLIUtils.java +++ b/bundle/src/main/java/com/jfrog/ide/eclipse/utils/CLIUtils.java @@ -12,13 +12,16 @@ import org.jfrog.build.extractor.executor.CommandExecutor; import org.jfrog.build.extractor.executor.CommandResults; import org.apache.commons.lang3.StringUtils; +import org.apache.maven.artifact.versioning.ComparableVersion; import com.jfrog.ide.eclipse.log.Logger; public class CLIUtils { public static final String JFROG_CLI_RELEASES_URL = "https://releases.jfrog.io/artifactory/jfrog-cli/v2-jf/"; - public static final String JFROG_CLI_FIXED_VERSION = "2.73.3"; + public static final String MINIMUM_JFROG_CLI_VERSION = "2.69.0"; // TODO: TBD + public static final String MAXIMUM_JFROG_CLI_VERSION = "2.73.3"; // TODO: TBD + public static final String DEFAULT_CLI_DESTINATION_PATH = ""; // TODO: determine where we would like to download and save the cli exe public static Logger logger = Logger.getInstance(); @@ -37,38 +40,46 @@ public static void downloadCLIIfNeeded() throws IOException { // For Unix/Linux, system folders can be different, typically /usr or /usr/local systemFolderPath = "/usr/bin"; } else { - systemFolderPath = "Unsupported Operating System"; + logger.error("Unsupported Operating System " + osName); + throw new UnsupportedOperationException("Unsupported Operating System " + osName); } Path jfrogExeFilePath = Paths.get(systemFolderPath, "jf.exe"); - // check if jfrog.exe file exist in system folder. if not download it - if (!Files.exists(jfrogExeFilePath)) { - logger.info("'jf.exe' file is not cached locally. Downloading it now..."); - downloadCliFromReleases(osName, JFROG_CLI_FIXED_VERSION); - } else { + // Check if 'jf.exe' exists locally + if (Files.exists(jfrogExeFilePath)) { logger.info("'jf.exe' file is cached locally. File location: " + jfrogExeFilePath); - // run jf.exe --version in a process to verify correct CLI version + + // Execute 'jf.exe --version' to verify CLI version CommandExecutor commandExecutor = new CommandExecutor(jfrogExeFilePath.toString(), null); List versionCommand = Arrays.asList("--version"); + try { CommandResults versionCommandOutput = commandExecutor.exeCommand(null, versionCommand, null, logger); String cliVersion = extractVersion(versionCommandOutput.getRes()); - downloadCliFromReleases(osName, cliVersion); + + if (validateCLIVersion(cliVersion)) { + logger.debug("Local CLI version is: " + cliVersion); + logger.info("Local 'jf.exe' file version has been verified and is compatible. Proceeding with its usage."); + } else { + logger.info("Local 'jf.exe' file version is not compatible. Downloading v" + MAXIMUM_JFROG_CLI_VERSION); + downloadCliFromReleases(osName, MAXIMUM_JFROG_CLI_VERSION, systemFolderPath); + } } catch (InterruptedException | IOException e) { // TODO: should we fail in case of error or download a new cli exe ? - logger.error("Failed to verify CLI version. Downloading v"+ JFROG_CLI_FIXED_VERSION); - downloadCliFromReleases(osName, JFROG_CLI_FIXED_VERSION); + logger.error("Failed to verify CLI version. Downloading v"+ MAXIMUM_JFROG_CLI_VERSION); + downloadCliFromReleases(osName, MAXIMUM_JFROG_CLI_VERSION, systemFolderPath); } - + } else { + logger.info("'jf.exe' file is not cached locally. Downloading it now..."); + downloadCliFromReleases(osName, MAXIMUM_JFROG_CLI_VERSION, systemFolderPath); } } - public static void downloadCliFromReleases(String osName, String cliVersion) throws IOException { + public static void downloadCliFromReleases(String osName, String cliVersion, String destinationPath) throws IOException { String osAndArch = getOSAndArc(); String fullCLIPath = JFROG_CLI_RELEASES_URL + cliVersion + "/jfrog-cli-" + osAndArch + "/jf.exe"; - String destinationPath = ""; // where we want to save the file? // TODO: download jf.exe from 'fullCLIPath' and save it at 'destinationPath' @@ -125,4 +136,12 @@ public static String extractVersion(String input) { } return null; } + + public static Boolean validateCLIVersion(String cliVersion) { + ComparableVersion currentCLIVersion = new ComparableVersion(cliVersion); + ComparableVersion maxCLIVersion = new ComparableVersion(MAXIMUM_JFROG_CLI_VERSION); + ComparableVersion minCLIVersion = new ComparableVersion(MINIMUM_JFROG_CLI_VERSION); + + return currentCLIVersion.compareTo(minCLIVersion) >=0 && currentCLIVersion.compareTo(maxCLIVersion) <= 0; + } } \ No newline at end of file