mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Expand env vars in cwd.
Signed-off-by: devodev <abalexandrebarone@gmail.com>
This commit is contained in:
parent
8c5457cf89
commit
691fc4f962
@ -22,9 +22,9 @@
|
|||||||
import path from "path";
|
import path from "path";
|
||||||
import os from "os";
|
import os from "os";
|
||||||
|
|
||||||
function resolveTilde(filePath: string) {
|
export function resolveTilde(filePath: string, home?: string) {
|
||||||
if (filePath[0] === "~" && (filePath[1] === "/" || filePath.length === 1)) {
|
if (filePath[0] === "~" && (filePath[1] === "/" || filePath.length === 1)) {
|
||||||
return filePath.replace("~", os.homedir());
|
return filePath.replace("~", home || os.homedir());
|
||||||
}
|
}
|
||||||
|
|
||||||
return filePath;
|
return filePath;
|
||||||
|
|||||||
@ -24,6 +24,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import { clearKubeconfigEnvVars } from "../utils/clear-kube-env-vars";
|
import { clearKubeconfigEnvVars } from "../utils/clear-kube-env-vars";
|
||||||
|
import { resolveEnv } from "../utils/shell-env";
|
||||||
|
|
||||||
describe("clearKubeconfigEnvVars tests", () => {
|
describe("clearKubeconfigEnvVars tests", () => {
|
||||||
it("should not touch non kubeconfig keys", () => {
|
it("should not touch non kubeconfig keys", () => {
|
||||||
@ -38,3 +39,29 @@ describe("clearKubeconfigEnvVars tests", () => {
|
|||||||
expect(clearKubeconfigEnvVars({ a: 1, kubeconfig: "1", kUbeconfig: "1" })).toStrictEqual({ a: 1 });
|
expect(clearKubeconfigEnvVars({ a: 1, kubeconfig: "1", kUbeconfig: "1" })).toStrictEqual({ a: 1 });
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe("resolveEnv tests", () => {
|
||||||
|
const env = {"HOME": "/home/user", "FOO": "foo", "bar": "bar"};
|
||||||
|
|
||||||
|
it("should resolve 1 var", () => {
|
||||||
|
expect(resolveEnv("$HOME", env, env.HOME)).toStrictEqual("/home/user");
|
||||||
|
});
|
||||||
|
it("should resolve multiple vars", () => {
|
||||||
|
expect(resolveEnv("$HOME/$FOO", env, env.HOME)).toStrictEqual("/home/user/foo");
|
||||||
|
});
|
||||||
|
it("should resolve lowercase var", () => {
|
||||||
|
expect(resolveEnv("$bar", env, env.HOME)).toStrictEqual("bar");
|
||||||
|
});
|
||||||
|
it("should resolve ~ as $HOME", () => {
|
||||||
|
expect(resolveEnv("~/foo", env, env.HOME)).toStrictEqual("/home/user/foo");
|
||||||
|
});
|
||||||
|
it("should not resolve missing var", () => {
|
||||||
|
expect(resolveEnv("$BAR", env, env.HOME)).toStrictEqual("$BAR");
|
||||||
|
});
|
||||||
|
it("should not resolve var with brackets", () => {
|
||||||
|
expect(resolveEnv("${HOME}", env, env.HOME)).toStrictEqual("${HOME}");
|
||||||
|
});
|
||||||
|
it("should not resolve invalid var (starts with digit)", () => {
|
||||||
|
expect(resolveEnv("$0HOME", {"0HOME": "/home/user"})).toStrictEqual("$0HOME");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|||||||
@ -23,7 +23,7 @@ import fse from "fs-extra";
|
|||||||
import type { Cluster } from "../cluster";
|
import type { Cluster } from "../cluster";
|
||||||
import { Kubectl } from "../kubectl";
|
import { Kubectl } from "../kubectl";
|
||||||
import type * as WebSocket from "ws";
|
import type * as WebSocket from "ws";
|
||||||
import { shellEnv } from "../utils/shell-env";
|
import { resolveEnv, shellEnv } from "../utils/shell-env";
|
||||||
import { app } from "electron";
|
import { app } from "electron";
|
||||||
import { clearKubeconfigEnvVars } from "../utils/clear-kube-env-vars";
|
import { clearKubeconfigEnvVars } from "../utils/clear-kube-env-vars";
|
||||||
import path from "path";
|
import path from "path";
|
||||||
@ -60,16 +60,22 @@ export abstract class ShellSession {
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected async open(shell: string, args: string[], env: Record<string, any>) {
|
protected async open(shell: string, args: string[], env: Record<string, any>) {
|
||||||
const cwd = (this.cwd && await fse.pathExists(this.cwd))
|
let cwd = env.HOME;
|
||||||
? this.cwd
|
|
||||||
: env.HOME;
|
if (this.cwd) {
|
||||||
|
const resolvedCwd = resolveEnv(this.cwd, env, env.HOME);
|
||||||
|
|
||||||
|
if (await fse.pathExists(resolvedCwd)) {
|
||||||
|
cwd = resolvedCwd;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
this.shellProcess = pty.spawn(shell, args, {
|
this.shellProcess = pty.spawn(shell, args, {
|
||||||
cols: 80,
|
cols: 80,
|
||||||
|
rows: 30,
|
||||||
cwd,
|
cwd,
|
||||||
env,
|
env,
|
||||||
name: "xterm-256color",
|
name: "xterm-256color",
|
||||||
rows: 30,
|
|
||||||
});
|
});
|
||||||
this.running = true;
|
this.running = true;
|
||||||
|
|
||||||
|
|||||||
@ -21,6 +21,7 @@
|
|||||||
|
|
||||||
import shellEnvironment from "shell-env";
|
import shellEnvironment from "shell-env";
|
||||||
import logger from "../logger";
|
import logger from "../logger";
|
||||||
|
import { resolveTilde } from "../../common/utils/paths";
|
||||||
|
|
||||||
export interface EnvironmentVariables {
|
export interface EnvironmentVariables {
|
||||||
readonly [key: string]: string;
|
readonly [key: string]: string;
|
||||||
@ -64,3 +65,13 @@ export async function shellEnv(shell?: string, forceRetry = false) : Promise<Env
|
|||||||
|
|
||||||
return envVars;
|
return envVars;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function resolveEnv(value: string, env: Record<string, any>, home?: string): string {
|
||||||
|
const resolvedValue = value.replace(/\$([a-zA-Z_]+[a-zA-Z0-9_]*)/g, function (match) {
|
||||||
|
const sub = env[match.substring(1)];
|
||||||
|
|
||||||
|
return sub || match;
|
||||||
|
});
|
||||||
|
|
||||||
|
return resolveTilde(resolvedValue, home);
|
||||||
|
}
|
||||||
|
|||||||
@ -67,8 +67,9 @@ export class ClusterHomeDirSetting extends React.Component<Props> {
|
|||||||
placeholder="$HOME"
|
placeholder="$HOME"
|
||||||
/>
|
/>
|
||||||
<small className="hint">
|
<small className="hint">
|
||||||
An explicit start path where the terminal will be launched,{" "}
|
An explicit start path where the terminal will be launched.{" "}
|
||||||
this is used as the current working directory (cwd) for the shell process.
|
This is used as the current working directory (cwd) for the shell process{" "}
|
||||||
|
and supports both ~ and environment variable expansion.
|
||||||
</small>
|
</small>
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user