Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Cache terminall shell env #274

Merged
merged 2 commits into from
Apr 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/main/node-shell-session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export class NodeShellSession extends ShellSession {
}
args = ["exec", "-i", "-t", "-n", "kube-system", this.podId, "--", "sh", "-c", "((clear && bash) || (clear && ash) || (clear && sh))"]

const shellEnv = this.getShellEnv()
const shellEnv = await this.getCachedShellEnv()
this.shellProcess = pty.spawn(shell, args, {
cols: 80,
cwd: this.cwd() || shellEnv["HOME"],
Expand Down
26 changes: 22 additions & 4 deletions src/main/shell-session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import { Cluster, ClusterPreferences } from "./cluster"
import { helmCli } from "./helm-cli"

export class ShellSession extends EventEmitter {
static shellEnv: any

protected websocket: WebSocket
protected shellProcess: pty.IPty
protected kubeconfigPath: string
Expand All @@ -30,8 +32,7 @@ export class ShellSession extends EventEmitter {
public async open() {
this.kubectlBinDir = await this.kubectl.binDir()
this.helmBinDir = helmCli.getBinaryDir()
await helmCli.binaryPath()
const env = this.getShellEnv()
const env = await this.getCachedShellEnv()
const shell = env.PTYSHELL
const args = await this.getShellArgs(shell)
this.shellProcess = pty.spawn(shell, args, {
Expand Down Expand Up @@ -75,8 +76,25 @@ export class ShellSession extends EventEmitter {
}
}

protected getShellEnv() {
const env = JSON.parse(JSON.stringify(shellEnv.sync()))
protected async getCachedShellEnv() {
let env: any
if (!ShellSession.shellEnv) {
env = await this.getShellEnv()
ShellSession.shellEnv = env
} else {
env = ShellSession.shellEnv

// refresh env in the background
this.getShellEnv().then((shellEnv: any) => {
ShellSession.shellEnv = shellEnv
})
}

return env
}

protected async getShellEnv() {
const env = JSON.parse(JSON.stringify(await shellEnv()))
const pathStr = [this.kubectlBinDir, this.helmBinDir, process.env.PATH].join(path.delimiter)

if(process.platform === "win32") {
Expand Down