Skip to content

armgit / codex

Public Imported from GitHub
0

Lightweight coding agent that runs in your terminal

Rust

Extract reusable Bash and Zsh startup scripts (#45749)

jif9899091441d6
diff --git a/codex-rs/shell-command/src/lib.rs b/codex-rs/shell-command/src/lib.rs
index 898965e937..74b7572d83 100644
--- a/codex-rs/shell-command/src/lib.rs
+++ b/codex-rs/shell-command/src/lib.rs
@@ -2,6 +2,9 @@
pub mod shell_detect;
pub mod shell_snapshot;
+mod startup;
+
+pub use startup::shell_startup_script;
pub mod bash;
pub(crate) mod command_safety;
diff --git a/codex-rs/shell-command/src/shell_snapshot_capture.rs b/codex-rs/shell-command/src/shell_snapshot_capture.rs
index 263d196970..80042fd626 100644
--- a/codex-rs/shell-command/src/shell_snapshot_capture.rs
+++ b/codex-rs/shell-command/src/shell_snapshot_capture.rs
@@ -6,6 +6,7 @@ use super::exports;
use super::literals;
use super::posix_env_path_expansion_function;
use crate::shell_detect::ShellType;
+use crate::shell_startup_script;
use std::borrow::Cow;
const SNAPSHOT_COMMAND_HELPER: &str = r#"__codex_snapshot_command() {
@@ -82,17 +83,7 @@ pub fn snapshot_capture_script(
fn zsh_snapshot_script(shell_startup: SnapshotStartup) -> String {
let startup = match shell_startup {
- SnapshotStartup::Interactive => {
- r##"if [[ -n "${ZDOTDIR-}" ]]; then
- rc="$ZDOTDIR/.zshrc"
-elif [[ -n "${HOME-}" ]]; then
- rc="$HOME/.zshrc"
-else
- rc=
-fi
-[[ -r "$rc" ]] && . "$rc"
-"##
- }
+ SnapshotStartup::Interactive => shell_startup_script(ShellType::Zsh),
SnapshotStartup::NonInteractive => "",
};
let script = r##"print '# Snapshot file'
@@ -121,12 +112,7 @@ SNAPSHOT_ENVIRONMENT
fn bash_snapshot_script(shell_startup: SnapshotStartup) -> String {
let startup = match shell_startup {
- SnapshotStartup::Interactive => {
- r##"if [ -z "${BASH_ENV-}" ] && [ -n "${HOME-}" ] && [ -r "$HOME/.bashrc" ]; then
- . "$HOME/.bashrc"
-fi
-"##
- }
+ SnapshotStartup::Interactive => shell_startup_script(ShellType::Bash),
SnapshotStartup::NonInteractive => "",
};
let script = r##"echo '# Snapshot file'
diff --git a/codex-rs/shell-command/src/startup.rs b/codex-rs/shell-command/src/startup.rs
new file mode 100644
index 0000000000..ede02e0512
--- /dev/null
+++ b/codex-rs/shell-command/src/startup.rs
@@ -0,0 +1,30 @@
+//! Profile seeding shared by shell snapshot capture and other initialized shells.
+//! Preserve the existing scripts; callers own the shell's launch flags and lifecycle.
+
+use crate::shell_detect::ShellType;
+
+/// Load the interactive configuration used by snapshot capture in a login shell.
+/// The caller must already launch the shell with its usual login startup flags.
+/// Only Bash and Zsh are supported here; POSIX sh's ENV handling remains in capture.
+pub fn shell_startup_script(shell_type: ShellType) -> &'static str {
+ match shell_type {
+ ShellType::Zsh => {
+ r#"if [[ -n "${ZDOTDIR-}" ]]; then
+ rc="$ZDOTDIR/.zshrc"
+elif [[ -n "${HOME-}" ]]; then
+ rc="$HOME/.zshrc"
+else
+ rc=
+fi
+[[ -r "$rc" ]] && . "$rc"
+"#
+ }
+ ShellType::Bash => {
+ r#"if [ -z "${BASH_ENV-}" ] && [ -n "${HOME-}" ] && [ -r "$HOME/.bashrc" ]; then
+ . "$HOME/.bashrc"
+fi
+"#
+ }
+ ShellType::Sh | ShellType::PowerShell | ShellType::Cmd => "",
+ }
+}