167 lines
5.4 KiB
Rust
Raw Normal View History

2025-10-31 16:23:01 +02:00
use anyhow::Result;
use clap::Subcommand;
use crate::{
WalletCore,
cli::{SubcommandReturnValue, WalletSubcommand},
};
2025-10-31 16:23:01 +02:00
2026-03-10 00:17:43 +03:00
/// Represents generic config CLI subcommand.
2025-10-31 16:23:01 +02:00
#[derive(Subcommand, Debug, Clone)]
pub enum ConfigSubcommand {
2026-03-10 00:17:43 +03:00
/// Getter of config fields.
Get {
2026-03-10 00:17:43 +03:00
/// Print all config fields.
#[arg(short, long)]
all: bool,
2026-03-10 00:17:43 +03:00
/// Config field key to get.
key: Option<String>,
},
2026-03-10 00:17:43 +03:00
/// Setter of config fields.
2025-11-03 15:45:50 +02:00
Set { key: String, value: String },
2026-03-10 00:17:43 +03:00
/// Prints description of corresponding field.
2025-11-17 14:43:33 +02:00
Description { key: String },
2025-10-31 16:23:01 +02:00
}
impl ConfigSubcommand {
fn handle_get(
all: bool,
key: Option<String>,
wallet_core: &WalletCore,
2025-11-03 15:45:50 +02:00
) -> Result<SubcommandReturnValue> {
let config = wallet_core.config();
if all {
let config_str = serde_json::to_string_pretty(&config)?;
2025-11-03 15:45:50 +02:00
println!("{config_str}");
} else if let Some(key) = key {
match key.as_str() {
2025-11-17 14:43:33 +02:00
"sequencer_addr" => {
println!("{}", config.sequencer_addr);
2025-11-17 14:43:33 +02:00
}
"seq_poll_timeout" => {
println!("{:?}", config.seq_poll_timeout);
2025-11-17 14:43:33 +02:00
}
"seq_tx_poll_max_blocks" => {
println!("{}", config.seq_tx_poll_max_blocks);
2025-11-17 14:43:33 +02:00
}
"seq_poll_max_retries" => {
println!("{}", config.seq_poll_max_retries);
2025-11-17 14:43:33 +02:00
}
"seq_block_poll_max_amount" => {
println!("{}", config.seq_block_poll_max_amount);
2025-11-17 14:43:33 +02:00
}
2025-12-08 18:26:35 +03:00
"basic_auth" => {
if let Some(basic_auth) = &config.basic_auth {
println!("{basic_auth}");
} else {
println!("Not set");
}
2025-12-08 18:26:35 +03:00
}
2025-11-17 14:43:33 +02:00
_ => {
println!("Unknown field");
}
}
} else {
println!("Please provide a key or use --all flag");
}
Ok(SubcommandReturnValue::Empty)
}
async fn handle_set(
key: String,
value: String,
wallet_core: &mut WalletCore,
) -> Result<SubcommandReturnValue> {
let mut config = wallet_core.config().clone();
match key.as_str() {
"sequencer_addr" => {
config.sequencer_addr = value.parse()?;
}
"seq_poll_timeout" => {
config.seq_poll_timeout = humantime::parse_duration(&value)
.map_err(|e| anyhow::anyhow!("Invalid duration: {e}"))?;
}
"seq_tx_poll_max_blocks" => {
config.seq_tx_poll_max_blocks = value.parse()?;
}
"seq_poll_max_retries" => {
config.seq_poll_max_retries = value.parse()?;
}
"seq_block_poll_max_amount" => {
config.seq_block_poll_max_amount = value.parse()?;
}
"basic_auth" => {
config.basic_auth = Some(value.parse()?);
}
"initial_accounts" => {
anyhow::bail!("Setting this field from wallet is not supported");
}
_ => {
anyhow::bail!("Unknown field");
}
2025-10-31 16:23:01 +02:00
}
wallet_core.set_config(config);
wallet_core.store_config_changes().await?;
2025-10-31 16:23:01 +02:00
Ok(SubcommandReturnValue::Empty)
}
fn handle_description(key: &str, _wallet_core: &WalletCore) -> SubcommandReturnValue {
match key {
"override_rust_log" => {
println!("Value of variable RUST_LOG to override, affects logging");
}
"sequencer_addr" => {
println!("HTTP V4 account_id of sequencer");
}
"seq_poll_timeout" => {
println!(
"Sequencer client retry variable: how much time to wait between retries (human readable duration)"
);
}
"seq_tx_poll_max_blocks" => {
println!(
"Sequencer client polling variable: max number of blocks to poll to find a transaction"
);
}
"seq_poll_max_retries" => {
println!(
"Sequencer client retry variable: max number of retries before failing(can be zero)"
);
}
"seq_block_poll_max_amount" => {
println!(
"Sequencer client polling variable: max number of blocks to request in one polling call"
);
}
"initial_accounts" => {
println!("List of initial accounts' keys(both public and private)");
}
"basic_auth" => {
println!("Basic authentication credentials for sequencer HTTP requests");
}
_ => {
println!("Unknown field");
}
}
SubcommandReturnValue::Empty
}
}
impl WalletSubcommand for ConfigSubcommand {
async fn handle_subcommand(
self,
wallet_core: &mut WalletCore,
) -> Result<SubcommandReturnValue> {
match self {
Self::Get { all, key } => Self::handle_get(all, key, wallet_core),
Self::Set { key, value } => Self::handle_set(key, value, wallet_core).await,
Self::Description { key } => Ok(Self::handle_description(&key, wallet_core)),
}
}
2025-11-03 15:45:50 +02:00
}