Skip to content

Commit af23a49

Browse files
author
Greyforge Admin
committed
Resolve dotted config get keys
1 parent 7954d02 commit af23a49

2 files changed

Lines changed: 47 additions & 1 deletion

File tree

src/cortex-cli/src/cli/handlers.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -836,7 +836,7 @@ pub async fn show_config(config_cli: ConfigCommand) -> Result<()> {
836836
if config_path.exists() {
837837
let content = std::fs::read_to_string(&config_path)?;
838838
let config: toml::Value = toml::from_str(&content)?;
839-
if let Some(value) = config.get(&args.key) {
839+
if let Some(value) = get_config_value(&config, &args.key) {
840840
println!("{}", value);
841841
} else {
842842
bail!("Key '{}' not found in configuration", args.key);
@@ -901,6 +901,19 @@ pub async fn show_config(config_cli: ConfigCommand) -> Result<()> {
901901
Ok(())
902902
}
903903

904+
fn get_config_value<'a>(config: &'a toml::Value, key: &str) -> Option<&'a toml::Value> {
905+
config.get(key).or_else(|| {
906+
let mut current = config;
907+
for part in key.split('.') {
908+
if part.is_empty() {
909+
return None;
910+
}
911+
current = current.get(part)?;
912+
}
913+
Some(current)
914+
})
915+
}
916+
904917
/// List features.
905918
pub async fn list_features() -> Result<()> {
906919
println!("Feature flags:");

src/cortex-cli/tests/config_get.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
use std::fs;
2+
use std::process::Command;
3+
4+
use tempfile::tempdir;
5+
6+
#[test]
7+
fn config_get_resolves_dotted_keys() {
8+
let home_dir = tempdir().unwrap();
9+
let cortex_home = home_dir.path().join(".cortex");
10+
fs::create_dir_all(&cortex_home).unwrap();
11+
fs::write(
12+
cortex_home.join("config.toml"),
13+
r#"[model]
14+
default = "gpt-4o"
15+
"#,
16+
)
17+
.unwrap();
18+
19+
let output = Command::new(env!("CARGO_BIN_EXE_Cortex"))
20+
.args(["config", "get", "model.default"])
21+
.env("HOME", home_dir.path())
22+
.env_remove("CORTEX_HOME")
23+
.output()
24+
.unwrap();
25+
26+
let stdout = String::from_utf8_lossy(&output.stdout);
27+
let stderr = String::from_utf8_lossy(&output.stderr);
28+
assert!(
29+
output.status.success(),
30+
"config get failed\nstdout:\n{stdout}\nstderr:\n{stderr}"
31+
);
32+
assert!(stdout.contains("gpt-4o"), "stdout was {stdout:?}");
33+
}

0 commit comments

Comments
 (0)