Skip to main content

forest/cli/
main.rs

1// Copyright 2019-2026 ChainSafe Systems
2// SPDX-License-Identifier: Apache-2.0, MIT
3
4use crate::cli::subcommands::Cli;
5use crate::cli_shared::logger;
6use crate::networks::NetworkChain;
7use crate::rpc::{self, prelude::*};
8use crate::shim::address::{CurrentNetwork, Network};
9use clap::Parser;
10use std::ffi::OsString;
11use std::str::FromStr as _;
12
13pub async fn main<ArgT>(args: impl IntoIterator<Item = ArgT>) -> anyhow::Result<()>
14where
15    ArgT: Into<OsString> + Clone,
16{
17    // Preliminary client without the token to check network. This needs to occur before parsing to ensure the
18    // `StrictAddress` validation works correctly.
19    let client = rpc::Client::default_or_from_env(None)?;
20    if let Ok(name) = StateNetworkName::call(&client, ()).await
21        && !matches!(NetworkChain::from_str(&name), Ok(NetworkChain::Mainnet))
22    {
23        CurrentNetwork::set_global(Network::Testnet);
24    }
25
26    // Capture Cli inputs
27    let Cli { token, cmd } = Cli::parse_from(args);
28
29    let client = rpc::Client::default_or_from_env(token.as_deref())?;
30
31    let _bg_tasks = logger::setup_logger(&crate::cli_shared::cli::CliOpts::default());
32
33    cmd.run(client).await.map_err(rpc::humanize_rpc_error)
34}