|
4 | 4 | import lombok.Getter; |
5 | 5 | import org.apache.commons.lang3.StringUtils; |
6 | 6 | import org.apache.commons.lang3.SystemUtils; |
7 | | -import org.jfrog.build.client.Version; |
8 | 7 | import org.jfrog.build.api.util.Log; |
9 | | -import org.jfrog.build.extractor.clientConfiguration.ArtifactoryManagerBuilder; |
10 | 8 | import org.jfrog.build.extractor.executor.CommandExecutor; |
11 | 9 | import org.jfrog.build.extractor.executor.CommandResults; |
12 | | -import org.jfrog.build.extractor.clientConfiguration.client.artifactory.ArtifactoryManager; |
13 | 10 |
|
14 | 11 | import java.io.File; |
15 | 12 | import java.io.IOException; |
| 13 | +import java.io.InputStream; |
| 14 | +import java.net.URL; |
| 15 | +import java.nio.file.Files; |
16 | 16 | import java.nio.file.Path; |
17 | 17 | import java.nio.file.Paths; |
| 18 | +import java.nio.file.StandardCopyOption; |
18 | 19 | import java.util.*; |
19 | 20 | import java.util.stream.Collectors; |
20 | 21 | import java.util.stream.Stream; |
21 | 22 | import java.util.regex.Matcher; |
22 | 23 | import java.util.regex.Pattern; |
23 | 24 |
|
24 | | -import static com.jfrog.ide.common.utils.ArtifactoryConnectionUtils.createAnonymousAccessArtifactoryManagerBuilder; |
25 | 25 | import static com.jfrog.ide.common.utils.Utils.*; |
26 | 26 |
|
27 | 27 | /** |
@@ -112,42 +112,44 @@ public CommandResults runCommand(File workingDirectory, Map<String, String> comm |
112 | 112 | return commandResults; |
113 | 113 | } |
114 | 114 |
|
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 { |
117 | 116 | // 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 | + } |
123 | 127 | } 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)); |
126 | 129 | downloadCliFromReleases(jfrogCliVersion, destinationPath); |
127 | 130 | } |
128 | 131 | } |
129 | 132 |
|
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}; |
132 | 135 | String fileLocationInReleases = String.join("/", urlParts); |
133 | 136 | Path basePath = Paths.get(destinationFolder); |
134 | 137 | String destinationPath = basePath.resolve(jfrogExec).toString(); |
| 138 | + String finalUrl = JFROG_CLI_RELEASES_URL + "/" + fileLocationInReleases; |
135 | 139 |
|
136 | 140 | // 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 | + |
142 | 144 | // setting the file as executable |
| 145 | + File cliExecutable = new File(String.valueOf(basePath.resolve(jfrogExec))); |
143 | 146 | if (!cliExecutable.setExecutable(true)) { |
144 | 147 | log.error(String.format("Failed to set downloaded CLI as executable. Path: %s", destinationPath)); |
145 | 148 | } else { |
146 | 149 | log.debug(String.format("Downloaded CLI to %s. Permission te execute: %s", destinationPath, cliExecutable.canExecute())); |
147 | 150 | } |
148 | 151 | } 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()); |
151 | 153 | } |
152 | 154 | } |
153 | 155 |
|
@@ -195,15 +197,15 @@ public CommandResults runCliAudit(File workingDirectory, List<String> scannedDir |
195 | 197 | } |
196 | 198 | } |
197 | 199 |
|
198 | | - private Version extractVersionFromCliOutput(String input) { |
| 200 | + private String extractVersionFromCliOutput(String input) { |
199 | 201 | if (input != null) { |
200 | 202 | // define a pattern for the version format 'x.x.x' |
201 | 203 | String regex = "\\b\\d+\\.\\d+\\.\\d+\\b"; |
202 | 204 | Pattern pattern = Pattern.compile(regex); |
203 | 205 | Matcher matcher = pattern.matcher(input); |
204 | 206 |
|
205 | 207 | if (matcher.find()) { |
206 | | - return new Version(matcher.group()); |
| 208 | + return matcher.group(); |
207 | 209 | } |
208 | 210 | } |
209 | 211 |
|
|
0 commit comments