From c59ac23997b02f77858a253202169a80a9321257 Mon Sep 17 00:00:00 2001 From: prplV Date: Wed, 4 Jun 2025 12:26:23 +0300 Subject: [PATCH] fmt --- noxis-cli/src/cli.rs | 197 +++++++++++----------------------- noxis-cli/src/cli_error.rs | 4 +- noxis-cli/src/cli_net.rs | 24 +++-- noxis-cli/src/lib.rs | 4 +- noxis-cli/src/main.rs | 6 +- noxis-rs/src/utils.rs | 11 +- noxis-rs/src/utils/metrics.rs | 110 +++++++++---------- 7 files changed, 135 insertions(+), 221 deletions(-) diff --git a/noxis-cli/src/cli.rs b/noxis-cli/src/cli.rs index b73a197..07a0c37 100644 --- a/noxis-cli/src/cli.rs +++ b/noxis-cli/src/cli.rs @@ -5,58 +5,39 @@ use metrics_models::MetricsMode; pub struct Cli { #[arg( short, - default_value="noxis-rs.sock", - help="explicit specify of NOXIS Socket file" + default_value = "noxis-rs.sock", + help = "explicit specify of NOXIS Socket file" )] - pub socket : String, - #[command( - subcommand, - help = "to manage Noxis work", - )] - pub command : Commands, + pub socket: String, + #[command(subcommand, help = "to manage Noxis work")] + pub command: Commands, } #[derive(Debug, Subcommand, serde::Serialize, serde::Deserialize)] pub enum Commands { - #[command( - about = "To get info about current Noxis status", - )] - Status, - #[command( - about = "To start Noxis process", - )] + #[command(about = "To get info about current Noxis status")] + Status, + #[command(about = "To start Noxis process")] Start(StartAction), - #[command( - about = "To stop Noxis process", - )] + #[command(about = "To stop Noxis process")] Stop, - #[command( - about = "To restart Noxis process", - )] + #[command(about = "To restart Noxis process")] Restart(StartAction), - #[command( - about = "To get list of processes that are being monitoring", - )] + #[command(about = "To get list of processes that are being monitoring")] Processes, // process command - #[command( - about = "To manage current process that is being monitoring", - )] + #[command(about = "To manage current process that is being monitoring")] Process(ProcessCommand), - #[command( - about = "To manage config settings", - )] + #[command(about = "To manage config settings")] Config(ConfigCommand), - #[command( - about = "To inspect system metrics in restricted mode", - )] + #[command(about = "To inspect system metrics in restricted mode")] Inspect(MetricsCommand), } #[derive(Debug, Parser, serde::Serialize, serde::Deserialize)] pub struct MetricsCommand { #[command(subcommand)] - pub mode : MetricsMode, + pub mode: MetricsMode, } #[derive(Debug, Parser, serde::Serialize, serde::Deserialize)] @@ -66,151 +47,93 @@ pub struct StartAction { num_args = 1.., value_delimiter = ' ' )] - pub flags : Vec, + pub flags: Vec, } #[derive(Debug, Parser, serde::Serialize, serde::Deserialize)] pub struct ConfigCommand { #[command(subcommand)] - pub action : ConfigAction, + pub action: ConfigAction, } #[derive(Debug, Subcommand, serde::Serialize, serde::Deserialize)] pub enum ConfigAction { - #[command( - about = "To change current Noxis configuration", - )] + #[command(about = "To change current Noxis configuration")] Local(LocalConfig), - #[command( - about = "To change credentials of the remote config server", - )] + #[command(about = "To change credentials of the remote config server")] Remote, - #[command( - about = "To reset all config settings", - )] + #[command(about = "To reset all config settings")] Reset, - #[command( - about = "To get current Noxis configuration", - name = "ls" - )] + #[command(about = "To get current Noxis configuration", name = "ls")] Show(EnvConfig), } #[derive(Debug, Parser, serde::Serialize, serde::Deserialize)] pub struct EnvConfig { // flag - #[arg( - long = "env", - action, - help = "to read environment vars configuration", - )] - pub is_env : bool, + #[arg(long = "env", action, help = "to read environment vars configuration")] + pub is_env: bool, } - #[derive(Debug, Parser, serde::Serialize, serde::Deserialize)] pub struct LocalConfig { // flag - #[arg( - long = "json", - action, - help = "to read following input as JSON", - )] - pub is_json : bool, + #[arg(long = "json", action, help = "to read following input as JSON")] + pub is_json: bool, // value - #[arg( - help = "path to config file or config String (with --json flag)", - )] - pub config : String, + #[arg(help = "path to config file or config String (with --json flag)")] + pub config: String, } #[derive(Debug, Parser, serde::Serialize, serde::Deserialize)] pub struct ProcessCommand { - #[arg( - help = "name of needed process", - )] - pub process : String, - #[command( - subcommand, - help = "To get current process's status", - )] - pub action : ProcessAction, + #[arg(help = "name of needed process")] + pub process: String, + #[command(subcommand, help = "To get current process's status")] + pub action: ProcessAction, } #[derive(Debug, Subcommand, serde::Serialize, serde::Deserialize)] pub enum ProcessAction { - #[command( - about = "To get info about current process status", - )] - Status, - #[command( - about = "To start current process", - )] - Start, - #[command( - about = "To stop current process", - )] - Stop, - #[command( - about = "To freeze (hybernaze) current process", - )] - Freeze, - #[command( - about = "To unfreeze (unhybernaze) current process", - )] - Unfreeze, - #[command( - about = "To restart current process", - )] - Restart, - #[command( - about = "To get info about current process's dependencies", - )] - Deps, - #[command( - about = "To get info about current process's files-dependencies", - )] - Files, - #[command( - about = "To get info about current process's services-dependencies", - )] - Services, + #[command(about = "To get info about current process status")] + Status, + #[command(about = "To start current process")] + Start, + #[command(about = "To stop current process")] + Stop, + #[command(about = "To freeze (hybernaze) current process")] + Freeze, + #[command(about = "To unfreeze (unhybernaze) current process")] + Unfreeze, + #[command(about = "To restart current process")] + Restart, + #[command(about = "To get info about current process's dependencies")] + Deps, + #[command(about = "To get info about current process's files-dependencies")] + Files, + #[command(about = "To get info about current process's services-dependencies")] + Services, } pub mod metrics_models { #[derive(Debug, clap::Parser, serde::Serialize, serde::Deserialize)] pub enum MetricsMode { - #[command( - about = "To capture all metrics about undercontrolled system", - )] + #[command(about = "To capture all metrics about undercontrolled system")] Full, - // system - #[command( - about = "To capture general host info", - )] + // system + #[command(about = "To capture general host info")] Host, - #[command( - about = "To capture detailed CPU metrics", - )] + #[command(about = "To capture detailed CPU metrics")] Cpu, - #[command( - about = "To capture RAM metrics", - )] + #[command(about = "To capture RAM metrics")] Ram, - #[command( - about = "To capture disk environment metrics", - )] + #[command(about = "To capture disk environment metrics")] Rom, - #[command( - about = "To capture system net interfaces metrics", - )] + #[command(about = "To capture system net interfaces metrics")] Network, // processes - #[command( - about = "To capture monitoring processes metrics", - )] - Processes - // Config - } + #[command(about = "To capture monitoring processes metrics")] + Processes, // Config + } } impl Cli { @@ -220,4 +143,4 @@ impl Cli { } self } -} \ No newline at end of file +} diff --git a/noxis-cli/src/cli_error.rs b/noxis-cli/src/cli_error.rs index ef9f802..c8a2c57 100644 --- a/noxis-cli/src/cli_error.rs +++ b/noxis-cli/src/cli_error.rs @@ -11,5 +11,5 @@ pub enum NoxisCliError { #[error("Can't parse CLI struct and send as byte stream")] ToStringCliParsingParsing, #[error("Can't read Noxis response due to {0}")] - CliResponseReadError(String) -} \ No newline at end of file + CliResponseReadError(String), +} diff --git a/noxis-cli/src/cli_net.rs b/noxis-cli/src/cli_net.rs index 622e118..ddc6653 100644 --- a/noxis-cli/src/cli_net.rs +++ b/noxis-cli/src/cli_net.rs @@ -1,26 +1,30 @@ -use tokio::net::UnixStream; -use tokio::io::{AsyncWriteExt, AsyncReadExt}; -use anyhow::Result; -use super::Cli; use super::cli_error::NoxisCliError; +use super::Cli; +use anyhow::Result; +use tokio::io::{AsyncReadExt, AsyncWriteExt}; +use tokio::net::UnixStream; async fn create_us_stream(cli: &Cli) -> Result { - Ok(UnixStream::connect(&cli.socket).await.map_err(|er| NoxisCliError::NoxisDaemonMissing((&cli.socket).to_string(), er.to_string()))?) + Ok(UnixStream::connect(&cli.socket).await.map_err(|er| { + NoxisCliError::NoxisDaemonMissing((&cli.socket).to_string(), er.to_string()) + })?) } pub async fn try_send(cli: Cli) -> Result<()> { // let stream = create_us_stream(&cli).await; let mut stream = create_us_stream(&cli).await?; - let msg = serde_json::to_vec(&cli) - .map_err(|_| NoxisCliError::ToStringCliParsingParsing)?; + let msg = serde_json::to_vec(&cli).map_err(|_| NoxisCliError::ToStringCliParsingParsing)?; - stream.write_all(&msg) + stream + .write_all(&msg) .await .map_err(|_| NoxisCliError::CliPromptCanNotBeSent)?; let mut response = Vec::new(); - stream.read_to_end(&mut response).await + stream + .read_to_end(&mut response) + .await .map_err(|er| NoxisCliError::CliResponseReadError(er.to_string()))?; let response = String::from_utf8_lossy(&response); @@ -29,4 +33,4 @@ pub async fn try_send(cli: Cli) -> Result<()> { } Ok(()) -} \ No newline at end of file +} diff --git a/noxis-cli/src/lib.rs b/noxis-cli/src/lib.rs index 5515aa9..7594858 100644 --- a/noxis-cli/src/lib.rs +++ b/noxis-cli/src/lib.rs @@ -1,5 +1,5 @@ mod cli; -mod cli_net; mod cli_error; +mod cli_net; -pub use cli::*; \ No newline at end of file +pub use cli::*; diff --git a/noxis-cli/src/main.rs b/noxis-cli/src/main.rs index 2ad8ead..1168e02 100644 --- a/noxis-cli/src/main.rs +++ b/noxis-cli/src/main.rs @@ -1,14 +1,14 @@ mod cli; -mod cli_net; mod cli_error; +mod cli_net; +use anyhow::Result; use clap::Parser; use cli::Cli; use cli_net::try_send; -use anyhow::Result; #[tokio::main] -async fn main() -> Result<()>{ +async fn main() -> Result<()> { dotenv::dotenv().ok(); let cli = Cli::parse().validate_socket(); try_send(cli).await?; diff --git a/noxis-rs/src/utils.rs b/noxis-rs/src/utils.rs index 4f67ac4..86f29ab 100644 --- a/noxis-rs/src/utils.rs +++ b/noxis-rs/src/utils.rs @@ -138,10 +138,10 @@ pub mod v2 { ) } pub async fn extract_extended_procs( - config : Arc, - prcs_list : &LinkedList, - files_list : &LinkedList, - servs_list : &LinkedList, + config: Arc, + prcs_list: &LinkedList, + files_list: &LinkedList, + servs_list: &LinkedList, ) -> Vec { let mut procs = Vec::new(); for prc in config.processes.iter() { @@ -278,7 +278,8 @@ pub mod v2 { &self.prcs, &self.files, &self.services, - ).await; + ) + .await; let _ = self.bus.1.clone().send(BusMessage::Response( crate::options::structs::bus::BusMessageDirection::ToMetrics, BusMessageContentType::ProcessQuery, diff --git a/noxis-rs/src/utils/metrics.rs b/noxis-rs/src/utils/metrics.rs index 0470b61..a07b02a 100644 --- a/noxis-rs/src/utils/metrics.rs +++ b/noxis-rs/src/utils/metrics.rs @@ -60,14 +60,14 @@ pub async fn init_metrics_grubber( Err(_) => { warn!("Unrecognized Metric mode was given"); let _ = bus_sender - .send(BusMessage::Response( - BusMessageDirection::ToCli, - BusMessageContentType::Result, - Box::new(Err(anyhow::Error::msg(format!( - "Unrecognized Metric mode was given" - )))), - )) - .await; + .send(BusMessage::Response( + BusMessageDirection::ToCli, + BusMessageContentType::Result, + Box::new(Err(anyhow::Error::msg(format!( + "Unrecognized Metric mode was given" + )))), + )) + .await; } Ok(mode) => { tokio::time::sleep(tokio::time::Duration::from_millis(200)).await; @@ -98,7 +98,7 @@ pub async fn init_metrics_grubber( // inspect processes MetricsMode::Processes => { todo!(); - }, + } }; // let metric: Box = Box::new(metric); let metric = metric.serialze_into_output(); @@ -118,48 +118,37 @@ pub async fn init_metrics_grubber( if let Ok(info) = cont.downcast::() { if let ProcessesQuery::All(info) = *info { let procs: Vec<_> = info - .into_iter() - .map(|prc| ProcessExtended::from_process_query_all(&mut system, prc)) - .collect(); + .into_iter() + .map(|prc| ProcessExtended::from_process_query_all(&mut system, prc)) + .collect(); let _ = bus_sender .send(BusMessage::Response( BusMessageDirection::ToCli, BusMessageContentType::Result, - Box::>::new( - Ok(serde_json::to_string_pretty(&procs)?) - ) - ), - ) - .await; + Box::>::new(Ok(serde_json::to_string_pretty( + &procs, + )?)), + )) + .await; } else { let _ = bus_sender - .send(BusMessage::Response( - BusMessageDirection::ToCli, - BusMessageContentType::Result, - Box::new( - Err( - anyhow::Error::msg(format!( - "Unknown type was send by the Supervisor" - ) - ) - ) - ), - )) - .await; + .send(BusMessage::Response( + BusMessageDirection::ToCli, + BusMessageContentType::Result, + Box::new(Err(anyhow::Error::msg(format!( + "Unknown type was send by the Supervisor" + )))), + )) + .await; } } else { let _ = bus_sender .send(BusMessage::Response( BusMessageDirection::ToCli, BusMessageContentType::Result, - Box::new( - Err( - anyhow::Error::msg(format!( - "Unknown type was send by the Supervisor" - ) - ) - ) - ), + Box::new(Err(anyhow::Error::msg(format!( + "Unknown type was send by the Supervisor" + )))), )) .await; } @@ -552,37 +541,34 @@ impl ProcessExtended { self.virtual_mem_usage = prc.virtual_memory(); } } - pub fn from_process_query_all( - system: &mut System, - proc : processes::ProcessesAll - ) -> Self { + pub fn from_process_query_all(system: &mut System, proc: processes::ProcessesAll) -> Self { system.refresh_processes(sysinfo::ProcessesToUpdate::All, true); return if let Some(prc) = system.process(proc.pid.new_sysinfo_pid()) { let disk_usage = prc.disk_usage(); Self { - name: proc.name, - status: proc.state, - pid: proc.pid, - dependencies: proc.dependencies, - cpu_usage: prc.cpu_usage(), - ram_usage: prc.memory(), - virtual_mem_usage: prc.virtual_memory(), - disks_usage_read_bytes: disk_usage.read_bytes, - disks_usage_write_bytes: disk_usage.written_bytes, + name: proc.name, + status: proc.state, + pid: proc.pid, + dependencies: proc.dependencies, + cpu_usage: prc.cpu_usage(), + ram_usage: prc.memory(), + virtual_mem_usage: prc.virtual_memory(), + disks_usage_read_bytes: disk_usage.read_bytes, + disks_usage_write_bytes: disk_usage.written_bytes, } } else { Self { - name: proc.name, - status: proc.state, - pid: proc.pid, - dependencies: proc.dependencies, - cpu_usage: 0.0, - ram_usage: 0, - virtual_mem_usage: 0, - disks_usage_read_bytes: 0, - disks_usage_write_bytes: 0, + name: proc.name, + status: proc.state, + pid: proc.pid, + dependencies: proc.dependencies, + cpu_usage: 0.0, + ram_usage: 0, + virtual_mem_usage: 0, + disks_usage_read_bytes: 0, + disks_usage_write_bytes: 0, } - } + }; } }