use clap::Parser;
use miette::{IntoDiagnostic, Result, bail};
use crate::actions::{
Context,
tamanu::{TamanuArgs, find_tamanu},
};
#[derive(Debug, Clone, Parser)]
pub struct ConfigArgs {
#[arg(short, long)]
pub package: Option<String>,
#[arg(short, long)]
pub compact: bool,
#[arg(short = 'n', long)]
pub or_null: bool,
#[arg(short, long)]
pub key: Option<String>,
#[arg(short, long)]
pub raw: bool,
}
pub async fn run(ctx: Context<TamanuArgs, ConfigArgs>) -> Result<()> {
let (_, root) = find_tamanu(&ctx.args_top)?;
let config = super::loader::load_config_as_object(&root, ctx.args_sub.package.as_deref())?;
let value = if let Some(key) = &ctx.args_sub.key {
let mut value = &config;
for part in key.split('.') {
value = match value.get(part) {
Some(value) => value,
None if ctx.args_sub.or_null => &serde_json::Value::Null,
None => bail!("key not found: {:?}", key),
};
}
value
} else {
&config
};
println!(
"{}",
match (ctx.args_sub, value.as_str()) {
(ConfigArgs { raw: true, .. }, Some(string)) => {
string.into()
}
(ConfigArgs { compact: true, .. }, _) => {
serde_json::to_string(&value).into_diagnostic()?
}
_ => serde_json::to_string_pretty(&value).into_diagnostic()?,
}
);
Ok(())
}