hx config init: write a commented sample config

This commit is contained in:
hermes committed 2026-10-09 13:59:40 +00:00
1 parent e4694e126b
commit 10b22f5774
3 files changed
+97 -1

No files matched your search

+53
View File
@@ -74,8 +74,28 @@ enum Cmd {
},
/// Serve lens diagnostics + LSP navigation as an MCP server over stdio.
McpServe,
/// Config file helpers.
Config {
#[command(subcommand)]
action: ConfigAction,
},
}
#[derive(Subcommand)]
enum ConfigAction {
/// Write a commented sample config to ~/.hx/config.toml (or .hx/config.toml with --project).
Init {
#[arg(long)]
project: bool,
/// Replace an existing file.
#[arg(long)]
force: bool,
},
}
/// Every key commented out; `sample_config_parses` keeps it in sync with `Config`.
const SAMPLE_CONFIG: &str = include_str!("config.sample.toml");
#[derive(Subcommand)]
enum McpAction {
/// OAuth login for an HTTP MCP server from mcp.json.
@@ -510,6 +530,24 @@ async fn main() -> Result<()> {
print!("{}", hub.system_prompt());
return Ok(());
}
Some(Cmd::Config {
action: ConfigAction::Init { project, force },
}) => {
let p = if project {
std::env::current_dir()?.join(".hx/config.toml")
} else {
context::hx_home().join("config.toml")
};
if p.exists() && !force {
anyhow::bail!("{} exists; use --force to replace it", p.display());
}
if let Some(d) = p.parent() {
std::fs::create_dir_all(d)?;
}
std::fs::write(&p, SAMPLE_CONFIG)?;
println!("wrote {}", p.display());
return Ok(());
}
Some(Cmd::McpServe) => {
let cwd = std::env::current_dir()?;
let mgr = Arc::new(hx_lens::manager::Manager::new(
@@ -930,3 +968,18 @@ mod tests {
assert_eq!(pricing("gpt-5.6-terra").unwrap().input, 2.5);
}
}
#[cfg(test)]
mod config_tests {
#[test]
fn sample_config_parses() {
let s = super::SAMPLE_CONFIG;
toml::from_str::<super::Config>(s).expect("commented sample");
let on: String = s
.lines()
.map(|l| l.strip_prefix("# ").unwrap_or(l))
.collect::<Vec<_>>()
.join("\n");
toml::from_str::<super::Config>(&on).expect("uncommented sample must match Config");
}
}