codeberg_cli/actions/config/
info.rs

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