Account manager
This commit is contained in:
+57
-8
@@ -2,21 +2,27 @@ use std::sync::Arc;
|
||||
|
||||
use tokio::{sync::Mutex, task::JoinHandle};
|
||||
|
||||
use crate::{store::config::Config, terminal::TerminalServer};
|
||||
use crate::{
|
||||
store::{accounts::AccountList, config::Config},
|
||||
terminal::TerminalServer,
|
||||
};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Engine {
|
||||
pub terminal_server: Arc<TerminalServer>,
|
||||
pub config: Arc<Mutex<Config>>,
|
||||
pub accounts: Arc<Mutex<AccountList>>,
|
||||
}
|
||||
|
||||
impl Engine {
|
||||
pub async fn new() -> tokio::io::Result<Arc<Self>> {
|
||||
let config = Arc::new(Mutex::new(Config::new().await?));
|
||||
let accounts = Arc::new(Mutex::new(AccountList::new().await?));
|
||||
|
||||
Ok(Arc::new_cyclic(|engine| Self {
|
||||
terminal_server: TerminalServer::new(engine.clone()),
|
||||
config,
|
||||
accounts,
|
||||
}))
|
||||
}
|
||||
|
||||
@@ -78,15 +84,15 @@ impl Engine {
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn invalid_command_usage(&self, name: &str) -> tokio::io::Result<()> {
|
||||
self.terminal_server.error(name, "Invalid usage").await
|
||||
}
|
||||
|
||||
pub async fn execute_command(&self, command: &str, args: Vec<&str>) -> tokio::io::Result<()> {
|
||||
match command {
|
||||
"config" => {
|
||||
const MESSAGE: &str = "Invalid command arguments, usage: config <reload>";
|
||||
|
||||
"config" | "cfg" => {
|
||||
if args.len() != 1 {
|
||||
self.terminal_server.error("config", MESSAGE).await?;
|
||||
|
||||
return Ok(());
|
||||
return self.invalid_command_usage("config").await;
|
||||
}
|
||||
|
||||
match args[0] {
|
||||
@@ -95,7 +101,50 @@ impl Engine {
|
||||
}
|
||||
|
||||
_ => {
|
||||
self.terminal_server.error("config", MESSAGE).await?;
|
||||
self.terminal_server
|
||||
.error("config", "Invalid usage")
|
||||
.await?;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
"account" | "acc" => {
|
||||
if args.len() == 0 {
|
||||
return self.invalid_command_usage("account man").await;
|
||||
}
|
||||
|
||||
match args[0] {
|
||||
"reload" => {
|
||||
*self.accounts.lock().await = AccountList::new().await?;
|
||||
}
|
||||
|
||||
"list" | "ls" => {
|
||||
self.terminal_server
|
||||
.info("account man", "ACCOUNT LIST")
|
||||
.await?;
|
||||
|
||||
let accounts = self.accounts.lock().await;
|
||||
|
||||
for (name, acc) in &accounts.accounts {
|
||||
self.terminal_server
|
||||
.info(
|
||||
"account man",
|
||||
&if name == &accounts.active {
|
||||
format!(
|
||||
"{} (active) -> {}",
|
||||
name,
|
||||
acc.get_truncated_address()
|
||||
)
|
||||
} else {
|
||||
format!("{} -> {}", name, acc.get_truncated_address())
|
||||
},
|
||||
)
|
||||
.await?;
|
||||
}
|
||||
}
|
||||
|
||||
_ => {
|
||||
return self.invalid_command_usage("account man").await;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user