use std::{collections::BTreeMap, fs, path::PathBuf};
use anyhow::Result;
use clap::Parser;
use log::info;
use serde_json::Value;
use crate::{clap::parsers::path_parser, printer::Printer};
#[derive(Debug, Parser)]
pub struct JsonSchemaCommand {
#[arg(value_parser = path_parser)]
pub dir: PathBuf,
}
impl JsonSchemaCommand {
pub fn execute(
self,
printer: &mut impl Printer,
schemas: BTreeMap<String, Value>,
) -> Result<()> {
let dir = &self.dir;
let count = schemas.len();
fs::create_dir_all(dir)?;
for (name, schema) in schemas {
let json = serde_json::to_vec_pretty(&schema)?;
info!("generate JSON Schema for command {name}");
fs::write(dir.join(format!("{name}.json")), json)?;
}
printer.out(format!(
"{count} JSON Schema(s) successfully generated in {}",
dir.display()
))
}
}