mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-08 05:31:23 +00:00
refactor(infra): share login shell env exec
This commit is contained in:
@@ -11,6 +11,21 @@ function resolveShell(env: NodeJS.ProcessEnv): string {
|
|||||||
return shell && shell.length > 0 ? shell : "/bin/sh";
|
return shell && shell.length > 0 ? shell : "/bin/sh";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function execLoginShellEnvZero(params: {
|
||||||
|
shell: string;
|
||||||
|
env: NodeJS.ProcessEnv;
|
||||||
|
exec: typeof execFileSync;
|
||||||
|
timeoutMs: number;
|
||||||
|
}): Buffer {
|
||||||
|
return params.exec(params.shell, ["-l", "-c", "env -0"], {
|
||||||
|
encoding: "buffer",
|
||||||
|
timeout: params.timeoutMs,
|
||||||
|
maxBuffer: DEFAULT_MAX_BUFFER_BYTES,
|
||||||
|
env: params.env,
|
||||||
|
stdio: ["ignore", "pipe", "pipe"],
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
function parseShellEnv(stdout: Buffer): Map<string, string> {
|
function parseShellEnv(stdout: Buffer): Map<string, string> {
|
||||||
const shellEnv = new Map<string, string>();
|
const shellEnv = new Map<string, string>();
|
||||||
const parts = stdout.toString("utf8").split("\0");
|
const parts = stdout.toString("utf8").split("\0");
|
||||||
@@ -70,13 +85,7 @@ export function loadShellEnvFallback(opts: ShellEnvFallbackOptions): ShellEnvFal
|
|||||||
|
|
||||||
let stdout: Buffer;
|
let stdout: Buffer;
|
||||||
try {
|
try {
|
||||||
stdout = exec(shell, ["-l", "-c", "env -0"], {
|
stdout = execLoginShellEnvZero({ shell, env: opts.env, exec, timeoutMs });
|
||||||
encoding: "buffer",
|
|
||||||
timeout: timeoutMs,
|
|
||||||
maxBuffer: DEFAULT_MAX_BUFFER_BYTES,
|
|
||||||
env: opts.env,
|
|
||||||
stdio: ["ignore", "pipe", "pipe"],
|
|
||||||
});
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
const msg = err instanceof Error ? err.message : String(err);
|
const msg = err instanceof Error ? err.message : String(err);
|
||||||
logger.warn(`[openclaw] shell env fallback failed: ${msg}`);
|
logger.warn(`[openclaw] shell env fallback failed: ${msg}`);
|
||||||
@@ -145,13 +154,7 @@ export function getShellPathFromLoginShell(opts: {
|
|||||||
|
|
||||||
let stdout: Buffer;
|
let stdout: Buffer;
|
||||||
try {
|
try {
|
||||||
stdout = exec(shell, ["-l", "-c", "env -0"], {
|
stdout = execLoginShellEnvZero({ shell, env: opts.env, exec, timeoutMs });
|
||||||
encoding: "buffer",
|
|
||||||
timeout: timeoutMs,
|
|
||||||
maxBuffer: DEFAULT_MAX_BUFFER_BYTES,
|
|
||||||
env: opts.env,
|
|
||||||
stdio: ["ignore", "pipe", "pipe"],
|
|
||||||
});
|
|
||||||
} catch {
|
} catch {
|
||||||
cachedShellPath = null;
|
cachedShellPath = null;
|
||||||
return cachedShellPath;
|
return cachedShellPath;
|
||||||
|
|||||||
Reference in New Issue
Block a user