Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/cortex-cli/src/cli/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -836,7 +836,7 @@ pub async fn show_config(config_cli: ConfigCommand) -> Result<()> {
if config_path.exists() {
let content = std::fs::read_to_string(&config_path)?;
let config: toml::Value = toml::from_str(&content)?;
if let Some(value) = config.get(&args.key) {
if let Some(value) = get_config_value(&config, &args.key) {
println!("{}", value);
} else {
bail!("Key '{}' not found in configuration", args.key);
Expand Down Expand Up @@ -901,6 +901,19 @@ pub async fn show_config(config_cli: ConfigCommand) -> Result<()> {
Ok(())
}

fn get_config_value<'a>(config: &'a toml::Value, key: &str) -> Option<&'a toml::Value> {
config.get(key).or_else(|| {
let mut current = config;
for part in key.split('.') {
if part.is_empty() {
return None;
}
current = current.get(part)?;
}
Some(current)
})
}

/// List features.
pub async fn list_features() -> Result<()> {
println!("Feature flags:");
Expand Down
33 changes: 33 additions & 0 deletions src/cortex-cli/tests/config_get.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use std::fs;
use std::process::Command;

use tempfile::tempdir;

#[test]
fn config_get_resolves_dotted_keys() {
let home_dir = tempdir().unwrap();
let cortex_home = home_dir.path().join(".cortex");
fs::create_dir_all(&cortex_home).unwrap();
fs::write(
cortex_home.join("config.toml"),
r#"[model]
default = "gpt-4o"
"#,
)
.unwrap();

let output = Command::new(env!("CARGO_BIN_EXE_Cortex"))
.args(["config", "get", "model.default"])
.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(),
"config get failed\nstdout:\n{stdout}\nstderr:\n{stderr}"
);
assert!(stdout.contains("gpt-4o"), "stdout was {stdout:?}");
}