refactor: clean up unused code and improve conditionals in various modules

This commit is contained in:
GW_MC
2026-07-18 04:59:42 +00:00
parent 83037f3ee2
commit 076e87695d
11 changed files with 35 additions and 81 deletions

View File

@@ -80,34 +80,6 @@ mod tests {
assert!(result.is_ok());
}
fn create_exec_file(path: &Path) {
write_file(path);
let metadata = fs::metadata(path);
assert!(metadata.is_ok());
let metadata = metadata.ok();
assert!(metadata.is_some());
let metadata = metadata.unwrap_or_else(|| unreachable!());
let mut perms = metadata.permissions();
perms.set_mode(0o755);
let result = fs::set_permissions(path, perms);
assert!(result.is_ok());
}
fn create_non_exec_file(path: &Path) {
write_file(path);
let metadata = fs::metadata(path);
assert!(metadata.is_ok());
let metadata = metadata.ok();
assert!(metadata.is_some());
let metadata = metadata.unwrap_or_else(|| unreachable!());
let mut perms = metadata.permissions();
perms.set_mode(0o644);
let result = fs::set_permissions(path, perms);
assert!(result.is_ok());
}
fn valid_tls_raw_paths(temp_dir: &TempDir) -> (PathBuf, PathBuf, PathBuf) {
let ca_path = temp_dir.path().join("ca.pem");
let cert_path = temp_dir.path().join("cert.pem");

View File

@@ -1,7 +1,3 @@
use std::sync::Arc;
use tokio::sync::Mutex;
pub mod ssh;
pub type AgentClient =
@@ -47,8 +43,6 @@ mod tests {
atomic::{AtomicBool, Ordering},
};
use tokio::sync::Mutex;
use crate::config::settings::{
GrpcSettings, LogSettings, MAuthSettings, Settings, TLSSettings,
};

View File

@@ -1,8 +1,6 @@
use std::sync::{Arc, Weak};
use std::sync::Weak;
use nxmesh_proto::{
AgentMessage, ConfigUpdate, MasterMessage, command::Command, master_message::Payload,
};
use nxmesh_proto::{ConfigUpdate, MasterMessage, command::Command, master_message::Payload};
use crate::service::master_handler::{MasterHandlerError, MessageResult};

View File

@@ -2,7 +2,7 @@ use std::sync::Arc;
use thiserror::Error;
use tokio::process::Command;
use tracing::{debug, warn};
use tracing::debug;
use crate::{config::settings::NginxSettings, service::master_handler::MasterHandlerError};
@@ -120,10 +120,7 @@ impl CommandHandler for CommandHandlerImpl {
if !output.status.success() {
let error_info = String::from_utf8_lossy(&output.stderr);
return Err(CommandHandlerError::CommandExecutionError(
std::io::Error::new(
std::io::ErrorKind::Other,
format!("Failed to reload nginx: {}", error_info.trim()),
),
std::io::Error::other(format!("Failed to reload nginx: {}", error_info.trim())),
));
}
let success_info = String::from_utf8_lossy(&output.stdout);
@@ -142,10 +139,7 @@ impl CommandHandler for CommandHandlerImpl {
if !output.status.success() {
let error_info = String::from_utf8_lossy(&output.stderr);
return Err(CommandHandlerError::CommandExecutionError(
std::io::Error::new(
std::io::ErrorKind::Other,
format!("Failed to stop nginx: {}", error_info.trim()),
),
std::io::Error::other(format!("Failed to stop nginx: {}", error_info.trim())),
));
}
let success_info = String::from_utf8_lossy(&output.stdout);
@@ -171,10 +165,10 @@ impl CommandHandler for CommandHandlerImpl {
if !output.status.success() {
let error_info = String::from_utf8_lossy(&output.stderr);
return Err(CommandHandlerError::CommandExecutionError(
std::io::Error::new(
std::io::ErrorKind::Other,
format!("Failed to validate nginx config: {}", error_info.trim()),
),
std::io::Error::other(format!(
"Failed to validate nginx config: {}",
error_info.trim()
)),
));
}
let success_info = String::from_utf8_lossy(&output.stdout);
@@ -191,10 +185,10 @@ impl CommandHandler for CommandHandlerImpl {
if !output.status.success() {
let error_info = String::from_utf8_lossy(&output.stderr);
return Err(CommandHandlerError::CommandExecutionError(
std::io::Error::new(
std::io::ErrorKind::Other,
format!("Failed to get nginx version: {}", error_info.trim()),
),
std::io::Error::other(format!(
"Failed to get nginx version: {}",
error_info.trim()
)),
));
}
@@ -211,10 +205,7 @@ impl CommandHandler for CommandHandlerImpl {
if !output.status.success() {
let error_info = String::from_utf8_lossy(&output.stderr);
return Err(CommandHandlerError::CommandExecutionError(
std::io::Error::new(
std::io::ErrorKind::Other,
format!("Failed to get nginx status: {}", error_info.trim()),
),
std::io::Error::other(format!("Failed to get nginx status: {}", error_info.trim())),
));
}

View File

@@ -2,7 +2,7 @@ use std::sync::Arc;
use fs4::tokio::AsyncFileExt;
use thiserror::Error;
use tokio::{io::AsyncWriteExt, process::Command};
use tokio::io::AsyncWriteExt;
use tracing::warn;
use crate::{config::settings::NginxSettings, service::master_handler::MasterHandlerError};
@@ -222,19 +222,19 @@ impl FsHandler for FsHandlerImpl {
let mut entries = tokio::fs::read_dir(&deployment_dir).await?;
let mut candidates: Vec<(std::path::PathBuf, std::time::SystemTime)> = Vec::new();
while let Some(entry) = entries.next_entry().await? {
if entry.file_type().await.map_or(false, |t| t.is_dir()) {
if let Ok(mtime) = entry.metadata().await.and_then(|m| m.modified()) {
if entry.file_type().await.is_ok_and(|t| t.is_dir())
&& let Ok(mtime) = entry.metadata().await.and_then(|m| m.modified())
{
candidates.push((entry.path(), mtime));
}
}
}
// sort descending by mtime (newest first)
candidates.sort_by(|a, b| b.1.cmp(&a.1));
candidates.sort_by_key(|b| std::cmp::Reverse(b.1));
for (dir, _) in &candidates {
let mut dir_entries = tokio::fs::read_dir(dir).await?;
while let Some(file) = dir_entries.next_entry().await? {
if file.file_type().await.map_or(false, |t| t.is_file()) {
if file.file_type().await.is_ok_and(|t| t.is_file()) {
let name = file.file_name().to_string_lossy().to_string();
if name == "nginx.conf" || name.ends_with(".conf") {
let path = file.path().to_string_lossy().to_string();

View File

@@ -1,7 +1,6 @@
use std::sync::Arc;
use sea_orm::DatabaseConnection;
use tonic::transport::Server;
pub mod ssh;

View File

@@ -33,11 +33,11 @@ pub async fn update_agent_handler(
Path(id): Path<uuid::Uuid>,
Json(body): Json<UpdateAgentRequest>,
) -> Result<impl IntoResponse, AppError> {
if let Some(ref name) = body.name {
if name.trim().is_empty() {
if let Some(ref name) = body.name
&& name.trim().is_empty()
{
return Err(AppError::BadRequest("name must not be empty".to_string()));
}
}
let rec = UpdateAgentRecord {
name: body.name,

View File

@@ -93,7 +93,7 @@ async fn get_config(
Path(id): Path<Uuid>,
) -> Result<Json<serde_json::Value>, AppError> {
let config = svc.get_proxy_config(id).await?;
Ok(Json(serde_json::to_value(&config.id).unwrap_or_default()))
Ok(Json(serde_json::to_value(config.id).unwrap_or_default()))
}
async fn update_config(

View File

@@ -131,7 +131,7 @@ impl CertificateService for CertificateServiceImpl {
.collect::<Vec<SanType>>(),
san_dns
.into_iter()
.map(|dns| SanType::DnsName(dns))
.map(SanType::DnsName)
.collect::<Vec<SanType>>(),
]
.concat();

View File

@@ -16,11 +16,11 @@ impl std::fmt::Display for ProxySettingRender<'_> {
if let Some(buffer) = self.setting.buffer_size {
writeln!(f, " proxy_buffer_size {};", buffer)?;
}
if self.setting.cache_enabled.unwrap_or(false) {
if let Some(zone_name) = self.cache_zone_name {
if self.setting.cache_enabled.unwrap_or(false)
&& let Some(zone_name) = self.cache_zone_name
{
writeln!(f, " proxy_cache {};", zone_name)?;
}
}
Ok(())
}
}

View File

@@ -18,11 +18,11 @@ impl std::fmt::Display for ServerBlockRender<'_> {
writeln!(f, " listen {};", self.block.listen_port)?;
}
if let Some(ref names) = self.block.server_name {
if !names.is_empty() {
if let Some(ref names) = self.block.server_name
&& !names.is_empty()
{
writeln!(f, " server_name {};", names.join(" "))?;
}
}
if let Some(cert) = self.ssl_cert {
writeln!(f, "{}", cert)?;