use crate::errors::{CliError, Result};
use clap::Parser;
use quillmark::Quillmark;
use std::fs;
use std::path::PathBuf;
#[derive(Parser)]
pub struct SchemaArgs {
#[arg(value_name = "QUILL_PATH")]
quill_path: PathBuf,
#[arg(short, long, value_name = "FILE")]
output: Option<PathBuf>,
#[arg(long)]
with_ui: bool,
}
pub fn execute(args: SchemaArgs) -> Result<()> {
if !args.quill_path.exists() {
return Err(CliError::InvalidArgument(format!(
"Quill directory not found: {}",
args.quill_path.display()
)));
}
let engine = Quillmark::new();
let quill = engine.quill_from_path(&args.quill_path)?;
let config = quill.source().config();
let schema_yaml = if args.with_ui {
config.form_schema_yaml()
} else {
config.schema_yaml()
}
.map_err(|e| CliError::InvalidArgument(format!("Failed to serialize schema: {}", e)))?;
if let Some(output_path) = args.output {
fs::write(&output_path, schema_yaml).map_err(CliError::Io)?;
} else {
println!("{}", schema_yaml);
}
Ok(())
}