use super::{utils, Args, CmdResult, Error, Path, PathBuf, State, StructOpt};
#[derive(StructOpt, Debug)]
pub struct Command {
#[structopt(short, long)]
pub pretty: bool,
#[structopt()]
pub pattern: String,
#[structopt(short, long)]
pub output: Option<PathBuf>,
}
impl Command {
pub fn run(&self, args: &Args, state: &State) -> CmdResult {
let contents = state
.schema_store
.get_one(&[&self.pattern], true, false)
.ok_or(Error::Query)?
.1;
let topic = "JSON Schema";
let contents = utils::format_json(topic, &contents, self.pretty)?;
if let Some(path) = &self.output {
log::info!("Saving {} to file '{}'...", topic, path.display());
args.check_output_file(&path)?;
let mut file = utils::create_file(topic, &path)?;
utils::write_output(topic, &path, &mut file, &contents)?;
} else {
let path = Path::new("stdout");
let mut file = std::io::stdout();
utils::write_output(topic, &path, &mut file, &contents)?;
}
Ok(0)
}
}