use clap::Parser; use serde::{Deserialize, Deserializer}; use serde_json::Value; #[derive(Debug, Parser, Deserialize)] #[command(author, version, about, long_about = None)] pub struct SysInfoCommand { #[arg(long, default_value_t = 1000)] pub single_report_max_log_line: usize, #[arg(long, default_value_t = 3)] pub frequency: u64, /// split by #[arg(long)] #[serde(deserialize_with = "deserialize_process_name")] process_name_limits: Option, #[arg(long, action = clap::ArgAction::SetTrue)] #[serde(default = "default_false")] pub enable_process_env: bool, #[arg(long, action = clap::ArgAction::SetTrue)] #[serde(default = "default_false")] pub enable_module_info: bool, #[arg(long, action = clap::ArgAction::SetTrue)] #[serde(default = "default_false")] pub only_execute_once: bool, } fn default_false() -> bool { false } fn deserialize_process_name<'de, D>(serializer: D) -> Result, D::Error> where D: Deserializer<'de>, { let value: Vec<&str> = Vec::deserialize(serializer)?; if value.is_empty() { return Ok(None); } Ok(Some(value.join(",,,"))) } impl SysInfoCommand { pub fn get_process_name_limits(&self) -> Vec { match &self.process_name_limits { Some(value) => value .split(",,,") .map(|x| x.to_owned()) .collect::>(), _ => { vec![] } } } } pub fn policy_serde_value_to_str(config: Value) -> String { tracing::debug!("policy serde value to str {config}"); let mut result = "".to_owned(); for arg in &[ format!( "--single-report-max-log-line {} ", config["single_report_max_log_line"] .as_u64() .unwrap_or(1000) ), format!("--frequency {} ", config["frequency"].as_u64().unwrap_or(3)), ] { result.push_str(arg); } let process_name_limits = config["process_name_limits"] .as_array() .unwrap() .iter() .map(|x| x.to_string()) .collect::>() .join(",,,"); if !process_name_limits.is_empty() { result.push_str(&format!("--process-name-limits {} ", process_name_limits)); } for flag in [ "enable_process_env", "enable_module_info", "only_execute_once", ] { if config[flag].as_bool().unwrap_or(false) { result.push_str(&format!("--{} ", flag.replace('_', "-"))) } } result }