zebra/zebrad/src/commands/generate.rs

61 lines
2.2 KiB
Rust

//! `generate` subcommand - generates a skeleton config.
use crate::config::ZebradConfig;
use abscissa_core::{Command, Options, Runnable};
/// `generate` subcommand
#[derive(Command, Debug, Options)]
pub struct GenerateCmd {
/// The file to write the generated config to.
#[options(help = "The file to write the generated config to (stdout if unspecified)")]
output_file: Option<String>,
}
impl Runnable for GenerateCmd {
/// Start the application.
fn run(&self) {
let default_config = ZebradConfig::default();
let mut output = r"# Default configuration for zebrad.
#
# This file can be used as a skeleton for custom configs.
#
# Unspecified fields use default values. Optional fields are Some(field) if the
# field is present and None if it is absent.
#
# This file is generated as an example using zebrad's current defaults.
# You should set only the config options you want to keep, and delete the rest.
# Only a subset of fields are present in the skeleton, since optional values
# whose default is None are omitted.
#
# The config format (including a complete list of sections and fields) is
# documented here:
# https://doc.zebra.zfnd.org/zebrad/config/struct.ZebradConfig.html
#
# zebrad attempts to load configs in the following order:
#
# 1. The -c flag on the command line, e.g., `zebrad -c myconfig.toml start`;
# 2. The file `zebrad.toml` in the users's preference directory (platform-dependent);
# 3. The default config.
"
.to_owned();
// this avoids a ValueAfterTable error
// https://github.com/alexcrichton/toml-rs/issues/145
let conf = toml::Value::try_from(default_config).unwrap();
output += &toml::to_string_pretty(&conf).expect("default config should be serializable");
match self.output_file {
Some(ref output_file) => {
use std::{fs::File, io::Write};
File::create(output_file)
.expect("must be able to open output file")
.write_all(output.as_bytes())
.expect("must be able to write output");
}
None => {
println!("{}", output);
}
}
}
}