From b877d8e4f60e3d37d2c9c7d93f37e778c8295296 Mon Sep 17 00:00:00 2001 From: Daniel Edrisian Date: Tue, 14 Oct 2025 14:17:49 -0700 Subject: [PATCH] Refactor --- codex-rs/cli/src/main.rs | 25 ++++++------------------- codex-rs/tui/src/lib.rs | 34 ++++++++++++++++++++++------------ 2 files changed, 28 insertions(+), 31 deletions(-) diff --git a/codex-rs/cli/src/main.rs b/codex-rs/cli/src/main.rs index c55b96b4df..7e9f24afdd 100644 --- a/codex-rs/cli/src/main.rs +++ b/codex-rs/cli/src/main.rs @@ -572,25 +572,12 @@ fn print_completion(cmd: CompletionCommand) { fn run_update_action(action: UpdateAction) -> anyhow::Result<()> { println!(); - match action { - UpdateAction::NpmGlobalLatest => { - println!("Updating Codex via npm (global latest)…"); - let status = std::process::Command::new("npm") - .args(["install", "-g", "@openai/codex@latest"]) - .status()?; - if !status.success() { - anyhow::bail!("npm update failed with status {status}"); - } - } - UpdateAction::BrewUpgrade => { - println!("Updating Codex via Homebrew…"); - let status = std::process::Command::new("brew") - .args(["upgrade", "codex"]) - .status()?; - if !status.success() { - anyhow::bail!("brew upgrade failed with status {status}"); - } - } + let (cmd, args) = action.command_args(); + let cmd_str = action.command_str(); + println!("Updating Codex via {cmd_str}…"); + let status = std::process::Command::new(cmd).args(args).status()?; + if !status.success() { + anyhow::bail!("`{cmd_str}` failed with status {status}"); } println!(); println!("🎉 Update ran successfully! Please restart Codex."); diff --git a/codex-rs/tui/src/lib.rs b/codex-rs/tui/src/lib.rs index 7e4854749d..061ca9cafb 100644 --- a/codex-rs/tui/src/lib.rs +++ b/codex-rs/tui/src/lib.rs @@ -82,6 +82,25 @@ pub enum UpdateAction { /// Update via `brew upgrade codex`. BrewUpgrade, } + +impl UpdateAction { + /// Returns the list of command-line arguments for invoking the update. + pub fn command_args(&self) -> (&'static str, &'static [&'static str]) { + match self { + UpdateAction::NpmGlobalLatest => ("npm", &["install", "-g", "@openai/codex@latest"]), + UpdateAction::BunGlobalLatest => ("bun", &["install", "-g", "@openai/codex@latest"]), + UpdateAction::BrewUpgrade => ("brew", &["upgrade", "codex"]), + } + } + + /// Returns string representation of the command-line arguments for invoking the update. + pub fn command_str(&self) -> String { + let (command, args) = self.command_args(); + let args_str = args.join(" "); + format!("{command} {args_str}") + } +} + mod wrapping; #[cfg(test)] @@ -340,20 +359,9 @@ async fn run_ratatui_app( ]; if let Some(update_action) = get_update_action() { - let cmd = match update_action { - UpdateAction::NpmGlobalLatest => { - "npm install -g @openai/codex@latest"; - } - UpdateAction::BunGlobalLatest => { - "bun install -g @openai/codex@latest"; - } - UpdateAction::BrewUpgrade => { - "brew upgrade codex"; - } - }; content_lines.push(Line::from(vec![ "Run ".into(), - cmd.cyan(), + update_action.command_str().cyan(), " to update.".into(), ])); } else { @@ -481,6 +489,7 @@ async fn run_ratatui_app( app_result } +#[cfg(not(debug_assertions))] pub(crate) fn get_update_action() -> Option { let exe = std::env::current_exe().unwrap_or_default(); let managed_by_npm = std::env::var_os("CODEX_MANAGED_BY_NPM").is_some(); @@ -499,6 +508,7 @@ pub(crate) fn get_update_action() -> Option { } #[test] +#[cfg(not(debug_assertions))] fn test_get_update_action() { let prev = std::env::var_os("CODEX_MANAGED_BY_NPM");