lssa/wallet/src/cli/config.rs

145 lines
5.5 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 WalletSubcommand for ConfigSubcommand {
2025-11-03 15:45:50 +02:00
async fn handle_subcommand(
self,
wallet_core: &mut WalletCore,
) -> Result<SubcommandReturnValue> {
let config = wallet_core.config();
2025-10-31 16:23:01 +02:00
match self {
2026-03-09 18:27:56 +03:00
Self::Get { all, key } => {
if all {
let config_str = serde_json::to_string_pretty(&config)?;
2025-10-31 16:23:01 +02:00
2025-11-03 15:45:50 +02:00
println!("{config_str}");
} else if let Some(key) = key {
match key.as_str() {
"sequencer_addr" => {
println!("{}", config.sequencer_addr);
}
"seq_poll_timeout" => {
println!("{:?}", config.seq_poll_timeout);
}
"seq_tx_poll_max_blocks" => {
println!("{}", config.seq_tx_poll_max_blocks);
}
"seq_poll_max_retries" => {
println!("{}", config.seq_poll_max_retries);
}
"seq_block_poll_max_amount" => {
println!("{}", config.seq_block_poll_max_amount);
}
"basic_auth" => {
if let Some(basic_auth) = &config.basic_auth {
println!("{basic_auth}");
} else {
println!("Not set");
}
}
_ => {
println!("Unknown field");
}
2025-12-08 18:26:35 +03:00
}
} else {
println!("Please provide a key or use --all flag");
2025-12-08 18:26:35 +03:00
}
}
2026-03-09 18:27:56 +03:00
Self::Set { key, value } => {
let mut config = config.clone();
2025-11-03 15:45:50 +02:00
match key.as_str() {
2025-10-31 16:23:01 +02:00
"sequencer_addr" => {
config.sequencer_addr = value.parse()?;
2025-10-31 16:23:01 +02:00
}
"seq_poll_timeout" => {
config.seq_poll_timeout = humantime::parse_duration(&value)
.map_err(|e| anyhow::anyhow!("Invalid duration: {e}"))?;
2025-10-31 16:23:01 +02:00
}
"seq_tx_poll_max_blocks" => {
config.seq_tx_poll_max_blocks = value.parse()?;
2025-10-31 16:23:01 +02:00
}
"seq_poll_max_retries" => {
config.seq_poll_max_retries = value.parse()?;
2025-10-31 16:23:01 +02:00
}
"seq_block_poll_max_amount" => {
config.seq_block_poll_max_amount = value.parse()?;
2025-10-31 16:23:01 +02:00
}
2025-12-08 18:26:35 +03:00
"basic_auth" => {
config.basic_auth = Some(value.parse()?);
2025-12-08 18:26:35 +03:00
}
2025-10-31 16:23:01 +02:00
"initial_accounts" => {
2025-11-03 15:45:50 +02:00
anyhow::bail!("Setting this field from wallet is not supported");
2025-10-31 16:23:01 +02:00
}
_ => {
2025-11-03 15:45:50 +02:00
anyhow::bail!("Unknown field");
2025-10-31 16:23:01 +02:00
}
}
2025-11-03 15:45:50 +02:00
wallet_core.set_config(config);
2026-03-03 23:21:08 +03:00
wallet_core.store_config_changes().await?;
2025-10-31 16:23:01 +02:00
}
2026-03-09 18:27:56 +03:00
Self::Description { key } => match key.as_str() {
2025-11-17 14:43:33 +02:00
"override_rust_log" => {
println!("Value of variable RUST_LOG to override, affects logging");
}
"sequencer_addr" => {
println!("HTTP V4 account_id of sequencer");
2025-11-17 14:43:33 +02:00
}
"seq_poll_timeout" => {
2025-11-17 14:43:33 +02:00
println!(
"Sequencer client retry variable: how much time to wait between retries (human readable duration)"
2025-11-17 14:43:33 +02:00
);
}
"seq_tx_poll_max_blocks" => {
2025-11-17 14:43:44 +02:00
println!(
"Sequencer client polling variable: max number of blocks to poll to find a transaction"
2025-11-17 14:43:44 +02:00
);
2025-11-17 14:43:33 +02:00
}
"seq_poll_max_retries" => {
println!(
"Sequencer client retry variable: max number of retries before failing(can be zero)"
2025-11-17 14:43:33 +02:00
);
}
"seq_block_poll_max_amount" => {
2025-11-17 14:43:33 +02:00
println!(
"Sequencer client polling variable: max number of blocks to request in one polling call"
2025-11-17 14:43:33 +02:00
);
}
"initial_accounts" => {
println!("List of initial accounts' keys(both public and private)");
}
2025-12-08 18:26:35 +03:00
"basic_auth" => {
println!("Basic authentication credentials for sequencer HTTP requests");
}
2025-11-17 14:43:33 +02:00
_ => {
println!("Unknown field");
}
},
2025-10-31 16:23:01 +02:00
}
Ok(SubcommandReturnValue::Empty)
}
2025-11-03 15:45:50 +02:00
}