From 9c37c69fb418f160c4964534137a0d91da3097cd Mon Sep 17 00:00:00 2001 From: Lauri Nevala Date: Wed, 2 Sep 2020 08:08:40 +0300 Subject: [PATCH] Allow to override logger for LensBinary (#776) Signed-off-by: Lauri Nevala --- src/main/helm/helm-repo-manager.ts | 1 + src/main/lens-binary.ts | 27 ++++++++++++++++----------- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/src/main/helm/helm-repo-manager.ts b/src/main/helm/helm-repo-manager.ts index 2c132311c3..3bbec7841f 100644 --- a/src/main/helm/helm-repo-manager.ts +++ b/src/main/helm/helm-repo-manager.ts @@ -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() diff --git a/src/main/lens-binary.ts b/src/main/lens-binary.ts index 0b0a9e1933..dd6f2aa058 100644 --- a/src/main/lens-binary.ts +++ b/src/main/lens-binary.ts @@ -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((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((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); })