zebrad/commands/
generate.rs

1//! `generate` subcommand - generates a default `zebrad.toml` config.
2
3use crate::config::ZebradConfig;
4use abscissa_core::{Command, Runnable};
5use clap::Parser;
6
7/// 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    )]
18    output_file: Option<String>,
19}
20
21impl Runnable for GenerateCmd {
22    /// Start the application.
23    #[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# CONFIGURATION SOURCES (in order of precedence, highest to lowest):
43#
44# 1. Environment variables with ZEBRA_ prefix (highest precedence)
45#    - Format: ZEBRA_SECTION__KEY (double underscore for nested keys)
46#    - Examples:
47#      - ZEBRA_NETWORK__NETWORK=Testnet
48#      - ZEBRA_RPC__LISTEN_ADDR=127.0.0.1:8232
49#      - ZEBRA_STATE__CACHE_DIR=/path/to/cache
50#      - ZEBRA_TRACING__FILTER=debug
51#      - ZEBRA_METRICS__ENDPOINT_ADDR=0.0.0.0:9999
52#
53# 2. Configuration file (TOML format)
54#    - At the path specified via -c flag, e.g. `zebrad -c myconfig.toml start`, or
55#    - At the default path in the user's preference directory (platform-dependent, see below)
56#
57# 3. Hard-coded defaults (lowest precedence)
58#
59# The user's preference directory and the default path to the `zebrad` config are platform dependent,
60# based on `dirs::preference_dir`, see https://docs.rs/dirs/latest/dirs/fn.preference_dir.html :
61#
62# | Platform | Value                                 | Example                                        |
63# | -------- | ------------------------------------- | ---------------------------------------------- |
64# | Linux    | `$XDG_CONFIG_HOME` or `$HOME/.config` | `/home/alice/.config/zebrad.toml`              |
65# | macOS    | `$HOME/Library/Preferences`           | `/Users/Alice/Library/Preferences/zebrad.toml` |
66# | Windows  | `{FOLDERID_RoamingAppData}`           | `C:\Users\Alice\AppData\Local\zebrad.toml`     |
67
68"
69        .to_owned();
70
71        // this avoids a ValueAfterTable error
72        // https://github.com/alexcrichton/toml-rs/issues/145
73        let conf = toml::Value::try_from(default_config).unwrap();
74        output += &toml::to_string_pretty(&conf).expect("default config should be serializable");
75        match self.output_file {
76            Some(ref output_file) => {
77                use std::{fs::File, io::Write};
78                File::create(output_file)
79                    .expect("must be able to open output file")
80                    .write_all(output.as_bytes())
81                    .expect("must be able to write output");
82            }
83            None => {
84                println!("{output}");
85            }
86        }
87    }
88}