From be9b11dbad01bb1f8c41fd25c4c8032a6582f711 Mon Sep 17 00:00:00 2001 From: Ruud Senden <8635138+rsenden@users.noreply.github.com> Date: Thu, 1 Feb 2024 06:24:17 +0100 Subject: [PATCH] chore: Re-introduce support for versions specified with 'v' prefix --- .../cli/cmd/AbstractToolInstallCommand.java | 2 +- .../helper/ToolInstallationHelper.java | 20 ++++++---- .../helper/ToolDefinitionRootDescriptor.java | 11 +++++- .../cli/ftest/tool/ToolFoDUploaderSpec.groovy | 38 +++++++++++++++++++ 4 files changed, 61 insertions(+), 10 deletions(-) diff --git a/fcli-core/fcli-tool/src/main/java/com/fortify/cli/tool/_common/cli/cmd/AbstractToolInstallCommand.java b/fcli-core/fcli-tool/src/main/java/com/fortify/cli/tool/_common/cli/cmd/AbstractToolInstallCommand.java index 1a2f13d83f..8873764943 100644 --- a/fcli-core/fcli-tool/src/main/java/com/fortify/cli/tool/_common/cli/cmd/AbstractToolInstallCommand.java +++ b/fcli-core/fcli-tool/src/main/java/com/fortify/cli/tool/_common/cli/cmd/AbstractToolInstallCommand.java @@ -217,7 +217,7 @@ private final void addUninstallPreparation(ToolDefinitionVersionDescriptor versi var toolName = installer.getToolName(); var installationDescriptor = ToolInstallationDescriptor.load(toolName, versionDescriptor); if ( installationDescriptor!=null ) { - var msg = String.format("Uninstall %s v%s from %s", toolName, versionDescriptor.getVersion(), installationDescriptor.getInstallDir()); + var msg = String.format("Uninstall %s %s from %s", toolName, versionDescriptor.getVersion(), installationDescriptor.getInstallDir()); requiredPreparations.put(msg, ()->uninstall(versionDescriptor, installationDescriptor)); } } diff --git a/fcli-core/fcli-tool/src/main/java/com/fortify/cli/tool/_common/helper/ToolInstallationHelper.java b/fcli-core/fcli-tool/src/main/java/com/fortify/cli/tool/_common/helper/ToolInstallationHelper.java index f298ef0a38..6db7e2efb2 100644 --- a/fcli-core/fcli-tool/src/main/java/com/fortify/cli/tool/_common/helper/ToolInstallationHelper.java +++ b/fcli-core/fcli-tool/src/main/java/com/fortify/cli/tool/_common/helper/ToolInstallationHelper.java @@ -34,18 +34,22 @@ public static final Path getToolsStatePath() { /** * The given version descriptor is considered a candidate for uninstall - * if all of the following conditions are met: - * - The full version is listed in --uninstall - * - The major version is listed in --uninstall - * - The major & minor version is listed in --uninstall - * - An installation descriptor for the version exists + * if an installation descriptor for the version exists, and any of the + * following conditions are met: + * - The full version is listed in --uninstall, optionally prefixed with 'v' + * - The major version is listed in --uninstall, optionally prefixed with 'v' + * - The major & minor version is listed in --uninstall, optionally prefixed with 'v' */ public static final boolean isCandidateForUninstall(String toolName, Set versionsToUninstall, ToolDefinitionVersionDescriptor versionDescriptor) { var version = versionDescriptor.getVersion(); return (versionsToUninstall.contains("all") - || versionsToUninstall.contains(version) - || versionsToUninstall.contains(SemVerHelper.getMajor(version).orElse("N/A")) - || versionsToUninstall.contains(SemVerHelper.getMajorMinor(version).orElse("N/A"))) + || containsCandidateForUninstall(versionsToUninstall, version) + || containsCandidateForUninstall(versionsToUninstall, SemVerHelper.getMajor(version).orElse("N/A")) + || containsCandidateForUninstall(versionsToUninstall, SemVerHelper.getMajorMinor(version).orElse("N/A"))) && ToolInstallationDescriptor.load(toolName, versionDescriptor)!=null; } + + private static final boolean containsCandidateForUninstall(Set versionsToUninstall, String candidate) { + return versionsToUninstall.contains(candidate) || versionsToUninstall.contains("v"+candidate); + } } diff --git a/fcli-core/fcli-tool/src/main/java/com/fortify/cli/tool/definitions/helper/ToolDefinitionRootDescriptor.java b/fcli-core/fcli-tool/src/main/java/com/fortify/cli/tool/definitions/helper/ToolDefinitionRootDescriptor.java index 15e9a8c847..bc22fa66a2 100644 --- a/fcli-core/fcli-tool/src/main/java/com/fortify/cli/tool/definitions/helper/ToolDefinitionRootDescriptor.java +++ b/fcli-core/fcli-tool/src/main/java/com/fortify/cli/tool/definitions/helper/ToolDefinitionRootDescriptor.java @@ -43,7 +43,7 @@ public final Stream getVersionsStream() { public final ToolDefinitionVersionDescriptor getVersion(String versionOrAlias) { return getVersionsStream() - .filter(v-> (v.getVersion().equals(versionOrAlias) || Arrays.stream(v.getAliases()).anyMatch(versionOrAlias::equals)) ) + .filter(v->matches(v, versionOrAlias)) .findFirst() .orElseThrow(() -> new IllegalArgumentException("Version or alias "+versionOrAlias+" not found")); } @@ -55,4 +55,13 @@ public final ToolDefinitionVersionDescriptor getVersionOrDefault(String versionO return getVersion(versionOrAlias); } + private static final boolean matches(ToolDefinitionVersionDescriptor descriptor, String versionOrAlias) { + var result = descriptor.getVersion().equals(versionOrAlias) + || Arrays.stream(descriptor.getAliases()).anyMatch(versionOrAlias::equals); + if ( !result && versionOrAlias.startsWith("v") ) { + result = matches(descriptor, versionOrAlias.replaceFirst("^v", "")); + } + return result; + } + } \ No newline at end of file diff --git a/fcli-other/fcli-functional-test/src/ftest/groovy/com/fortify/cli/ftest/tool/ToolFoDUploaderSpec.groovy b/fcli-other/fcli-functional-test/src/ftest/groovy/com/fortify/cli/ftest/tool/ToolFoDUploaderSpec.groovy index 56afd0966f..b3fc99e638 100644 --- a/fcli-other/fcli-functional-test/src/ftest/groovy/com/fortify/cli/ftest/tool/ToolFoDUploaderSpec.groovy +++ b/fcli-other/fcli-functional-test/src/ftest/groovy/com/fortify/cli/ftest/tool/ToolFoDUploaderSpec.groovy @@ -110,4 +110,42 @@ class ToolFoDUploaderSpec extends FcliBaseSpec { } } + def "installWithVPrefix"() { + def args = "tool fod-uploader install -y -v=v5.0.0 --progress=none" + when: + def result = Fcli.run(args, {it.expectZeroExitCode()}) + then: + verifyAll(result.stdout) { + size()>0 + it[0].replace(' ', '').equals("NameVersionAliasesStableInstalldirAction") + it[1].contains("5.0.0") + it[1].contains(" INSTALLED") + } + } + + def "installAndUninstallWithVPrefix"() { + def args = "tool fod-uploader install -y -v=v5.0.1 --uninstall=v5.0.0 --progress=none" + when: + def result = Fcli.run(args, {it.expectZeroExitCode()}) + then: + verifyAll(result.stdout) { + size()>0 + it[0].replace(' ', '').equals("NameVersionAliasesStableInstalldirAction") + it[1].contains("5.0.1") + it[1].contains(" INSTALLED") + } + } + + def "uninstallWithVPrefix"() { + def args = "tool fod-uploader uninstall -y -v=v5.0.1 --progress=none" + when: + def result = Fcli.run(args, {it.expectZeroExitCode()}) + then: + verifyAll(result.stdout) { + size()>0 + it[0].replace(' ', '').equals("NameVersionAliasesStableInstalldirAction") + it[1].contains(" UNINSTALLED") + } + } + }