codeberg_cli/actions/config/
info.rs

1use config::ValueKind;
2use itertools::Itertools;
3
4use crate::{actions::GeneralArgs, types::config::BergConfig};
5
6use clap::Parser;
7
8/// Display short overview of which config values are used
9#[derive(Parser, Debug)]
10pub struct ConfigInfoArgs {}
11
12impl ConfigInfoArgs {
13    pub fn run(self, general_args: GeneralArgs) -> anyhow::Result<()> {
14        let _ = general_args;
15        // use the raw config since it contains the origin of eeld
16        let raw_config = BergConfig::raw()?;
17        let config = BergConfig::new()?;
18
19        present_config_info(config, raw_config)?;
20
21        Ok(())
22    }
23}
24
25fn present_config_info(config: BergConfig, raw_config: config::Config) -> anyhow::Result<()> {
26    let mut table = config.make_table();
27
28    table.set_header(vec!["Field", "Source", "Value"]).add_rows(
29        raw_config
30            .cache
31            .into_table()?
32            .into_iter()
33            .sorted_by_key(|(k, _)| k.to_string())
34            .map(|(k, v)| {
35                // this is a bit difficult since the lib doesn't make the field public
36                let origin = {
37                    let dbg = format!("{v:?}");
38                    dbg.find("origin: ").map_or_else(
39                        || String::from("Default"),
40                        |idx| {
41                            let origin = dbg
42                                .chars()
43                                .skip(idx)
44                                .skip("origin: ".len())
45                                .take_while(|&c| c != ',')
46                                .collect::<String>();
47
48                            match origin.as_str() {
49                                "None" => String::from("Default"),
50                                s if s.starts_with("Some(") => {
51                                    s.replace("Some(", "").replace(')', "")
52                                }
53                                s => s.to_string(),
54                            }
55                        },
56                    )
57                };
58                let v = {
59                    let is_string = matches!(v.kind, ValueKind::String(_));
60                    if is_string {
61                        format!("{:?}", v.to_string())
62                    } else {
63                        v.to_string()
64                    }
65                };
66                vec![k, origin, v]
67            }),
68    );
69
70    println!("{table}", table = table.show());
71
72    Ok(())
73}