Skip to content

Commit 07eecf9

Browse files
committed
introduced non-interactive cmd arg instead - defaults to false
1 parent df3ae87 commit 07eecf9

File tree

8 files changed

+38
-10
lines changed

8 files changed

+38
-10
lines changed

forc-plugins/forc-node/src/chain_config.rs

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ use std::{
1414
collections::{HashMap, HashSet},
1515
fmt::Display,
1616
fs,
17-
io::IsTerminal,
1817
path::PathBuf,
1918
};
2019

@@ -71,28 +70,35 @@ pub struct ConfigFetcher {
7170
client: reqwest::Client,
7271
base_url: String,
7372
config_vault: PathBuf,
73+
non_interactive: bool,
7474
}
7575

7676
impl Default for ConfigFetcher {
7777
/// Creates a new fetcher to interact with github.
7878
/// By default user's chain configuration vault is at: `~/.forc/chainspecs`
7979
fn default() -> Self {
80+
Self::new(false)
81+
}
82+
}
83+
84+
impl ConfigFetcher {
85+
pub fn new(non_interactive: bool) -> Self {
8086
Self {
8187
client: reqwest::Client::new(),
8288
base_url: "https://api.github.com".to_string(),
8389
config_vault: user_forc_directory().join(CONFIG_FOLDER),
90+
non_interactive,
8491
}
8592
}
86-
}
8793

88-
impl ConfigFetcher {
8994
#[cfg(test)]
9095
/// Override the base url, to be used in tests.
9196
pub fn with_base_url(base_url: String) -> Self {
9297
Self {
9398
client: reqwest::Client::new(),
9499
base_url,
95100
config_vault: user_forc_directory().join(CONFIG_FOLDER),
101+
non_interactive: false,
96102
}
97103
}
98104

@@ -102,9 +108,14 @@ impl ConfigFetcher {
102108
client: reqwest::Client::new(),
103109
base_url,
104110
config_vault,
111+
non_interactive: false,
105112
}
106113
}
107114

