refactor(g3-cli): eliminate code-path aliasing in config and project content loading
Consolidate duplicated logic into canonical shared functions: - Extract load_config_with_cli_overrides() to utils.rs - Was duplicated in lib.rs and accumulative.rs with subtle differences - lib.rs version had Chrome diagnostics + provider validation - accumulative.rs version was missing both - Now all callers use the complete canonical implementation - Extract combine_project_content() to project_files.rs - Was duplicated inline in lib.rs and agent_mode.rs - Simplified implementation using iterator flatten - Added unit tests for all cases This eliminates drift risk where the duplicated implementations could diverge over time (accumulative.rs was already missing Chrome diagnostics and provider validation). Agent: fowler
This commit is contained in:
@@ -2,10 +2,12 @@
|
||||
|
||||
use anyhow::Result;
|
||||
use crossterm::style::{Color, ResetColor, SetForegroundColor};
|
||||
use g3_config::Config;
|
||||
use g3_core::ui_writer::UiWriter;
|
||||
use g3_core::Agent;
|
||||
use std::path::PathBuf;
|
||||
|
||||
use crate::cli_args::Cli;
|
||||
use crate::simple_output::SimpleOutput;
|
||||
|
||||
/// Display context window progress bar.
|
||||
@@ -89,3 +91,56 @@ pub fn setup_workspace_directory() -> Result<PathBuf> {
|
||||
|
||||
Ok(workspace_dir)
|
||||
}
|
||||
|
||||
/// Load configuration with CLI argument overrides applied.
|
||||
///
|
||||
/// This is the canonical function for loading config with CLI overrides.
|
||||
/// All CLI entry points should use this to ensure consistent behavior.
|
||||
pub fn load_config_with_cli_overrides(cli: &Cli) -> Result<Config> {
|
||||
let mut config = Config::load_with_overrides(
|
||||
cli.config.as_deref(),
|
||||
cli.provider.clone(),
|
||||
cli.model.clone(),
|
||||
)?;
|
||||
|
||||
// Apply webdriver flag override
|
||||
if cli.webdriver {
|
||||
config.webdriver.enabled = true;
|
||||
}
|
||||
|
||||
// Apply chrome-headless flag override
|
||||
if cli.chrome_headless {
|
||||
config.webdriver.enabled = true;
|
||||
config.webdriver.browser = g3_config::WebDriverBrowser::ChromeHeadless;
|
||||
|
||||
// Run Chrome diagnostics on first use
|
||||
let report =
|
||||
g3_computer_control::run_chrome_diagnostics(config.webdriver.chrome_binary.as_deref());
|
||||
println!("{}", report.format_report());
|
||||
}
|
||||
|
||||
// Apply safari flag override
|
||||
if cli.safari {
|
||||
config.webdriver.enabled = true;
|
||||
config.webdriver.browser = g3_config::WebDriverBrowser::Safari;
|
||||
}
|
||||
|
||||
// Apply no-auto-compact flag override
|
||||
if cli.manual_compact {
|
||||
config.agent.auto_compact = false;
|
||||
}
|
||||
|
||||
// Validate provider if specified
|
||||
if let Some(ref provider) = cli.provider {
|
||||
let valid_providers = ["anthropic", "databricks", "embedded", "openai"];
|
||||
if !valid_providers.contains(&provider.as_str()) {
|
||||
return Err(anyhow::anyhow!(
|
||||
"Invalid provider '{}'. Valid options: {:?}",
|
||||
provider,
|
||||
valid_providers
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
Ok(config)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user