Skip to content

Commit b445890

Browse files
authored
Fix bug when downloading CLI (#173)
1 parent ba01bb5 commit b445890

File tree

3 files changed

+71
-40
lines changed

3 files changed

+71
-40
lines changed

src/main/java/com/jfrog/ide/common/configuration/JfrogCliDriver.java

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,24 @@
44
import lombok.Getter;
55
import org.apache.commons.lang3.StringUtils;
66
import org.apache.commons.lang3.SystemUtils;
7-
import org.jfrog.build.client.Version;
87
import org.jfrog.build.api.util.Log;
9-
import org.jfrog.build.extractor.clientConfiguration.ArtifactoryManagerBuilder;
108
import org.jfrog.build.extractor.executor.CommandExecutor;
119
import org.jfrog.build.extractor.executor.CommandResults;
12-
import org.jfrog.build.extractor.clientConfiguration.client.artifactory.ArtifactoryManager;
1310

1411
import java.io.File;
1512
import java.io.IOException;
13+
import java.io.InputStream;
14+
import java.net.URL;
15+
import java.nio.file.Files;
1616
import java.nio.file.Path;
1717
import java.nio.file.Paths;
18+
import java.nio.file.StandardCopyOption;
1819
import java.util.*;
1920
import java.util.stream.Collectors;
2021
import java.util.stream.Stream;
2122
import java.util.regex.Matcher;
2223
import java.util.regex.Pattern;
2324

24-
import static com.jfrog.ide.common.utils.ArtifactoryConnectionUtils.createAnonymousAccessArtifactoryManagerBuilder;
2525
import static com.jfrog.ide.common.utils.Utils.*;
2626

2727
/**
@@ -112,42 +112,44 @@ public CommandResults runCommand(File workingDirectory, Map<String, String> comm
112112
return commandResults;
113113
}
114114

115-
public void downloadCliIfNeeded(String destinationPath, String rawJfrogCliVersion) throws IOException {
116-
Version jfrogCliVersion = new Version(rawJfrogCliVersion);
115+
public void downloadCliIfNeeded(String destinationPath, String jfrogCliVersion) throws IOException {
117116
// verify installed cli version
118-
Version cliVersion = extractVersionFromCliOutput(runVersion(null));
119-
log.debug("Local CLI version is: " + cliVersion);
120-
// cli is installed but not the correct version
121-
if (cliVersion != null && cliVersion.equals(jfrogCliVersion)) {
122-
log.info("Local Jfrog CLI version has been verified and is compatible. Proceeding with its usage.");
117+
if (Files.exists(Paths.get(path, jfrogExec))){
118+
String cliVersion = extractVersionFromCliOutput(runVersion(new File(path)));
119+
log.debug("Local CLI version is: " + cliVersion);
120+
if (jfrogCliVersion.equals(cliVersion)) {
121+
log.info("Local Jfrog CLI version has been verified and is compatible. Proceeding with its usage.");
122+
} else {
123+
log.info(String.format("JFrog CLI version %s is installed, but the required version is %s. " +
124+
"Initiating download of version %s to the destination: %s.", cliVersion, jfrogCliVersion, jfrogCliVersion, destinationPath));
125+
downloadCliFromReleases(jfrogCliVersion, destinationPath);
126+
}
123127
} else {
124-
log.info(String.format("JFrog CLI is either not installed or the current version is incompatible. " +
125-
"Initiating download of version %s to the destination: %s.", jfrogCliVersion, destinationPath));
128+
log.info(String.format("JFrog CLI is not installed. Initiating download of version %s to the destination: %s.", jfrogCliVersion, destinationPath));
126129
downloadCliFromReleases(jfrogCliVersion, destinationPath);
127130
}
128131
}
129132

130-
public void downloadCliFromReleases(Version cliVersion, String destinationFolder) throws IOException {
131-
String[] urlParts = {"jfrog-cli/v2-jf", cliVersion.toString(), "jfrog-cli-" + getOSAndArc(), jfrogExec};
133+
public void downloadCliFromReleases(String cliVersion, String destinationFolder) throws IOException {
134+
String[] urlParts = {"jfrog-cli/v2-jf", cliVersion, "jfrog-cli-" + getOSAndArc(), jfrogExec};
132135
String fileLocationInReleases = String.join("/", urlParts);
133136
Path basePath = Paths.get(destinationFolder);
134137
String destinationPath = basePath.resolve(jfrogExec).toString();
138+
String finalUrl = JFROG_CLI_RELEASES_URL + "/" + fileLocationInReleases;
135139

136140
// download executable from releases and save it in 'destinationPath'
137-
try {
138-
ServerConfig serverConfig = getServerConfig();
139-
ArtifactoryManagerBuilder artifactoryManagerBuilder = createAnonymousAccessArtifactoryManagerBuilder(JFROG_CLI_RELEASES_URL, serverConfig.getProxyConfForTargetUrl(JFROG_CLI_RELEASES_URL), log);
140-
ArtifactoryManager artifactoryManager = artifactoryManagerBuilder.build();
141-
File cliExecutable = artifactoryManager.downloadToFile(fileLocationInReleases, destinationPath);
141+
try (InputStream in = new URL(finalUrl).openStream()){
142+
Files.copy(in, basePath.resolve(jfrogExec), StandardCopyOption.REPLACE_EXISTING);
143+
142144
// setting the file as executable
145+
File cliExecutable = new File(String.valueOf(basePath.resolve(jfrogExec)));
143146
if (!cliExecutable.setExecutable(true)) {
144147
log.error(String.format("Failed to set downloaded CLI as executable. Path: %s", destinationPath));
145148
} else {
146149
log.debug(String.format("Downloaded CLI to %s. Permission te execute: %s", destinationPath, cliExecutable.canExecute()));
147150
}
148151
} catch (IOException e) {
149-
log.error(String.format("Failed to download CLI from %s. Reason: %s", fileLocationInReleases, e.getMessage()), e);
150-
throw e;
152+
throw new IOException(String.format("Failed to download CLI from %s. Reason: %s", fileLocationInReleases, e.getMessage()), e.getCause());
151153
}
152154
}
153155

@@ -195,15 +197,15 @@ public CommandResults runCliAudit(File workingDirectory, List<String> scannedDir
195197
}
196198
}
197199

198-
private Version extractVersionFromCliOutput(String input) {
200+
private String extractVersionFromCliOutput(String input) {
199201
if (input != null) {
200202
// define a pattern for the version format 'x.x.x'
201203
String regex = "\\b\\d+\\.\\d+\\.\\d+\\b";
202204
Pattern pattern = Pattern.compile(regex);
203205
Matcher matcher = pattern.matcher(input);
204206

205207
if (matcher.find()) {
206-
return new Version(matcher.group());
208+
return matcher.group();
207209
}
208210
}
209211

src/main/java/com/jfrog/ide/common/utils/Utils.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ public static String getOSAndArc() throws IOException {
191191
if (StringUtils.equalsAny(arch, "aarch64", "arm64")) {
192192
return "mac-arm64";
193193
} else {
194-
return "mac-amd64";
194+
return "mac-386";
195195
}
196196
}
197197
// Linux
@@ -213,6 +213,8 @@ public static String getOSAndArc() throws IOException {
213213
return "linux-arm";
214214
case "aarch64":
215215
return "linux-arm64";
216+
case "s390x":
217+
return "linux-s390x";
216218
case "ppc64":
217219
case "ppc64le":
218220
return "linux-" + arch;

src/test/java/com/jfrog/ide/common/configuration/JfrogCliDriverTest.java

Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import java.io.File;
1111
import java.io.IOException;
1212
import java.io.InputStream;
13+
import java.lang.reflect.Method;
1314
import java.net.URL;
1415
import java.nio.file.Files;
1516
import java.nio.file.Path;
@@ -27,7 +28,7 @@
2728
* @author tala
2829
*/
2930
public class JfrogCliDriverTest {
30-
31+
private final String TEST_NAME_TO_SKIP_CLI_DOWNLOAD = "testDownloadCliIfNeeded_whenCliIsNotInstalled";
3132
private final SimpleDateFormat timeStampFormat = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss");
3233
private final Map<String, String> testEnv = new HashMap<>();
3334
private JfrogCliDriver jfrogCliDriver;
@@ -55,23 +56,28 @@ private void cliExportTest() {
5556
}
5657

5758
@BeforeMethod
58-
public void setUp(Object[] testArgs) {
59+
public void setUp(Object[] testArgs, Method method) {
5960
try {
60-
configJfrogCli();
61-
testServerId = createServerId();
62-
String[] serverConfigCmdArgs = {"config", "add", testServerId, "--url=" + SERVER_URL, "--interactive=false", "--enc-password=false"};
63-
List<String> credentials = new ArrayList<>(Arrays.asList("--user=" + USER_NAME, "--password=" + PASSWORD));
64-
jfrogCliDriver.runCommand(tempDir, testEnv, serverConfigCmdArgs, Collections.emptyList(), credentials, new NullLog());
61+
boolean skipDownload = TEST_NAME_TO_SKIP_CLI_DOWNLOAD.equals(method.getName());
62+
configJfrogCli(skipDownload);
63+
if(!skipDownload) {
64+
testServerId = createServerId();
65+
String[] serverConfigCmdArgs = {"config", "add", testServerId, "--url=" + SERVER_URL, "--interactive=false", "--enc-password=false"};
66+
List<String> credentials = new ArrayList<>(Arrays.asList("--user=" + USER_NAME, "--password=" + PASSWORD));
67+
jfrogCliDriver.runCommand(tempDir, testEnv, serverConfigCmdArgs, Collections.emptyList(), credentials, new NullLog());
68+
}
6569
} catch (IOException | InterruptedException e) {
6670
fail(e.getMessage(), e);
6771
}
6872
}
6973

70-
private void configJfrogCli() {
74+
private void configJfrogCli(Boolean skipDownload) {
7175
try {
7276
tempDir = Files.createTempDirectory("ide-plugins-common-cli-test").toFile();
7377
tempDir.deleteOnExit();
74-
getCli(tempDir);
78+
if(!skipDownload) {
79+
getCli(tempDir);
80+
}
7581
} catch (IOException | InterruptedException e) {
7682
fail(e.getMessage(), e);
7783
}
@@ -100,10 +106,29 @@ private void getCli(File execDir) throws IOException, InterruptedException {
100106
}
101107

102108

109+
@Test
110+
void testDownloadCliIfNeeded_whenCliIsNotInstalled() throws IOException {
111+
String jfrogCliVersionToDownload = "2.73.0";
112+
String destinationFolder = tempDir.getAbsolutePath();
113+
File destinationFolderFile = new File(destinationFolder);
114+
Path jfrogCliPath = Paths.get(destinationFolder).resolve(jfrogCliDriver.getJfrogExec());
115+
116+
// Verify Jfrog cli executable file does not exist
117+
assertFalse(Files.exists(jfrogCliPath));
118+
119+
jfrogCliDriver.downloadCliIfNeeded(destinationFolder, jfrogCliVersionToDownload);
120+
121+
// Assert the new downloaded cli version is compatible with the required version
122+
String newJfrogCliVersion = jfrogCliDriver.runVersion(destinationFolderFile);
123+
124+
assertNotNull(newJfrogCliVersion);
125+
assertTrue(newJfrogCliVersion.contains(jfrogCliVersionToDownload));
126+
}
127+
103128
@Test
104129
void testDownloadCliIfNeeded_whenCliIsInstalledButIncompatible() throws IOException {
105130
// We use hardcoded version because the setup method downloads the latest cli version which is greater than 2.73.0.
106-
String jfrogCliVersion = "2.73.0";
131+
String jfrogCliVersionToDownload = "2.73.0";
107132
String destinationFolder = tempDir.getAbsolutePath();
108133
File destinationFolderFile = new File(destinationFolder);
109134
Path jfrogCliPath = Paths.get(destinationFolder).resolve(jfrogCliDriver.getJfrogExec());
@@ -112,12 +137,12 @@ void testDownloadCliIfNeeded_whenCliIsInstalledButIncompatible() throws IOExcept
112137
assertTrue(Files.exists(jfrogCliPath));
113138
String currentCliVersion = jfrogCliDriver.runVersion(destinationFolderFile);
114139

115-
jfrogCliDriver.downloadCliIfNeeded(destinationFolder, jfrogCliVersion);
140+
jfrogCliDriver.downloadCliIfNeeded(destinationFolder, jfrogCliVersionToDownload);
116141

117142
// Assert the new downloaded cli version is compatible with the required version
118143
String newJfrogCliVersion = jfrogCliDriver.runVersion(destinationFolderFile);
119144

120-
assertTrue(newJfrogCliVersion.contains(jfrogCliVersion));
145+
assertTrue(newJfrogCliVersion.contains(jfrogCliVersionToDownload));
121146
assertNotEquals(currentCliVersion, newJfrogCliVersion);
122147
}
123148

@@ -201,10 +226,12 @@ private String createServerId() {
201226
}
202227

203228
@AfterMethod
204-
public void cleanUp() {
229+
public void cleanUp(Method method) {
205230
try {
206-
String[] serverConfigCmdArgs = {"config", "remove", testServerId, "--quiet"};
207-
jfrogCliDriver.runCommand(tempDir, testEnv, serverConfigCmdArgs, Collections.emptyList(), null, new NullLog());
231+
if (!TEST_NAME_TO_SKIP_CLI_DOWNLOAD.equals(method.getName())) {
232+
String[] serverConfigCmdArgs = {"config", "remove", testServerId, "--quiet"};
233+
jfrogCliDriver.runCommand(tempDir, testEnv, serverConfigCmdArgs, Collections.emptyList(), null, new NullLog());
234+
}
208235
} catch (IOException | InterruptedException e) {
209236
fail(e.getMessage(), e);
210237
}

0 commit comments

Comments
 (0)