better commands

This commit is contained in:
2026-08-02 02:36:23 +02:00
parent 11aff3bcb2
commit e61aa231ea
+89 -69
View File
@@ -1,7 +1,6 @@
use crate::{engine::Engine, store::config::ConfigManager}; use crate::{engine::Engine, store::config::ConfigManager};
use pulse_sdk::prelude::*; use pulse_sdk::prelude::*;
use toml::Value;
impl Engine { impl Engine {
pub async fn invalid_command_usage(&self, name: &str) -> tokio::io::Result<()> { pub async fn invalid_command_usage(&self, name: &str) -> tokio::io::Result<()> {
@@ -13,7 +12,43 @@ impl Engine {
command: &str, command: &str,
mut args: Vec<&str>, mut args: Vec<&str>,
) -> tokio::io::Result<()> { ) -> tokio::io::Result<()> {
macro_rules! set_cfg {
($n:ident) => {
let Ok(Ok($n)) = toml::value::Value::try_from(args[2]).map(|v| v.try_into()) else {
return self
.terminal_server
.error("config::set", "Unable to parse value")
.await;
};
};
}
match command { match command {
"help" | "?" => {
self.terminal_server
.info("engine::help", "AVAILABLE COMMANDS")
.await?;
let commands = [
("help", "Show this help message"),
("config reload", "Reload configuration from disk"),
("config save", "Save current configuration"),
("account list", "List available accounts"),
("account use <name>", "Switch active account"),
("preset list", "List available presets"),
("preset use <name>", "Switch active preset"),
("strategy start", "Start strategy runtime"),
("strategy set <id>", "Change active strategy"),
("strategy <command>", "Send command to strategy runtime"),
];
for (command, description) in commands {
self.terminal_server
.info("engine::help", &format!("{:<30} {}", command, description))
.await?;
}
}
"config" | "cfg" => { "config" | "cfg" => {
if args.len() == 0 { if args.len() == 0 {
return self.invalid_command_usage("config").await; return self.invalid_command_usage("config").await;
@@ -21,10 +56,23 @@ impl Engine {
match args[0] { match args[0] {
"reload" => { "reload" => {
*self.config.lock().await = ConfigManager::new().await?; let mut config = self.config.lock().await;
let new_config = ConfigManager::new().await?;
let strategy_changed =
new_config.get_ref()?.strategy != config.get_ref()?.strategy;
*config = new_config;
self.terminal_server self.terminal_server
.info("engine::config", "successfully reloaded") .info("engine::config", "successfully reloaded")
.await?; .await?;
if strategy_changed {
self.strategy_engine
.reload(&config.get_ref()?.strategy)
.await?;
}
} }
"save" => { "save" => {
@@ -34,72 +82,6 @@ impl Engine {
.await?; .await?;
} }
"set" => {
macro_rules! set_cfg {
($n:ident, $v:expr) => {
if let Ok(Ok($n)) = Value::try_from(args[2]).map(|v| v.try_into()) {
$v;
} else {
self.terminal_server
.error("config::set", "Unable to parse value")
.await?;
}
};
}
if args.len() == 3 {
match args[1] {
"watchlist" | "watch" | "wl" => {
set_cfg!(v, self.config.lock().await.get_mut()?.watchlist = v);
self.terminal_server
.info("config::set", "watchlist set successfully, use `config save` to persist changes")
.await?;
}
"strategy" | "strat" | "sg" => {
set_cfg!(id, {
let id: String = id;
if !crate::store::pulse_strategy(&id)?
.join("strategy.toml")
.exists()
{
return self
.terminal_server
.error(
"config::set[strategy]",
&format!("Non existent strategy `{id}`"),
)
.await;
}
self.strategy_engine.reload(id.as_str()).await?;
self.config.lock().await.get_mut()?.strategy = id;
});
self.terminal_server
.info("config::set", "strategy set successfully, use `config save` to persist changes")
.await?;
}
_ => {
self.terminal_server
.error("config::set", "Invalid usage, available options: watchlist, strategy, cooldown")
.await?;
}
}
self.terminal_server
.broadcast(TerminalServerMessage::ConfigUpdated(
self.get_config_status().await?,
))
.await?;
} else {
self.invalid_command_usage("engine::config").await?;
}
}
_ => { _ => {
self.invalid_command_usage("engine::config").await?; self.invalid_command_usage("engine::config").await?;
} }
@@ -254,6 +236,35 @@ impl Engine {
match strategy_command { match strategy_command {
"start" => {} "start" => {}
"set" => {
set_cfg!(id);
let id: String = id;
if !crate::store::pulse_strategy(&id)?
.join("strategy.toml")
.exists()
{
return self
.terminal_server
.error(
"config::set[strategy]",
&format!("Non existent strategy `{id}`"),
)
.await;
}
self.strategy_engine.reload(id.as_str()).await?;
self.config.lock().await.get_mut()?.strategy = id;
self.terminal_server
.info(
"config::set",
"strategy set successfully, use `config save` to persist changes",
)
.await?;
}
_ => { _ => {
self.strategy_engine self.strategy_engine
.send(&StrategyEngineMessage::Command { .send(&StrategyEngineMessage::Command {
@@ -267,11 +278,20 @@ impl Engine {
_ => { _ => {
self.terminal_server self.terminal_server
.error("engine::cmd", &format!("command '{}' not found", command)) .error(
"engine::cmd",
&format!("command '{}' not found, use help or ?", command),
)
.await?; .await?;
} }
} }
self.terminal_server
.broadcast(TerminalServerMessage::ConfigUpdated(
self.get_config_status().await?,
))
.await?;
Ok(()) Ok(())
} }
} }