vector/
generate_schema.rs1use clap::Parser;
4use std::fs;
5use std::path::PathBuf;
6use vector_lib::configurable::schema::generate_root_schema;
7
8use crate::config::ConfigBuilder;
9
10#[derive(Parser, Debug)]
11#[command(rename_all = "kebab-case")]
12pub struct Opts {
14 #[arg(short, long)]
16 pub(crate) output_path: Option<PathBuf>,
17}
18
19#[allow(clippy::print_stdout, clippy::print_stderr)]
21pub fn cmd(opts: &Opts) -> exitcode::ExitCode {
22 match generate_root_schema::<ConfigBuilder>() {
23 Ok(schema) => {
24 let json = serde_json::to_string_pretty(&schema)
25 .expect("rendering root schema to JSON should not fail");
26
27 if let Some(output_path) = &opts.output_path {
28 if output_path.exists() {
29 eprintln!("Error: Output file {output_path:?} already exists");
30 return exitcode::CANTCREAT;
31 }
32
33 return match fs::write(output_path, json) {
34 Ok(_) => {
35 println!("Schema successfully written to {output_path:?}");
36 exitcode::OK
37 }
38 Err(e) => {
39 eprintln!("Error writing to file {output_path:?}: {e:?}");
40 exitcode::IOERR
41 }
42 };
43 } else {
44 println!("{json}");
45 }
46 exitcode::OK
47 }
48 Err(e) => {
49 eprintln!("error while generating schema: {e:?}");
50 exitcode::SOFTWARE
51 }
52 }
53}