From 9ec1e01c8fe1f61023b014eb5e10d434db3e7794 Mon Sep 17 00:00:00 2001 From: Henry de Valence Date: Fri, 18 Oct 2019 17:04:41 -0700 Subject: [PATCH] Add a config command. --- zebrad/src/commands.rs | 7 +++++- zebrad/src/commands/config.rs | 46 +++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 zebrad/src/commands/config.rs diff --git a/zebrad/src/commands.rs b/zebrad/src/commands.rs index c599496e..af1db925 100644 --- a/zebrad/src/commands.rs +++ b/zebrad/src/commands.rs @@ -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), diff --git a/zebrad/src/commands/config.rs b/zebrad/src/commands/config.rs new file mode 100644 index 00000000..ab3ae8ba --- /dev/null +++ b/zebrad/src/commands/config.rs @@ -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, +} + +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); + } + } + } +}