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

Clean up electron-store based stores

- Add some type script types for better editor experience
- Remove redundent global of a singleton

Signed-off-by: Sebastian Malton <smalton@mirantis.com>
This commit is contained in:
Sebastian Malton 2020-06-23 12:09:17 -04:00
parent cd66a985cc
commit 97645f4327
17 changed files with 87 additions and 107 deletions

View File

@ -197,7 +197,7 @@ describe("for a pre 2.0 config with an existing cluster", () => {
it("migrates to modern format with kubeconfig under a key", async () => {
const clusterStore = ClusterStore.getInstance()
const storedCluster = clusterStore.store.get('clusters')[0]
const storedCluster = clusterStore.getAllClusters()[0]
expect(storedCluster.kubeConfig).toBe('kubeconfig content')
})
})
@ -231,7 +231,7 @@ describe("for a pre 2.4.1 config with an existing cluster", () => {
it("migrates to modern format throwing out the state related data", async () => {
const clusterStore = ClusterStore.getInstance()
const storedClusterData = clusterStore.store.get('clusters')[0]
const storedClusterData = clusterStore.getAllClusters()[0]
expect(storedClusterData.hasOwnProperty('online')).toBe(false)
expect(storedClusterData.hasOwnProperty('accessible')).toBe(false)
expect(storedClusterData.hasOwnProperty('failureReason')).toBe(false)
@ -264,7 +264,7 @@ describe("for a pre 2.6.0 config with a cluster that has arrays in auth config",
it("replaces array format access token and expiry into string", async () => {
const clusterStore = ClusterStore.getInstance()
const storedClusterData = clusterStore.store.get('clusters')[0]
const storedClusterData = clusterStore.getAllClusters()[0]
const kc = yaml.safeLoad(storedClusterData.kubeConfig)
expect(kc.users[0].user['auth-provider'].config['access-token']).toBe("should be string")
expect(kc.users[0].user['auth-provider'].config['expiry']).toBe("should be string")
@ -301,7 +301,7 @@ describe("for a pre 2.6.0 config with a cluster icon", () => {
it("moves the icon into preferences", async () => {
const clusterStore = ClusterStore.getInstance()
const storedClusterData = clusterStore.store.get('clusters')[0]
const storedClusterData = clusterStore.getAllClusters()[0]
expect(storedClusterData.hasOwnProperty('icon')).toBe(false)
expect(storedClusterData.preferences.hasOwnProperty('icon')).toBe(true)
expect(storedClusterData.preferences.icon).toBe("icon path")
@ -338,7 +338,7 @@ describe("for a pre 2.7.0-beta.0 config without a workspace", () => {
it("adds cluster to default workspace", async () => {
const clusterStore = ClusterStore.getInstance()
const storedClusterData = clusterStore.store.get("clusters")[0]
const storedClusterData = clusterStore.getAllClusters()[0]
expect(storedClusterData.workspace).toBe('default')
})
})

View File

@ -14,7 +14,7 @@ jest.mock("electron", () => {
// Console.log needs to be called before fs-mocks, see https://github.com/tschaub/mock-fs/issues/234
console.log("");
import { userStore, User, UserPreferences, UserStore } from "../../../src/common/user-store"
import { UserStore } from "../../../src/common/user-store"
describe("for an empty config", () => {
beforeEach(() => {
@ -25,7 +25,6 @@ describe("for an empty config", () => {
}
}
mockFs(mockOpts)
const userStore = UserStore.getInstance()
})
afterEach(() => {
@ -33,31 +32,31 @@ describe("for an empty config", () => {
})
it("allows setting and retrieving lastSeenAppVersion", async () => {
userStore.setLastSeenAppVersion("1.2.3");
expect(userStore.lastSeenAppVersion()).toBe("1.2.3");
UserStore.getInstance().setLastSeenAppVersion("1.2.3");
expect(UserStore.getInstance().lastSeenAppVersion()).toBe("1.2.3");
})
it("allows adding and listing seen contexts", async () => {
userStore.storeSeenContext(['foo'])
expect(userStore.getSeenContexts().length).toBe(1)
userStore.storeSeenContext(['foo', 'bar'])
const seenContexts = userStore.getSeenContexts()
UserStore.getInstance().storeSeenContext(['foo'])
expect(UserStore.getInstance().getSeenContexts().length).toBe(1)
UserStore.getInstance().storeSeenContext(['foo', 'bar'])
const seenContexts = UserStore.getInstance().getSeenContexts()
expect(seenContexts.length).toBe(2) // check 'foo' isn't added twice
expect(seenContexts[0]).toBe('foo')
expect(seenContexts[1]).toBe('bar')
})
it("allows setting and getting preferences", async () => {
userStore.setPreferences({
UserStore.getInstance().setPreferences({
httpsProxy: 'abcd://defg',
})
const storedPreferences = userStore.getPreferences()
const storedPreferences = UserStore.getInstance().getPreferences()
expect(storedPreferences.httpsProxy).toBe('abcd://defg')
expect(storedPreferences.colorTheme).toBe('dark') // defaults to dark
userStore.setPreferences({
UserStore.getInstance().setPreferences({
colorTheme: 'light'
})
expect(userStore.getPreferences().colorTheme).toBe('light')
expect(UserStore.getInstance().getPreferences().colorTheme).toBe('light')
})
})
@ -74,7 +73,6 @@ describe("migrations", () => {
}
}
mockFs(mockOpts)
const userStore = UserStore.getInstance()
})
afterEach(() => {
@ -82,6 +80,6 @@ describe("migrations", () => {
})
it("sets last seen app version to 0.0.0", async () => {
expect(userStore.lastSeenAppVersion()).toBe('0.0.0')
expect(UserStore.getInstance().lastSeenAppVersion()).toBe('0.0.0')
})
})

View File

@ -1,9 +0,0 @@
const userStore = {
getPreferences: jest.fn(() => {
return {
downloadMirror: "default"
}
})
}
export { userStore };

View File

@ -10,7 +10,7 @@ import * as version270Beta1 from "./migrations/cluster-store/2.7.0-beta.1"
export class ClusterStore {
private static instance: ClusterStore;
public store: ElectronStore;
private store: ElectronStore;
private constructor() {
this.store = new ElectronStore({
@ -107,7 +107,3 @@ export class ClusterStore {
ClusterStore.instance = null
}
}
const clusterStore: ClusterStore = ClusterStore.getInstance();
export { clusterStore };

View File

@ -1,8 +1,8 @@
import * as request from "request"
import { userStore } from "../common/user-store"
import { UserStore } from "../common/user-store"
export function globalRequestOpts(requestOpts: request.Options ) {
const userPrefs = userStore.getPreferences()
const userPrefs = UserStore.getInstance().getPreferences()
if (userPrefs.httpsProxy) {
requestOpts.proxy = userPrefs.httpsProxy
}

View File

@ -1,5 +1,5 @@
import { machineIdSync } from 'node-machine-id'
import { userStore } from "../common/user-store"
import { UserStore } from "../common/user-store"
import * as ua from "universal-analytics"
const GA_ID = "UA-159377374-1"
@ -36,7 +36,7 @@ export class Tracker {
}
protected telemetryAllowed() {
const userPrefs = userStore.getPreferences()
const userPrefs = UserStore.getInstance().getPreferences()
return !!userPrefs.allowTelemetry
}
}

View File

@ -16,7 +16,7 @@ export interface UserPreferences {
export class UserStore {
private static instance: UserStore;
public store: ElectronStore;
private store: ElectronStore;
private constructor() {
this.store = new ElectronStore({
@ -78,7 +78,3 @@ export class UserStore {
UserStore.instance = null
}
}
const userStore: UserStore = UserStore.getInstance();
export { userStore };

View File

@ -1,5 +1,5 @@
import * as ElectronStore from "electron-store"
import { clusterStore } from "./cluster-store"
import { ClusterStore } from "./cluster-store"
export interface WorkspaceData {
id: string;
@ -7,6 +7,10 @@ export interface WorkspaceData {
description?: string;
}
interface WorkspaceStoreData {
workspaces: WorkspaceData[];
}
export class Workspace implements WorkspaceData {
public id: string
public name: string
@ -20,14 +24,21 @@ export class Workspace implements WorkspaceData {
export class WorkspaceStore {
public static defaultId = "default"
private static instance: WorkspaceStore;
public store: ElectronStore;
private store: ElectronStore<WorkspaceStoreData>;
private constructor() {
this.store = new ElectronStore({
name: "lens-workspace-store"
name: "lens-workspace-store",
})
}
if (this.store.get("workspaces", []).length === 0) {
this.store.set("workspaces", [{
id: "default",
name: "default"
}]);
}
}
public storeWorkspace(workspace: WorkspaceData) {
const workspaces = this.getAllWorkspaces()
const index = workspaces.findIndex((w) => w.id === workspace.id)
@ -46,12 +57,16 @@ export class WorkspaceStore {
const workspaces = this.getAllWorkspaces()
const index = workspaces.findIndex((w) => w.id === workspace.id)
if (index !== -1) {
clusterStore.removeClustersByWorkspace(workspace.id)
ClusterStore.getInstance().removeClustersByWorkspace(workspace.id)
workspaces.splice(index, 1)
this.store.set("workspaces", workspaces)
}
}
public getWorkspace(id: string): Workspace | null {
return this.store.get("workspaces", []).find(wsd => wsd.id == id) || null;
}
public getAllWorkspaces(): Array<Workspace> {
const workspacesData: WorkspaceData[] = this.store.get("workspaces", [])

View File

@ -2,7 +2,7 @@ import { KubeConfig } from "@kubernetes/client-node"
import { PromiseIpc } from "electron-promise-ipc"
import * as http from "http"
import { Cluster, ClusterBaseInfo } from "./cluster"
import { clusterStore } from "../common/cluster-store"
import { ClusterStore } from "../common/cluster-store"
import * as k8s from "./k8s"
import logger from "./logger"
import { LensProxy } from "./proxy"
@ -177,10 +177,10 @@ export class ClusterManager {
}
try {
const clusterIcon = await this.uploadClusterIcon(cluster, fileUpload.name, fileUpload.path)
clusterStore.reloadCluster(cluster);
ClusterStore.getInstance().reloadCluster(cluster);
if(!cluster.preferences) cluster.preferences = {};
cluster.preferences.icon = clusterIcon
clusterStore.storeCluster(cluster);
ClusterStore.getInstance().storeCluster(cluster);
return {success: true, cluster: this.clusterResponse(cluster), message: ""}
} catch(error) {
return {success: false, message: error}
@ -192,7 +192,7 @@ export class ClusterManager {
const cluster = this.getCluster(id)
if (cluster && cluster.preferences) {
cluster.preferences.icon = null;
clusterStore.storeCluster(cluster)
ClusterStore.getInstance().storeCluster(cluster)
return {success: true, cluster: this.clusterResponse(cluster), message: ""}
} else {
return {success: false, message: "Cluster not found"}
@ -224,7 +224,7 @@ export class ClusterManager {
logger.debug(`IPC: clusterStored: ${clusterId}`)
const cluster = this.clusters.get(clusterId)
if (cluster) {
clusterStore.reloadCluster(cluster);
ClusterStore.getInstance().reloadCluster(cluster);
cluster.stopServer()
}
});
@ -247,7 +247,7 @@ export class ClusterManager {
const cluster = this.clusters.get(id)
if (cluster) {
cluster.stopServer()
clusterStore.removeCluster(cluster.id);
ClusterStore.getInstance().removeCluster(cluster.id);
this.clusters.delete(cluster.id)
}
return Array.from(this.clusters.values())

View File

@ -1,7 +1,7 @@
import { ContextHandler } from "./context-handler"
import { FeatureStatusMap } from "./feature"
import * as k8s from "./k8s"
import { clusterStore } from "../common/cluster-store"
import { ClusterStore } from "../common/cluster-store"
import logger from "./logger"
import { KubeConfig, CoreV1Api, AuthorizationV1Api, V1ResourceAttributes } from "@kubernetes/client-node"
import * as fm from "./feature-manager";
@ -119,20 +119,13 @@ export class Cluster implements ClusterInfo {
}
public async refreshCluster() {
clusterStore.reloadCluster(this)
ClusterStore.getInstance().reloadCluster(this)
this.contextHandler.setClusterPreferences(this.preferences)
const connectionStatus = await this.getConnectionStatus()
if (connectionStatus == ClusterStatus.AccessGranted) {
this.accessible = true
} else {
this.accessible = false
}
if (connectionStatus > ClusterStatus.Offline) {
this.online = true
} else {
this.online = false
}
this.accessible = connectionStatus == ClusterStatus.AccessGranted;
this.online = connectionStatus > ClusterStatus.Offline;
if (this.accessible) {
this.distribution = this.detectKubernetesDistribution(this.version)
this.features = await fm.getFeatures(this.contextHandler)
@ -145,7 +138,7 @@ export class Cluster implements ClusterInfo {
}
public updateKubeconfig(kubeconfig: string) {
const storedCluster = clusterStore.getCluster(this.id)
const storedCluster = ClusterStore.getInstance().getCluster(this.id)
if (!storedCluster) { return }
this.kubeConfig = kubeconfig
@ -153,18 +146,15 @@ export class Cluster implements ClusterInfo {
}
public getPrometheusApiPrefix() {
if (!this.preferences.prometheus?.prefix) {
return ""
}
return this.preferences.prometheus.prefix
return this.preferences.prometheus?.prefix || "";
}
public save() {
clusterStore.storeCluster(this)
ClusterStore.getInstance().storeCluster(this)
}
public toClusterInfo(): ClusterInfo {
const clusterInfo: ClusterInfo = {
return {
id: this.id,
workspace: this.workspace,
url: this.url,
@ -181,8 +171,7 @@ export class Cluster implements ClusterInfo {
kubeCtl: this.kubeCtl,
kubeConfig: this.kubeConfig,
preferences: this.preferences
}
return clusterInfo;
};
}
protected async k8sRequest(path: string, opts?: request.RequestPromiseOptions) {

View File

@ -7,7 +7,7 @@ import logger from "./logger"
import initMenu from "./menu"
import * as proxy from "./proxy"
import { WindowManager } from "./window-manager";
import { clusterStore } from "../common/cluster-store"
import { ClusterStore } from "../common/cluster-store"
import { tracker } from "./tracker"
import { ClusterManager } from "./cluster-manager";
import AppUpdater from "./app-updater"
@ -57,7 +57,7 @@ async function main() {
}
// create cluster manager
clusterManager = new ClusterManager(clusterStore.getAllClusterObjects(), port)
clusterManager = new ClusterManager(ClusterStore.getInstance().getAllClusterObjects(), port)
// run proxy
try {
proxyServer = proxy.listen(port, clusterManager)

View File

@ -9,7 +9,7 @@ import * as md5File from "md5-file"
import { globalRequestOpts } from "../common/request"
import * as lockFile from "proper-lockfile"
import { helmCli } from "./helm-cli"
import { userStore } from "../common/user-store"
import { UserStore } from "../common/user-store"
const bundledVersion = require("../../package.json").config.bundledKubectlVersion
const kubectlMap: Map<string, string> = new Map([
@ -138,7 +138,7 @@ export class Kubectl {
logger.error(`Local kubectl is version ${version}, expected ${this.kubectlVersion}, unlinking`)
}
catch(err) {
logger.error(`Local kubectl failed to run properly (${err.message}), unlinking`)
logger.error(`Local kubectl failed to run properly (${err.message}), unlinking`)
}
await fs.promises.unlink(this.path)
}
@ -288,7 +288,7 @@ export class Kubectl {
if (process.platform == "darwin") {
return packageMirrors.get("default") // MacOS packages are only available from default
}
const mirror = packageMirrors.get(userStore.getPreferences().downloadMirror)
const mirror = packageMirrors.get(UserStore.getInstance().getPreferences().downloadMirror)
if (mirror) { return mirror }
return packageMirrors.get("default")

View File

@ -1,6 +1,6 @@
import { LensApiRequest } from "../router"
import { LensApi } from "../lens-api"
import { userStore } from "../../common/user-store"
import { UserStore } from "../../common/user-store"
import { getAppVersion } from "../../common/app-utils"
import { CoreV1Api, AuthorizationV1Api } from "@kubernetes/client-node"
import { Cluster } from "../cluster"
@ -81,7 +81,7 @@ class ConfigRoute extends LensApi {
const data = {
clusterName: cluster.contextName,
lensVersion: getAppVersion(),
lensTheme: `kontena-${userStore.getPreferences().colorTheme}`,
lensTheme: `kontena-${UserStore.getInstance().getPreferences().colorTheme}`,
kubeVersion: cluster.version,
chartsEnabled: true,
isClusterAdmin: cluster.isAdmin,

View File

@ -22,7 +22,7 @@
import ClusterMenuItem from "@/components/MainMenu/ClusterMenuItem";
import AddClusterMenuItem from "@/components/MainMenu/AddClusterMenuItem";
import draggable from 'vuedraggable'
import { clusterStore } from "@/../common/cluster-store";
import { ClusterStore } from "@/../common/cluster-store";
import * as os from "os";
const { remote } = require('electron')
@ -50,7 +50,7 @@ export default {
},
set: function(clusters) {
this.$store.commit("updateClusters", clusters);
clusterStore.storeClusters(clusters);
ClusterStore.getInstance().storeClusters(clusters);
}
}
},

View File

@ -1,6 +1,6 @@
import Vue from 'vue'
import Vuex from 'vuex'
import { userStore } from "../../common/user-store"
import { UserStore } from "../../common/user-store"
import KubeContexts from './modules/kube-contexts'
import Clusters from './modules/clusters'
import HelmRepos from './modules/helm-repos'
@ -31,29 +31,24 @@ export default new Vuex.Store({
hud: {
isMenuVisible: true,
},
seenContexts: userStore.getSeenContexts(),
lastSeenAppVersion: userStore.lastSeenAppVersion(),
seenContexts: UserStore.getInstance().getSeenContexts(),
lastSeenAppVersion: UserStore.getInstance().lastSeenAppVersion(),
},
mutations: {
storeSeenContexts(state, context) {
const seenContexts = userStore.storeSeenContext(context);
state.seenContexts = seenContexts
state.seenContexts = UserStore.getInstance().storeSeenContext(context)
},
updateLastSeenAppVersion(state, appVersion) {
state.lastSeenAppVersion = appVersion;
userStore.setLastSeenAppVersion(appVersion)
UserStore.getInstance().setLastSeenAppVersion(appVersion)
},
loadPreferences(state) {
this.commit("savePreferences", userStore.getPreferences());
this.commit("savePreferences", UserStore.getInstance().getPreferences());
},
savePreferences(state, prefs) {
if (prefs.allowTelemetry) {
tracker.event("telemetry", "enabled")
} else {
tracker.event("telemetry", "disabled")
}
tracker.event("telemetry", prefs.allowTelemetry ? "enabled" : "disabled")
state.preferences = prefs;
userStore.setPreferences(prefs);
UserStore.getInstance().setPreferences(prefs);
this.dispatch("destroyWebviews")
promiseIpc.send("preferencesSaved")
},

View File

@ -4,7 +4,7 @@ import { MutationTree, ActionTree, GetterTree } from "vuex"
import { PromiseIpc } from 'electron-promise-ipc'
import { Tracker } from "../../../common/tracker"
import { remote } from "electron"
import { clusterStore } from "../../../common/cluster-store"
import { ClusterStore } from "../../../common/cluster-store"
import { Workspace } from "../../../common/workspace-store"
const promiseIpc = new PromiseIpc( { maxTimeoutMs: 120000 } );
@ -217,7 +217,7 @@ const actions: ActionTree<ClusterState, any> = {
})
},
storeCluster({commit}, cluster: ClusterInfo) {
clusterStore.storeCluster(cluster);
ClusterStore.getInstance().storeCluster(cluster);
commit("updateCluster", cluster)
promiseIpc.send("clusterStored", cluster.id)
}

View File

@ -1,5 +1,5 @@
import { MutationTree, ActionTree, GetterTree } from "vuex"
import { workspaceStore, Workspace, WorkspaceData } from "../../../common/workspace-store"
import { WorkspaceStore, Workspace, WorkspaceData } from "../../../common/workspace-store"
export interface WorkspaceState {
workspaces: Array<Workspace>;
@ -7,8 +7,8 @@ export interface WorkspaceState {
}
const state: WorkspaceState = {
workspaces: workspaceStore.getAllWorkspaces(),
currentWorkspace: workspaceStore.getAllWorkspaces().find((w) => w.id === "default")
workspaces: WorkspaceStore.getInstance().getAllWorkspaces(),
currentWorkspace: WorkspaceStore.getInstance().getAllWorkspaces().find((w) => w.id === "default"),
}
const actions: ActionTree<WorkspaceState, any> = {
@ -27,16 +27,16 @@ const mutations: MutationTree<WorkspaceState> = {
state.currentWorkspace = workspace
},
addWorkspace(state, workspace: WorkspaceData) {
workspaceStore.storeWorkspace(workspace)
state.workspaces = workspaceStore.getAllWorkspaces()
WorkspaceStore.getInstance().storeWorkspace(workspace)
state.workspaces = WorkspaceStore.getInstance().getAllWorkspaces()
},
updateWorkspace(state, workspace: WorkspaceData) {
workspaceStore.storeWorkspace(workspace)
state.workspaces = workspaceStore.getAllWorkspaces()
WorkspaceStore.getInstance().storeWorkspace(workspace)
state.workspaces = WorkspaceStore.getInstance().getAllWorkspaces()
},
removeWorkspace(state, workspace: Workspace) {
workspaceStore.removeWorkspace(workspace)
state.workspaces = workspaceStore.getAllWorkspaces()
WorkspaceStore.getInstance().removeWorkspace(workspace)
state.workspaces = WorkspaceStore.getInstance().getAllWorkspaces()
}
}