codeberg_cli/actions/config/
info.rs

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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
use config::ValueKind;
use itertools::Itertools;

use crate::{actions::GeneralArgs, types::config::BergConfig};

use clap::Parser;

/// Display short overview of which config values are used
#[derive(Parser, Debug)]
pub struct ConfigInfoArgs {}

impl ConfigInfoArgs {
    pub fn run(self, general_args: GeneralArgs) -> anyhow::Result<()> {
        let _ = general_args;
        // use the raw config since it contains the origin of eeld
        let raw_config = BergConfig::raw()?;
        let config = BergConfig::new()?;

        present_config_info(config, raw_config)?;

        Ok(())
    }
}

fn present_config_info(config: BergConfig, raw_config: config::Config) -> anyhow::Result<()> {
    let mut table = config.make_table();

    table.set_header(vec!["Field", "Source", "Value"]).add_rows(
        raw_config
            .cache
            .into_table()?
            .into_iter()
            .sorted_by_key(|(k, _)| k.to_string())
            .map(|(k, v)| {
                // this is a bit difficult since the lib doesn't make the field public
                let origin = {
                    let dbg = format!("{v:?}");
                    dbg.find("origin: ").map_or_else(
                        || String::from("Default"),
                        |idx| {
                            let origin = dbg
                                .chars()
                                .skip(idx)
                                .skip("origin: ".len())
                                .take_while(|&c| c != ',')
                                .collect::<String>();

                            match origin.as_str() {
                                "None" => String::from("Default"),
                                s if s.starts_with("Some(") => {
                                    s.replace("Some(", "").replace(')', "")
                                }
                                s => s.to_string(),
                            }
                        },
                    )
                };
                let v = {
                    let is_string = matches!(v.kind, ValueKind::String(_));
                    if is_string {
                        format!("{:?}", v.to_string())
                    } else {
                        v.to_string()
                    }
                };
                vec![k, origin, v]
            }),
    );

    println!("{table}", table = table.show());

    Ok(())
}