1//! `generate` subcommand - generates a default `zebrad.toml` config.
23use crate::config::ZebradConfig;
4use abscissa_core::{Command, Runnable};
5use clap::Parser;
67/// Generate a default `zebrad.toml` configuration
8#[derive(Command, Debug, Default, Parser)]
9pub struct GenerateCmd {
10/// The file to write the generated config to.
11//
12 // TODO: use PathBuf here instead, to support non-UTF-8 paths
13#[clap(
14 long,
15 short,
16 help = "The file to write the generated config to (stdout if unspecified)"
17)]
18output_file: Option<String>,
19}
2021impl Runnable for GenerateCmd {
22/// Start the application.
23#[allow(clippy::print_stdout)]
24fn run(&self) {
25let default_config = ZebradConfig::default();
26let 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` |
5657"
58.to_owned();
5960// this avoids a ValueAfterTable error
61 // https://github.com/alexcrichton/toml-rs/issues/145
62let conf = toml::Value::try_from(default_config).unwrap();
63 output += &toml::to_string_pretty(&conf).expect("default config should be serializable");
64match self.output_file {
65Some(ref output_file) => {
66use 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 }
72None => {
73println!("{output}");
74 }
75 }
76 }
77}