115+
fn non_interactive(&self) -> bool {
116+
self.non_interactive
117+
}
118+
108119
fn get_base_url(&self) -> &str {
109120
&self.base_url
110121
}
@@ -294,7 +305,7 @@ async fn validate_local_chainconfig(fetcher: &ConfigFetcher) -> anyhow::Result<(
294305
"Local node configuration files are missing at {}",
295306
local_conf_dir.display()
296307
));
297-
let non_interactive = !std::io::stdout().is_terminal();
308+
let non_interactive = fetcher.non_interactive();
298309
if non_interactive {
299310
println_action_green(
300311
"Downloading",
@@ -335,7 +346,7 @@ async fn validate_remote_chainconfig(
335346
println_warning(&format!(
336347
"A network configuration update detected for {conf}, this might create problems while syncing with rest of the network"
337348
));
338-
let non_interactive = !std::io::stdout().is_terminal();
349+
let non_interactive = fetcher.non_interactive();
339350
if non_interactive {
340351
println_action_green(
341352
"Updating",
@@ -366,8 +377,11 @@ async fn validate_remote_chainconfig(
366377
/// Check local state of the configuration file in the vault (if they exists)
367378
/// and compare them to the remote one in github. If a change is detected asks
368379
/// user if they want to update, and does the update for them.
369-
pub async fn check_and_update_chain_config(conf: ChainConfig) -> anyhow::Result<()> {
370-
let fetcher = ConfigFetcher::default();
380+
pub async fn check_and_update_chain_config(
381+
conf: ChainConfig,
382+
non_interactive: bool,
383+
) -> anyhow::Result<()> {
384+
let fetcher = ConfigFetcher::new(non_interactive);
371385
match conf {
372386
ChainConfig::Local => validate_local_chainconfig(&fetcher).await?,
373387
remote_config => validate_remote_chainconfig(&fetcher, &remote_config).await?,

forc-plugins/forc-node/src/ignition/cmd.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ pub struct IgnitionCmd {
1010
pub db_path: PathBuf,
1111
#[clap(long, default_value_t = MAINNET_BOOTSTRAP_NODE.to_string())]
1212
pub bootstrap_node: String,
13+
14+
/// Skip interactive prompts (intended for scripted/test environments).
15+
#[clap(long, hide = true)]
16+
pub non_interactive: bool,
1317
}
1418

1519
fn default_ignition_db_path() -> PathBuf {

forc-plugins/forc-node/src/ignition/op.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use std::{
1919
/// Configures the node with ignition configuration to connect the node to latest mainnet.
2020
/// Returns `None` if this is a dry_run and no child process created for fuel-core.
2121
pub async fn run(cmd: IgnitionCmd, dry_run: bool) -> anyhow::Result<Option<Child>> {
22-
check_and_update_chain_config(ChainConfig::Ignition).await?;
22+
check_and_update_chain_config(ChainConfig::Ignition, cmd.non_interactive).await?;
2323
let keypair = if let (Some(peer_id), Some(secret)) = (
2424
&cmd.connection_settings.peer_id,
2525
&cmd.connection_settings.secret,

forc-plugins/forc-node/src/local/cmd.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ pub struct LocalCmd {
5757
/// Block number to fork from (latest if not specified)
5858
#[clap(long, value_name = "BLOCK")]
5959
pub fork_block_number: Option<u32>,
60+
61+
/// Skip interactive prompts (intended for scripted/test environments).
62+
#[clap(long, hide = true)]
63+
pub non_interactive: bool,
6064
}
6165

6266
fn get_coins_per_account(

forc-plugins/forc-node/src/local/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use std::sync::Arc;
2020
/// By default, the node is in `debug` mode and the db used is `in-memory`.
2121
/// Returns `None` if this is a dry_run and no service created for fuel-core.
2222
pub async fn run(cmd: cmd::LocalCmd, dry_run: bool) -> anyhow::Result<Option<FuelService>> {
23-
check_and_update_chain_config(ChainConfig::Local).await?;
23+
check_and_update_chain_config(ChainConfig::Local, cmd.non_interactive).await?;
2424

2525
let fork_url = cmd.fork_url.to_owned();
2626
let fork_block_number = cmd.fork_block_number;

forc-plugins/forc-node/src/testnet/cmd.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ pub struct TestnetCmd {
1010
pub db_path: PathBuf,
1111
#[clap(long, default_value_t = TESTNET_BOOTSTRAP_NODE.to_string())]
1212
pub bootstrap_node: String,
13+
14+
/// Skip interactive prompts (intended for scripted/test environments).
15+
#[clap(long, hide = true)]
16+
pub non_interactive: bool,
1317
}
1418

1519
fn default_testnet_db_path() -> PathBuf {

forc-plugins/forc-node/src/testnet/op.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use std::{
2020
/// Configures the node with testnet configuration to connect the node to latest testnet.
2121
/// Returns `None` if this is a dry_run and no child process created for fuel-core.
2222
pub async fn run(cmd: TestnetCmd, dry_run: bool) -> anyhow::Result<Option<Child>> {
23-
check_and_update_chain_config(ChainConfig::Testnet).await?;
23+
check_and_update_chain_config(ChainConfig::Testnet, cmd.non_interactive).await?;
2424
let keypair = if let (Some(peer_id), Some(secret)) = (
2525
&cmd.connection_settings.peer_id,
2626
&cmd.connection_settings.secret,

forc-plugins/forc-node/tests/local.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ async fn run_node(fork_url: Option<String>) -> (FuelService, String) {
7171
poa_instant: true,
7272
fork_url,
7373
fork_block_number: None,
74+
non_interactive: true,
7475
};
7576
let service = run(local_cmd, false).await.unwrap().unwrap();
7677
// Wait for node to start graphql service
@@ -511,6 +512,7 @@ async fn start_local_node_check_health() {
511512
poa_instant: false,
512513
fork_url: None,
513514
fork_block_number: None,
515+
non_interactive: true,
514516
};
515517

516518
let _service = run(local_cmd, false).await.unwrap().unwrap();

0 commit comments

Comments
 (0)