From 34f308e89443442ce8e31178a1840c73749e19ae Mon Sep 17 00:00:00 2001 From: Greyforge Admin Date: Tue, 19 May 2026 23:30:58 -0400 Subject: [PATCH] Detect workspace JSON config --- src/cortex-cli/src/workspace_cmd.rs | 54 ++++++++++++++------------ src/cortex-cli/tests/workspace_show.rs | 40 +++++++++++++++++++ 2 files changed, 70 insertions(+), 24 deletions(-) create mode 100644 src/cortex-cli/tests/workspace_show.rs diff --git a/src/cortex-cli/src/workspace_cmd.rs b/src/cortex-cli/src/workspace_cmd.rs index 1e716c458..33cee8d44 100644 --- a/src/cortex-cli/src/workspace_cmd.rs +++ b/src/cortex-cli/src/workspace_cmd.rs @@ -8,7 +8,7 @@ use anyhow::{Result, bail}; use clap::Parser; use serde::{Deserialize, Serialize}; -use std::path::PathBuf; +use std::path::{Path, PathBuf}; /// Workspace CLI command. #[derive(Debug, Parser)] @@ -85,7 +85,7 @@ struct WorkspaceInfo { settings: Option, } -/// Workspace settings from .cortex/config.toml +/// Workspace settings from .cortex/config.toml or .cortex/config.json #[derive(Debug, Serialize, Deserialize, Default)] struct WorkspaceSettings { #[serde(default)] @@ -136,11 +136,11 @@ fn find_workspace_root() -> PathBuf { async fn run_show(args: WorkspaceShowArgs) -> Result<()> { let root = find_workspace_root(); let cortex_dir = root.join(".cortex"); - let config_path = cortex_dir.join("config.toml"); + let config_path = find_workspace_config_path(&cortex_dir); let agents_path = root.join("AGENTS.md"); let git_dir = root.join(".git"); - let has_cortex_config = config_path.exists(); + let has_cortex_config = config_path.is_some(); let has_agents_md = agents_path.exists(); let has_git = git_dir.exists(); @@ -151,24 +151,14 @@ async fn run_show(args: WorkspaceShowArgs) -> Result<()> { .map(|s| s.to_string()); // Load settings if config exists - let settings = if has_cortex_config { - std::fs::read_to_string(&config_path) - .ok() - .and_then(|content| toml::from_str(&content).ok()) - } else { - None - }; + let settings = config_path.as_deref().and_then(load_workspace_settings); let info = WorkspaceInfo { root: root.clone(), has_cortex_config, has_agents_md, has_git, - config_path: if has_cortex_config { - Some(config_path) - } else { - None - }, + config_path: config_path.clone(), agents_path: if has_agents_md { Some(agents_path) } else { @@ -193,14 +183,12 @@ async fn run_show(args: WorkspaceShowArgs) -> Result<()> { println!("Configuration:"); println!("{}", "-".repeat(40)); - println!( - " .cortex/config.toml: {}", - if info.has_cortex_config { - "present" - } else { - "not found" - } - ); + if let Some(ref path) = info.config_path { + let display_path = path.strip_prefix(&root).unwrap_or(path); + println!(" {}: present", display_path.display()); + } else { + println!(" .cortex/config.toml or .cortex/config.json: not found"); + } println!( " AGENTS.md: {}", if info.has_agents_md { @@ -237,6 +225,24 @@ async fn run_show(args: WorkspaceShowArgs) -> Result<()> { Ok(()) } +fn find_workspace_config_path(cortex_dir: &Path) -> Option { + ["config.toml", "config.json"] + .into_iter() + .map(|name| cortex_dir.join(name)) + .find(|path| path.exists()) +} + +fn load_workspace_settings(config_path: &Path) -> Option { + let content = std::fs::read_to_string(config_path).ok()?; + match config_path + .extension() + .and_then(|extension| extension.to_str()) + { + Some("json") => serde_json::from_str(&content).ok(), + _ => toml::from_str(&content).ok(), + } +} + async fn run_init(args: WorkspaceInitArgs) -> Result<()> { let root = find_workspace_root(); let cortex_dir = root.join(".cortex"); diff --git a/src/cortex-cli/tests/workspace_show.rs b/src/cortex-cli/tests/workspace_show.rs new file mode 100644 index 000000000..8af20a06b --- /dev/null +++ b/src/cortex-cli/tests/workspace_show.rs @@ -0,0 +1,40 @@ +use std::fs; +use std::process::Command; + +use serde_json::Value; +use tempfile::tempdir; + +#[test] +fn workspace_show_json_detects_config_json() { + let workspace = tempdir().unwrap(); + let home_dir = tempdir().unwrap(); + let cortex_dir = workspace.path().join(".cortex"); + fs::create_dir_all(&cortex_dir).unwrap(); + fs::write(cortex_dir.join("config.json"), r#"{"model":"gpt-4o"}"#).unwrap(); + + let output = Command::new(env!("CARGO_BIN_EXE_Cortex")) + .args(["workspace", "show", "--json"]) + .current_dir(workspace.path()) + .env("HOME", home_dir.path()) + .env_remove("CORTEX_HOME") + .output() + .unwrap(); + + let stdout = String::from_utf8_lossy(&output.stdout); + let stderr = String::from_utf8_lossy(&output.stderr); + assert!( + output.status.success(), + "workspace show failed\nstdout:\n{stdout}\nstderr:\n{stderr}" + ); + + let json: Value = serde_json::from_str(&stdout).unwrap(); + assert_eq!(json["has_cortex_config"], true); + assert!( + json["config_path"] + .as_str() + .unwrap() + .ends_with(".cortex/config.json"), + "config_path was {:?}", + json["config_path"] + ); +}