1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

Fix download kubectl

Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
Sebastian Malton 2021-05-27 17:00:37 -04:00
parent 3a031c6953
commit 84849d0755

View File

@ -25,6 +25,7 @@ import md5File from "md5-file";
import requestPromise from "request-promise-native"; import requestPromise from "request-promise-native";
import { ensureDir, pathExists } from "fs-extra"; import { ensureDir, pathExists } from "fs-extra";
import path from "path"; import path from "path";
import { noop } from "../src/common/utils";
class KubectlDownloader { class KubectlDownloader {
public kubectlVersion: string; public kubectlVersion: string;
@ -46,7 +47,7 @@ class KubectlDownloader {
method: "HEAD", method: "HEAD",
uri: this.url, uri: this.url,
resolveWithFullResponse: true resolveWithFullResponse: true
}).catch((error) => { console.log(error); }); }).catch(console.error);
if (response.headers["etag"]) { if (response.headers["etag"]) {
return response.headers["etag"].replace(/"/g, ""); return response.headers["etag"].replace(/"/g, "");
@ -62,7 +63,7 @@ class KubectlDownloader {
const hash = md5File.sync(this.path); const hash = md5File.sync(this.path);
const etag = await this.urlEtag(); const etag = await this.urlEtag();
if(hash == etag) { if (hash == etag) {
console.log("Kubectl md5sum matches the remote etag"); console.log("Kubectl md5sum matches the remote etag");
return true; return true;
@ -76,13 +77,10 @@ class KubectlDownloader {
} }
public async downloadKubectl() { public async downloadKubectl() {
const exists = await this.checkBinary(); if (await this.checkBinary()) {
return console.log("Already exists and is valid");
if(exists) {
console.log("Already exists and is valid");
return;
} }
await ensureDir(path.dirname(this.path), 0o755); await ensureDir(path.dirname(this.path), 0o755);
const file = fs.createWriteStream(this.path); const file = fs.createWriteStream(this.path);
@ -96,18 +94,16 @@ class KubectlDownloader {
stream.on("complete", () => { stream.on("complete", () => {
console.log("kubectl binary download finished"); console.log("kubectl binary download finished");
// eslint-disable-next-line @typescript-eslint/no-empty-function file.end(noop);
file.end(() => {});
}); });
stream.on("error", (error) => { stream.on("error", (error) => {
console.log(error); console.log(error);
// eslint-disable-next-line @typescript-eslint/no-empty-function fs.unlink(this.path, noop);
fs.unlink(this.path, () => {}); throw error;
throw(error);
}); });
return new Promise((resolve, reject) => { return new Promise<void>((resolve, reject) => {
file.on("close", () => { file.on("close", () => {
console.log("kubectl binary download closed"); console.log("kubectl binary download closed");
fs.chmod(this.path, 0o755, (err) => { fs.chmod(this.path, 0o755, (err) => {
@ -136,4 +132,3 @@ downloads.forEach((dlOpts) => {
console.log(`Downloading: ${JSON.stringify(dlOpts)}`); console.log(`Downloading: ${JSON.stringify(dlOpts)}`);
downloader.downloadKubectl().then(() => downloader.checkBinary().then(() => console.log("Download complete"))); downloader.downloadKubectl().then(() => downloader.checkBinary().then(() => console.log("Download complete")));
}); });