forest/cli/subcommands/
config_cmd.rs1use std::io::Write;
5
6use anyhow::Context as _;
7use clap::Subcommand;
8
9use crate::{cli::subcommands::Config, rpc};
10
11#[derive(Debug, Subcommand)]
12pub enum ConfigCommands {
13 Dump,
15}
16
17impl ConfigCommands {
18 pub async fn run(self, _: rpc::Client) -> anyhow::Result<()> {
19 self.run_internal(&mut std::io::stdout())
20 }
21
22 fn run_internal<W: Write + Unpin>(self, sink: &mut W) -> anyhow::Result<()> {
23 match self {
24 Self::Dump => writeln!(
25 sink,
26 "{}",
27 toml::to_string(&Config::default())
28 .context("Could not convert configuration to TOML format")?
29 )
30 .context("Failed to write the configuration"),
31 }
32 }
33}
34
35#[cfg(test)]
36mod tests {
37 use super::*;
38
39 #[tokio::test]
40 async fn given_default_configuration_should_print_valid_toml() {
41 let expected_config = Config::default();
42 let mut sink = std::io::BufWriter::new(Vec::new());
43
44 ConfigCommands::Dump.run_internal(&mut sink).unwrap();
45
46 let actual_config: Config = toml::from_str(std::str::from_utf8(sink.buffer()).unwrap())
47 .expect("Invalid configuration!");
48
49 assert_eq!(expected_config, actual_config);
50 }
51}