diff --git a/src/db.rs b/src/db.rs index f801e11..4584f20 100644 --- a/src/db.rs +++ b/src/db.rs @@ -708,24 +708,42 @@ impl Db { pub fn set_exchange_credentials( &mut self, exchange: Exchange, + exchange_account: &str, exchange_credentials: ExchangeCredentials, ) -> DbResult<()> { - self.clear_exchange_credentials(exchange)?; + self.clear_exchange_credentials(exchange, exchange_account)?; self.credentials_db - .set(&format!("{exchange:?}"), &exchange_credentials) + .set( + &format!("{exchange:?}{exchange_account}"), + &exchange_credentials, + ) .unwrap(); Ok(self.credentials_db.dump()?) } - pub fn get_exchange_credentials(&self, exchange: Exchange) -> Option { - self.credentials_db.get(&format!("{exchange:?}")) + pub fn get_exchange_credentials( + &self, + exchange: Exchange, + exchange_account: &str, + ) -> Option { + self.credentials_db + .get(&format!("{exchange:?}{exchange_account}")) } - pub fn clear_exchange_credentials(&mut self, exchange: Exchange) -> DbResult<()> { - if self.get_exchange_credentials(exchange).is_some() { - self.credentials_db.rem(&format!("{exchange:?}")).ok(); + pub fn clear_exchange_credentials( + &mut self, + exchange: Exchange, + exchange_account: &str, + ) -> DbResult<()> { + if self + .get_exchange_credentials(exchange, exchange_account) + .is_some() + { + self.credentials_db + .rem(&format!("{exchange:?}{exchange_account}")) + .ok(); self.credentials_db.dump()?; } Ok(()) @@ -737,7 +755,7 @@ impl Db { .into_iter() .filter_map(|key| { if let Ok(exchange) = key.parse() { - self.get_exchange_credentials(exchange) + self.get_exchange_credentials(exchange, "") .map(|exchange_credentials| (exchange, exchange_credentials)) } else { None diff --git a/src/main.rs b/src/main.rs index df268e1..f5ddbdc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5065,6 +5065,16 @@ async fn main() -> Result<(), Box> { .about("Exchange interactions") .setting(AppSettings::SubcommandRequiredElseHelp) .setting(AppSettings::InferSubcommands) + .arg( + Arg::with_name("exchange_account") + .long("account") + .value_name("ACCOUNT_NAME") + .takes_value(true) + .global(true) + .help("User-defined name of the exchange account to use \ + (extra help: specify this option BEFORE the subcomand name, \ + not after as indicated by --help) [default: '']"), + ) .subcommand( SubCommand::with_name("balance") .about("Get exchange balance") @@ -6225,9 +6235,13 @@ async fn main() -> Result<(), Box> { assert!(exchanges.contains(&exchange), "Bug!"); let exchange = Exchange::from_str(exchange)?; + let exchange_account = value_t!(exchange_matches, "exchange_account", String) + .ok() + .unwrap_or_default(); + let exchange_client = || { let exchange_credentials = db - .get_exchange_credentials(exchange) + .get_exchange_credentials(exchange, &exchange_account) .ok_or_else(|| format!("No API key set for {exchange:?}"))?; exchange_client_new(exchange, exchange_credentials) }; @@ -6709,28 +6723,31 @@ async fn main() -> Result<(), Box> { .await?; } ("api", Some(api_matches)) => match api_matches.subcommand() { - ("show", Some(_arg_matches)) => match db.get_exchange_credentials(exchange) { - Some(ExchangeCredentials { - api_key, - subaccount, - .. - }) => { - println!("API Key: {api_key}"); - println!("Secret: ********"); - if let Some(subaccount) = subaccount { - println!("Subaccount: {subaccount}"); + ("show", Some(_arg_matches)) => { + match db.get_exchange_credentials(exchange, &exchange_account) { + Some(ExchangeCredentials { + api_key, + subaccount, + .. + }) => { + println!("API Key: {api_key}"); + println!("Secret: ********"); + if let Some(subaccount) = subaccount { + println!("Subaccount: {subaccount}"); + } + } + None => { + println!("No API key set for {exchange:?}"); } } - None => { - println!("No API key set for {exchange:?}"); - } - }, + } ("set", Some(arg_matches)) => { let api_key = value_t_or_exit!(arg_matches, "api_key", String); let secret = value_t_or_exit!(arg_matches, "secret", String); let subaccount = value_t!(arg_matches, "subaccount", String).ok(); db.set_exchange_credentials( exchange, + &exchange_account, ExchangeCredentials { api_key, secret, @@ -6740,7 +6757,7 @@ async fn main() -> Result<(), Box> { println!("API key set for {exchange:?}"); } ("clear", Some(_arg_matches)) => { - db.clear_exchange_credentials(exchange)?; + db.clear_exchange_credentials(exchange, &exchange_account)?; println!("Cleared API key for {exchange:?}"); } _ => unreachable!(),