-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathgenerate_schema.rs
53 lines (47 loc) · 1.68 KB
/
generate_schema.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
//! Vector `generate-schema` command implementation.
use clap::Parser;
use std::fs;
use std::path::PathBuf;
use vector_lib::configurable::schema::generate_root_schema;
use crate::config::ConfigBuilder;
#[derive(Parser, Debug)]
#[command(rename_all = "kebab-case")]
/// Command line options for the `generate-schema` command.
pub struct Opts {
/// File path to
#[arg(short, long)]
pub(crate) output_path: Option<PathBuf>,
}
/// Execute the `generate-schema` command.
#[allow(clippy::print_stdout, clippy::print_stderr)]
pub fn cmd(opts: &Opts) -> exitcode::ExitCode {
match generate_root_schema::<ConfigBuilder>() {
Ok(schema) => {
let json = serde_json::to_string_pretty(&schema)
.expect("rendering root schema to JSON should not fail");
if let Some(output_path) = &opts.output_path {
if output_path.exists() {
eprintln!("Error: Output file {output_path:?} already exists");
return exitcode::CANTCREAT;
}
return match fs::write(output_path, json) {
Ok(_) => {
println!("Schema successfully written to {output_path:?}");
exitcode::OK
}
Err(e) => {
eprintln!("Error writing to file {output_path:?}: {e:?}");
exitcode::IOERR
}
};
} else {
println!("{json}");
}
exitcode::OK
}
Err(e) => {
eprintln!("error while generating schema: {:?}", e);
exitcode::SOFTWARE
}
}
}