Import current project code

This commit is contained in:
Your Name
2026-11-17 09:19:28 +02:00
parent 666ef42688
commit a09c4ba2cc
8 changed files with 251 additions and 27 deletions

View File

@@ -9,15 +9,14 @@
use core::panic;
use std::{
collections::HashMap,
sync::{Arc, Mutex},
collections::{BTreeMap, HashMap}, sync::{Arc, Mutex},
};
use anyhow::bail;
use futures::future::join_all;
use mikrotik_rs::{CommandBuilder, Event, MikrotikDevice};
use mikrotik_rs::{CommandBuilder, Event, MikrotikDevice, QueryOperator, proto::word::Word::Attribute};
use crate::config::{Config, ConfigHost};
use crate::config::{Config, ConfigHost, cli::{FilterOp, MtCommand}};
lazy_static::lazy_static! {
static ref MTC: Arc<Mutex<MtCommanderInternal>> = MtCommanderInternal::new_mutex();
@@ -38,7 +37,7 @@ impl MtCommander {
pub fn run_command_on_hosts(
hosts: &[ConfigHost],
command: &str,
command: &MtCommand
) -> impl Future<
Output = anyhow::Result<
HashMap<ConfigHost, anyhow::Result<Vec<HashMap<String, Option<String>>>>>,
@@ -57,7 +56,7 @@ impl MtCommander {
}
pub async fn run_command_on_host(
host: &ConfigHost,
command: &str,
command: &MtCommand
) -> anyhow::Result<Vec<HashMap<String, Option<String>>>> {
let mtc = {
MTC.lock()
@@ -124,7 +123,7 @@ impl MtCommanderInternal {
async fn run_command(
&self,
host: &ConfigHost,
cmd: &str,
command: &MtCommand
) -> anyhow::Result<Vec<HashMap<String, Option<String>>>> {
let addr = format!("{}:{}", &host.ip, host.port(&self.cfg()));
let dev_res = MikrotikDevice::builder(&addr)
@@ -143,8 +142,36 @@ impl MtCommanderInternal {
}
};
let mut cmd = CommandBuilder::new().command(&command.cmd);
for filter in &command.filter {
cmd = match filter.op {
FilterOp::Eq => cmd.query_equal(&filter.key, &filter.value),
FilterOp::Neq => cmd
.query_equal(&filter.key, &filter.value)
.query_operations([QueryOperator::Not].into_iter()),
FilterOp::Le => cmd
.query_lt(&filter.key, &filter.value)
.query_equal(&filter.key, &filter.value)
.query_operations([QueryOperator::Or].into_iter()),
FilterOp::Ge => cmd
.query_gt(&filter.key, &filter.value)
.query_equal(&filter.key, &filter.value)
.query_operations([QueryOperator::Or].into_iter()),
FilterOp::Lt => cmd.query_lt(&filter.key, &filter.value),
FilterOp::Gt => cmd.query_gt(&filter.key, &filter.value),
}
}
for (attr_k, attr_v) in &command.attr {
cmd=cmd.attribute(attr_k, attr_v.as_deref())
}
let cmd = cmd.build();
log::debug!("Query with filters: {:?}", cmd);
let mut rx = dev
.send_command(CommandBuilder::new().command(cmd).build())
.send_command(cmd)
.await?;
let mut res = Vec::new();