Add a config command.

This commit is contained in:
Henry de Valence 2019-10-18 17:04:41 -07:00 committed by Deirdre Connolly
parent ad43a61fb4
commit 9ec1e01c8f
2 changed files with 52 additions and 1 deletions

View File

@ -10,11 +10,12 @@
//! See the `impl Configurable` below for how to specify the path to the
//! application's configuration file.
mod config;
mod connect;
mod start;
mod version;
use self::{connect::ConnectCmd, start::StartCmd, version::VersionCmd};
use self::{config::ConfigCmd, connect::ConnectCmd, start::StartCmd, version::VersionCmd};
use crate::config::ZebradConfig;
use abscissa_core::{
config::Override, Command, Configurable, FrameworkError, Help, Options, Runnable,
@ -35,6 +36,10 @@ pub enum ZebradCmd {
#[options(help = "start the application")]
Start(StartCmd),
/// The `config` subcommand
#[options(help = "generate a skeleton configuration")]
Config(ConfigCmd),
/// The `version` subcommand
#[options(help = "display version information")]
Version(VersionCmd),

View File

@ -0,0 +1,46 @@
//! `config` subcommand - generates a skeleton config.
use crate::config::ZebradConfig;
use abscissa_core::{Command, Options, Runnable};
/// `config` subcommand
#[derive(Command, Debug, Options)]
pub struct ConfigCmd {
/// 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 ConfigCmd {
/// Start the application.
fn run(&self) {
let default_config = ZebradConfig::default();
let mut output = r"# Default configuration values for zebrad.
#
# This file is intended as a skeleton for custom configs.
#
# Because this contains default values, and the default
# values may change, you should delete all entries except
# for the ones you wish to change.
#
# Documentation on the meanings of each config option
# can be found in Rustdoc. XXX add link to rendered docs.
"
.to_owned();
output += &toml::to_string_pretty(&default_config)
.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);
}
}
}
}