Skip to main content

forge/cmd/
config.rs

1use super::build::BuildArgs;
2use clap::Parser;
3use eyre::Result;
4use foundry_cli::{opts::EvmArgs, utils::LoadConfig};
5use foundry_common::shell;
6use foundry_config::fix::fix_tomls_with_root;
7
8foundry_config::impl_figment_convert!(ConfigArgs, build, evm);
9
10/// CLI arguments for `forge config`.
11#[derive(Clone, Debug, Parser)]
12pub struct ConfigArgs {
13    /// Print only a basic set of the currently set config values.
14    #[arg(long)]
15    basic: bool,
16
17    /// Attempt to fix any configuration warnings.
18    #[arg(long)]
19    fix: bool,
20
21    // support nested build arguments
22    #[command(flatten)]
23    build: BuildArgs,
24
25    #[command(flatten)]
26    evm: EvmArgs,
27}
28
29impl ConfigArgs {
30    pub fn run(self) -> Result<()> {
31        if self.fix {
32            let root = self.build.build.project_paths.try_project_root()?;
33            for warning in fix_tomls_with_root(root) {
34                sh_warn!("{warning}")?;
35            }
36            return Ok(());
37        }
38
39        let config = self
40            .load_config_unsanitized()?
41            .normalized_optimizer_settings()
42            // we explicitly normalize the version, so mimic the behavior when invoking solc
43            .normalized_evm_version();
44
45        let s = if self.basic {
46            let config = config.into_basic();
47            if shell::is_json() {
48                serde_json::to_string_pretty(&config)?
49            } else {
50                config.to_string_pretty()?
51            }
52        } else if shell::is_json() {
53            serde_json::to_string_pretty(&config)?
54        } else {
55            config.to_string_pretty()?
56        };
57
58        sh_println!("{s}")?;
59        Ok(())
60    }
61}