|
| 1 | +/* |
| 2 | + * Copyright 2018 Google LLC. All rights reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 5 | + * use this file except in compliance with the License. You may obtain a copy of |
| 6 | + * the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | + * License for the specific language governing permissions and limitations under |
| 14 | + * the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.cloud.tools.skaffold.downloader; |
| 18 | + |
| 19 | +import static java.nio.file.attribute.PosixFilePermission.GROUP_EXECUTE; |
| 20 | +import static java.nio.file.attribute.PosixFilePermission.OTHERS_EXECUTE; |
| 21 | +import static java.nio.file.attribute.PosixFilePermission.OWNER_EXECUTE; |
| 22 | + |
| 23 | +import com.google.cloud.tools.skaffold.filesystem.OperatingSystem; |
| 24 | +import com.google.common.annotations.VisibleForTesting; |
| 25 | +import java.io.IOException; |
| 26 | +import java.net.MalformedURLException; |
| 27 | +import java.net.URL; |
| 28 | +import java.nio.file.Files; |
| 29 | +import java.nio.file.Path; |
| 30 | +import java.nio.file.attribute.PosixFilePermission; |
| 31 | +import java.util.EnumSet; |
| 32 | +import java.util.Set; |
| 33 | + |
| 34 | +/** |
| 35 | + * Downloads {@code skaffold} executable. |
| 36 | + * |
| 37 | + * @see <a |
| 38 | + * href="https://github.com/GoogleContainerTools/skaffold/blob/master/docs/published-artifacts.adoc">https://github.com/GoogleContainerTools/skaffold/blob/master/docs/published-artifacts.adoc</a> |
| 39 | + */ |
| 40 | +public class SkaffoldDownloader { |
| 41 | + |
| 42 | + /** |
| 43 | + * Downloads the latest {@code skaffold} release. |
| 44 | + * |
| 45 | + * @throws IOException if an I/O exception occurs during download |
| 46 | + * @throws MalformedURLException if the URL to download from is malformed |
| 47 | + */ |
| 48 | + public static void downloadLatest(Path destination) throws IOException { |
| 49 | + // Skaffold publishes the latest release with version "latest". |
| 50 | + download(new URL(getUrl("latest", OperatingSystem.resolve())), destination); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Downloads to the {@code destination}. |
| 55 | + * |
| 56 | + * @param destination the destination file to download {@code skaffold} to |
| 57 | + * @throws IOException if an I/O exception occurs during download |
| 58 | + */ |
| 59 | + @VisibleForTesting |
| 60 | + static void download(URL url, Path destination) throws IOException { |
| 61 | + if (Downloader.download(url, destination) == -1) { |
| 62 | + throw new IOException("Could not get size of skaffold binary to download"); |
| 63 | + } |
| 64 | + |
| 65 | + // Makes skaffold executable. |
| 66 | + try { |
| 67 | + Set<PosixFilePermission> executableFilePermissions = |
| 68 | + Files.getPosixFilePermissions(destination); |
| 69 | + executableFilePermissions.addAll(EnumSet.of(OWNER_EXECUTE, GROUP_EXECUTE, OTHERS_EXECUTE)); |
| 70 | + Files.setPosixFilePermissions(destination, executableFilePermissions); |
| 71 | + |
| 72 | + } catch (UnsupportedOperationException ex) { |
| 73 | + // File system does not support POSIX. |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Resolves the correct URL to download {@code skaffold} from based on the version to download and |
| 79 | + * the operating system. |
| 80 | + * |
| 81 | + * @param version the version to download (use {@code latest} for the latest version) |
| 82 | + * @param operatingSystem the {@link OperatingSystem} |
| 83 | + * @return the URL to download from |
| 84 | + * @see <a |
| 85 | + * href="https://github.com/GoogleContainerTools/skaffold/releases">https://github.com/GoogleContainerTools/skaffold/releases</a> |
| 86 | + */ |
| 87 | + @VisibleForTesting |
| 88 | + static String getUrl(String version, OperatingSystem operatingSystem) { |
| 89 | + String base = "https://storage.googleapis.com/skaffold/releases/" + version + "/"; |
| 90 | + |
| 91 | + switch (operatingSystem) { |
| 92 | + case LINUX: |
| 93 | + return base + "skaffold-linux-amd64"; |
| 94 | + |
| 95 | + case MAC_OS: |
| 96 | + return base + "skaffold-darwin-amd64"; |
| 97 | + |
| 98 | + case WINDOWS: |
| 99 | + return base + "skaffold-windows-amd64.exe"; |
| 100 | + |
| 101 | + default: |
| 102 | + throw new IllegalStateException("unreachable"); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + private SkaffoldDownloader() {} |
| 107 | +} |
0 commit comments