forge/cmd/
remappings.rs

1use clap::{Parser, ValueHint};
2use eyre::Result;
3use foundry_cli::utils::LoadConfig;
4use foundry_config::impl_figment_convert_basic;
5use std::{collections::BTreeMap, path::PathBuf};
6
7/// CLI arguments for `forge remappings`.
8#[derive(Clone, Debug, Parser)]
9pub struct RemappingArgs {
10    /// The project's root path.
11    ///
12    /// By default root of the Git repository, if in one,
13    /// or the current working directory.
14    #[arg(long, value_hint = ValueHint::DirPath, value_name = "PATH")]
15    root: Option<PathBuf>,
16    /// Pretty-print the remappings, grouping each of them by context.
17    #[arg(long)]
18    pretty: bool,
19}
20impl_figment_convert_basic!(RemappingArgs);
21
22impl RemappingArgs {
23    pub fn run(self) -> Result<()> {
24        let config = self.load_config()?;
25
26        if self.pretty {
27            let mut groups = BTreeMap::<_, Vec<_>>::new();
28            for remapping in config.remappings {
29                groups.entry(remapping.context.clone()).or_default().push(remapping);
30            }
31            for (group, remappings) in groups {
32                if let Some(group) = group {
33                    sh_println!("Context: {group}")?;
34                } else {
35                    sh_println!("Global:")?;
36                }
37
38                for mut remapping in remappings {
39                    remapping.context = None; // avoid writing context twice
40                    sh_println!("- {remapping}")?;
41                }
42                sh_println!()?;
43            }
44        } else {
45            for remapping in config.remappings {
46                sh_println!("{remapping}")?;
47            }
48        }
49
50        Ok(())
51    }
52}