zebrad/commands/
generate.rs1use crate::config::ZebradConfig;
4use abscissa_core::{Command, Runnable};
5use clap::Parser;
6
7#[derive(Command, Debug, Default, Parser)]
9pub struct GenerateCmd {
10 #[clap(
14 long,
15 short,
16 help = "The file to write the generated config to (stdout if unspecified)"
17 )]
18 output_file: Option<String>,
19}
20
21impl Runnable for GenerateCmd {
22 #[allow(clippy::print_stdout)]
24 fn run(&self) {
25 let default_config = ZebradConfig::default();
26 let mut output = r"# Default configuration for zebrad.
27#
28# This file can be used as a skeleton for custom configs.
29#
30# Unspecified fields use default values. Optional fields are Some(field) if the
31# field is present and None if it is absent.
32#
33# This file is generated as an example using zebrad's current defaults.
34# You should set only the config options you want to keep, and delete the rest.
35# Only a subset of fields are present in the skeleton, since optional values
36# whose default is None are omitted.
37#
38# The config format (including a complete list of sections and fields) is
39# documented here:
40# https://docs.rs/zebrad/latest/zebrad/config/struct.ZebradConfig.html
41#
42# zebrad attempts to load configs in the following order:
43#
44# 1. The -c flag on the command line, e.g., `zebrad -c myconfig.toml start`;
45# 2. The file `zebrad.toml` in the users's preference directory (platform-dependent);
46# 3. The default config.
47#
48# The user's preference directory and the default path to the `zebrad` config are platform dependent,
49# based on `dirs::preference_dir`, see https://docs.rs/dirs/latest/dirs/fn.preference_dir.html :
50#
51# | Platform | Value | Example |
52# | -------- | ------------------------------------- | ---------------------------------------------- |
53# | Linux | `$XDG_CONFIG_HOME` or `$HOME/.config` | `/home/alice/.config/zebrad.toml` |
54# | macOS | `$HOME/Library/Preferences` | `/Users/Alice/Library/Preferences/zebrad.toml` |
55# | Windows | `{FOLDERID_RoamingAppData}` | `C:\Users\Alice\AppData\Local\zebrad.toml` |
56
57"
58 .to_owned();
59
60 let conf = toml::Value::try_from(default_config).unwrap();
63 output += &toml::to_string_pretty(&conf).expect("default config should be serializable");
64 match self.output_file {
65 Some(ref output_file) => {
66 use std::{fs::File, io::Write};
67 File::create(output_file)
68 .expect("must be able to open output file")
69 .write_all(output.as_bytes())
70 .expect("must be able to write output");
71 }
72 None => {
73 println!("{output}");
74 }
75 }
76 }
77}