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() { async init() {
helmCli.setLogger(logger)
await helmCli.ensureBinary(); await helmCli.ensureBinary();
if (!this.initialized) { if (!this.initialized) {
this.helmEnv = await this.parseHelmEnv() this.helmEnv = await this.parseHelmEnv()

View File

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