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

Allow to override logger for LensBinary (#776)

Signed-off-by: Lauri Nevala <lauri.nevala@gmail.com>
This commit is contained in:
Lauri Nevala 2020-09-02 08:08:40 +03:00 committed by GitHub
parent 993b601f74
commit 9c37c69fb4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 11 deletions

View File

@ -46,6 +46,7 @@ export class HelmRepoManager extends Singleton {
}
async init() {
helmCli.setLogger(logger)
await helmCli.ensureBinary();
if (!this.initialized) {
this.helmEnv = await this.parseHelmEnv()

View File

@ -1,10 +1,10 @@
import path from "path"
import fs from "fs"
import request from "request"
import logger from "./logger"
import { ensureDir, pathExists } from "fs-extra"
import * as tar from "tar"
import { isWindows } from "../common/vars";
import winston from "winston"
export type LensBinaryOpts = {
version: string;
@ -27,6 +27,7 @@ export class LensBinary {
protected arch: string
protected originalBinaryName: string
protected requestOpts: request.Options
protected logger: Console | winston.Logger
constructor(opts: LensBinaryOpts) {
const baseDir = opts.baseDir
@ -34,7 +35,7 @@ export class LensBinary {
this.binaryName = opts.newBinaryName || opts.originalBinaryName
this.binaryVersion = opts.version
this.requestOpts = opts.requestOpts
this.logger = console
let arch = null
if (process.arch == "x64") {
@ -59,6 +60,10 @@ export class LensBinary {
}
}
public setLogger(logger: Console | winston.Logger) {
this.logger = logger
}
protected binaryDir() {
throw new Error("binaryDir not implemented")
}
@ -93,7 +98,7 @@ export class LensBinary {
await this.ensureBinary()
return this.dirname
} catch (err) {
logger.error(err)
this.logger.error(err)
return ""
}
}
@ -107,17 +112,17 @@ export class LensBinary {
const isValid = await this.checkBinary()
if (!isValid) {
await this.downloadBinary().catch((error) => {
logger.error(error)
this.logger.error(error)
});
if (this.tarPath) await this.untarBinary()
if (this.originalBinaryName != this.binaryName) await this.renameBinary()
logger.info(`${this.originalBinaryName} has been downloaded to ${this.getBinaryPath()}`)
this.logger.info(`${this.originalBinaryName} has been downloaded to ${this.getBinaryPath()}`)
}
}
protected async untarBinary() {
return new Promise<void>((resolve, reject) => {
logger.debug(`Extracting ${this.originalBinaryName} binary`)
this.logger.debug(`Extracting ${this.originalBinaryName} binary`)
tar.x({
file: this.tarPath,
cwd: this.dirname
@ -129,7 +134,7 @@ export class LensBinary {
protected async renameBinary() {
return new Promise<void>((resolve, reject) => {
logger.debug(`Renaming ${this.originalBinaryName} binary to ${this.binaryName}`)
this.logger.debug(`Renaming ${this.originalBinaryName} binary to ${this.binaryName}`)
fs.rename(this.getOriginalBinaryPath(), this.getBinaryPath(), (err) => {
if (err) {
reject(err)
@ -148,7 +153,7 @@ export class LensBinary {
const file = fs.createWriteStream(binaryPath)
const url = this.getUrl()
logger.info(`Downloading ${this.originalBinaryName} ${this.binaryVersion} from ${url} to ${binaryPath}`)
this.logger.info(`Downloading ${this.originalBinaryName} ${this.binaryVersion} from ${url} to ${binaryPath}`)
const requestOpts: request.UriOptions & request.CoreOptions = {
uri: url,
gzip: true,
@ -158,12 +163,12 @@ export class LensBinary {
const stream = request(requestOpts)
stream.on("complete", () => {
logger.info(`Download of ${this.originalBinaryName} finished`)
this.logger.info(`Download of ${this.originalBinaryName} finished`)
file.end()
})
stream.on("error", (error) => {
logger.error(error)
this.logger.error(error)
fs.unlink(binaryPath, () => {
// do nothing
})
@ -171,7 +176,7 @@ export class LensBinary {
})
return new Promise((resolve, reject) => {
file.on("close", () => {
logger.debug(`${this.originalBinaryName} binary download closed`)
this.logger.debug(`${this.originalBinaryName} binary download closed`)
if (!this.tarPath) fs.chmod(binaryPath, 0o755, (err) => {
if (err) reject(err);
})