Skip to content

Commit 6d304ed

Browse files
authored
Adds SkaffoldDownloader. (#5)
1 parent 5128571 commit 6d304ed

3 files changed

Lines changed: 183 additions & 0 deletions

File tree

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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 java.io.IOException;
20+
import java.nio.file.Path;
21+
import org.junit.Assert;
22+
import org.junit.Rule;
23+
import org.junit.Test;
24+
import org.junit.rules.TemporaryFolder;
25+
26+
/** Integration tests for {@link SkaffoldDownloader}. */
27+
public class SkaffoldDownloaderIntegrationTest {
28+
29+
@Rule public TemporaryFolder temporaryFolder = new TemporaryFolder();
30+
31+
@Test
32+
public void testDownloadLatest() throws IOException, InterruptedException {
33+
Path temporarySkaffoldExecutable = temporaryFolder.newFile().toPath();
34+
SkaffoldDownloader.downloadLatest(temporarySkaffoldExecutable);
35+
Process skaffoldProcess = new ProcessBuilder(temporarySkaffoldExecutable.toString()).start();
36+
Assert.assertEquals(0, skaffoldProcess.waitFor());
37+
}
38+
}
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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 com.google.cloud.tools.skaffold.filesystem.OperatingSystem;
20+
import org.junit.Assert;
21+
import org.junit.Test;
22+
23+
/** Test for {@link SkaffoldDownloader}. */
24+
public class SkaffoldDownloaderTest {
25+
26+
@Test
27+
public void testGetUrl() {
28+
Assert.assertEquals(
29+
"https://storage.googleapis.com/skaffold/releases/version/skaffold-linux-amd64",
30+
SkaffoldDownloader.getUrl("version", OperatingSystem.LINUX));
31+
Assert.assertEquals(
32+
"https://storage.googleapis.com/skaffold/releases/someversion/skaffold-darwin-amd64",
33+
SkaffoldDownloader.getUrl("someversion", OperatingSystem.MAC_OS));
34+
Assert.assertEquals(
35+
"https://storage.googleapis.com/skaffold/releases/latest/skaffold-windows-amd64.exe",
36+
SkaffoldDownloader.getUrl("latest", OperatingSystem.WINDOWS));
37+
}
38+
}

0 commit comments

Comments
 (0)