use common/utils/singleton class for the stores

Signed-off-by: Roman <ixrock@gmail.com>
This commit is contained in:
Roman committed 2020-07-03 16:57:31 +03:00
1 parent d7cffeb7a8
commit 776fc4462c
14 files changed
+263 -307

No files matched your search

+2 -2
View File
@@ -179,7 +179,7 @@ export class ClusterManager {
clusterStore.reloadCluster(cluster);
if(!cluster.preferences) cluster.preferences = {};
cluster.preferences.icon = clusterIcon
clusterStore.storeCluster(cluster);
clusterStore.saveCluster(cluster);
return {success: true, cluster: cluster.toClusterInfo(), message: ""}
} catch(error) {
return {success: false, message: error}
@@ -191,7 +191,7 @@ export class ClusterManager {
const cluster = this.getCluster(id)
if (cluster && cluster.preferences) {
cluster.preferences.icon = null;
clusterStore.storeCluster(cluster)
clusterStore.saveCluster(cluster)
return {success: true, cluster: cluster.toClusterInfo(), message: ""}
} else {
return {success: false, message: "Cluster not found"}
+1 -1
View File
@@ -156,7 +156,7 @@ export class Cluster implements ClusterInfo {
}
public save() {
clusterStore.storeCluster(this)
clusterStore.saveCluster(this)
}
public toClusterInfo(): ClusterInfo {
+24 -27
View File
@@ -3,9 +3,11 @@ import logger from "./logger";
import * as yaml from "js-yaml";
import { promiseExec } from "./promise-exec";
import { helmCli } from "./helm-cli";
import { Singleton } from "../common/utils/singleton";
type HelmEnv = {
[key: string]: string | undefined;
export type HelmEnv = Record<string, string> & {
HELM_REPOSITORY_CACHE?: string;
HELM_REPOSITORY_CONFIG?: string;
}
export type HelmRepo = {
@@ -14,23 +16,11 @@ export type HelmRepo = {
cacheFilePath?: string;
}
export class HelmRepoManager {
private static instance: HelmRepoManager;
export class HelmRepoManager extends Singleton {
public static cache = {}
protected helmEnv: HelmEnv
protected initialized: boolean
static getInstance(): HelmRepoManager {
if(!HelmRepoManager.instance) {
HelmRepoManager.instance = new HelmRepoManager()
}
return HelmRepoManager.instance;
}
private constructor() {
// use singleton getInstance()
}
public async init() {
const helm = await helmCli.binaryPath()
if (!this.initialized) {
@@ -42,7 +32,9 @@ export class HelmRepoManager {
protected async parseHelmEnv() {
const helm = await helmCli.binaryPath()
const { stdout } = await promiseExec(`"${helm}" env`).catch((error) => { throw(error.stderr)})
const { stdout } = await promiseExec(`"${helm}" env`).catch((error) => {
throw(error.stderr)
})
const lines = stdout.split(/\r?\n/) // split by new line feed
const env: HelmEnv = {}
lines.forEach((line: string) => {
@@ -55,14 +47,14 @@ export class HelmRepoManager {
}
public async repositories(): Promise<Array<HelmRepo>> {
if(!this.initialized) {
if (!this.initialized) {
await this.init()
}
const repositoryFilePath = this.helmEnv.HELM_REPOSITORY_CONFIG
const repoFile = await fs.promises.readFile(repositoryFilePath, 'utf8').catch(async (error) => {
return null
})
if(!repoFile) {
if (!repoFile) {
await this.addRepo({ name: "stable", url: "https://kubernetes-charts.storage.googleapis.com/" })
return await this.repositories()
}
@@ -97,22 +89,23 @@ export class HelmRepoManager {
const helm = await helmCli.binaryPath()
logger.debug(`${helm} repo update`)
const {stdout } = await promiseExec(`"${helm}" repo update`).catch((error) => { return { stdout: error.stdout } })
const { stdout } = await promiseExec(`"${helm}" repo update`).catch((error) => {
return { stdout: error.stdout }
})
return stdout
}
protected async addRepositories(repositories: HelmRepo[]){
protected async addRepositories(repositories: HelmRepo[]) {
const currentRepositories = await this.repositories()
repositories.forEach(async (repo: HelmRepo) => {
try {
const repoExists = currentRepositories.find((currentRepo: HelmRepo) => {
return currentRepo.url == repo.url
})
if(!repoExists) {
if (!repoExists) {
await this.addRepo(repo)
}
}
catch(error) {
} catch (error) {
logger.error(JSON.stringify(error))
}
});
@@ -128,7 +121,7 @@ export class HelmRepoManager {
try {
const output = await this.removeRepo(repo)
logger.debug(output)
} catch(error) {
} catch (error) {
logger.error(error)
}
})
@@ -139,7 +132,9 @@ export class HelmRepoManager {
const helm = await helmCli.binaryPath()
logger.debug(`${helm} repo add ${repository.name} ${repository.url}`)
const {stdout } = await promiseExec(`"${helm}" repo add ${repository.name} ${repository.url}`).catch((error) => { throw(error.stderr)})
const { stdout } = await promiseExec(`"${helm}" repo add ${repository.name} ${repository.url}`).catch((error) => {
throw(error.stderr)
})
return stdout
}
@@ -147,9 +142,11 @@ export class HelmRepoManager {
const helm = await helmCli.binaryPath()
logger.debug(`${helm} repo remove ${repository.name} ${repository.url}`)
const { stdout, stderr } = await promiseExec(`"${helm}" repo remove ${repository.name}`).catch((error) => { throw(error.stderr)})
const { stdout, stderr } = await promiseExec(`"${helm}" repo remove ${repository.name}`).catch((error) => {
throw(error.stderr)
})
return stdout
}
}
export const repoManager = HelmRepoManager.getInstance()
export const repoManager = HelmRepoManager.getInstance<HelmRepoManager>()
+1 -1
View File
@@ -5,8 +5,8 @@ import { app, dialog, protocol } from "electron"
import { isMac, vueAppName, isDevelopment } from "../common/vars";
if (isDevelopment) {
const appName = 'LensDev';
app.setName(appName);
const appData = app.getPath('appData');
app.setName(appName);
app.setPath('userData', path.join(appData, appName));
}
import "../common/prometheus-providers"