1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// Copyright 2019-2024 ChainSafe Systems
// SPDX-License-Identifier: Apache-2.0, MIT

use std::io::Write;

use anyhow::Context as _;
use clap::Subcommand;

use crate::cli::subcommands::Config;

#[derive(Debug, Subcommand)]
pub enum ConfigCommands {
    /// Dump default configuration to standard output
    Dump,
}

impl ConfigCommands {
    pub fn run<W: Write + Unpin>(self, sink: &mut W) -> anyhow::Result<()> {
        match self {
            Self::Dump => writeln!(
                sink,
                "{}",
                toml::to_string(&Config::default())
                    .context("Could not convert configuration to TOML format")?
            )
            .context("Failed to write the configuration"),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[tokio::test]
    async fn given_default_configuration_should_print_valid_toml() {
        let expected_config = Config::default();
        let mut sink = std::io::BufWriter::new(Vec::new());

        ConfigCommands::Dump.run(&mut sink).unwrap();

        let actual_config: Config = toml::from_str(std::str::from_utf8(sink.buffer()).unwrap())
            .expect("Invalid configuration!");

        assert_eq!(expected_config, actual_config);
    }
}