Expand env vars in cwd.

Signed-off-by: devodev <abalexandrebarone@gmail.com>
This commit is contained in:
devodev committed 2021-07-16 19:52:28 -04:00
1 parent 8c5457cf89
commit 691fc4f962
5 files changed
+54 -9

No files matched your search

+2 -2
View File
@@ -22,9 +22,9 @@
import path from "path";
import os from "os";
function resolveTilde(filePath: string) {
export function resolveTilde(filePath: string, home?: string) {
if (filePath[0] === "~" && (filePath[1] === "/" || filePath.length === 1)) {
return filePath.replace("~", os.homedir());
return filePath.replace("~", home || os.homedir());
}
return filePath;
+27
View File
@@ -24,6 +24,7 @@
*/
import { clearKubeconfigEnvVars } from "../utils/clear-kube-env-vars";
import { resolveEnv } from "../utils/shell-env";
describe("clearKubeconfigEnvVars tests", () => {
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 });
});
});
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");
});
});
+11 -5
View File
@@ -23,7 +23,7 @@ import fse from "fs-extra";
import type { Cluster } from "../cluster";
import { Kubectl } from "../kubectl";
import type * as WebSocket from "ws";
import { shellEnv } from "../utils/shell-env";
import { resolveEnv, shellEnv } from "../utils/shell-env";
import { app } from "electron";
import { clearKubeconfigEnvVars } from "../utils/clear-kube-env-vars";
import path from "path";
@@ -60,16 +60,22 @@ export abstract class ShellSession {
}
protected async open(shell: string, args: string[], env: Record<string, any>) {
const cwd = (this.cwd && await fse.pathExists(this.cwd))
? this.cwd
: env.HOME;
let 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, {
cols: 80,
rows: 30,
cwd,
env,
name: "xterm-256color",
rows: 30,
});
this.running = true;
+11
View File
@@ -21,6 +21,7 @@
import shellEnvironment from "shell-env";
import logger from "../logger";
import { resolveTilde } from "../../common/utils/paths";
export interface EnvironmentVariables {
readonly [key: string]: string;
@@ -64,3 +65,13 @@ export async function shellEnv(shell?: string, forceRetry = false) : Promise<Env
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"
/>
<small className="hint">
An explicit start path where the terminal will be launched,{" "}
this is used as the current working directory (cwd) for the shell process.
An explicit start path where the terminal will be launched.{" "}
This is used as the current working directory (cwd) for the shell process{" "}
and supports both ~ and environment variable expansion.
</small>
</>
);