use std::sync::Arc;
use crate::{app::CliApp, docker_service::health_check::HealthChecker};
use anyhow::Result;
use client_core::container::DockerManager;
use tracing::{error, info, warn};
pub fn show_client_version() {
info!("🦆 Nuwax Cli ent Status");
info!("==================");
info!("📋 Basic Information:");
info!(
" Client version: v{version}",
version = env!("CARGO_PKG_VERSION")
);
}
pub async fn run_status(app: &CliApp) -> Result<()> {
show_client_version();
run_status_details(app).await
}
pub async fn run_status_details(app: &CliApp) -> Result<()> {
info!(
" Docker service version: {version}",
version = app.config.get_docker_versions()
);
info!(" Configuration file: {file}", file = "config.toml");
let client_uuid = app.database.get_or_create_client_uuid().await?;
info!(" Client UUID: {uuid}", uuid = client_uuid);
info!("📁 File Status:");
let docker_compose_path = std::path::Path::new(&app.config.docker.compose_file);
let env_file_path = std::path::Path::new(&app.config.docker.env_file);
let current_version = &app.config.get_docker_versions();
let download_path = app.config.get_version_download_file_path(
current_version,
"full",
None, );
if docker_compose_path.exists() {
info!(
" ✅ Docker Compose file: {file}",
file = app.config.docker.compose_file
);
} else {
info!(
" ❌ Docker Compose file: {file} (not exists)",
file = app.config.docker.compose_file
);
}
match &download_path {
Ok(path) => {
info!(" ✅ Service package file: {path}", path = path.display());
}
Err(e) => {
info!(
" ❌ Service package file: (Error: {error})",
error = e.to_string()
);
}
}
info!("🐳 Docker Service Status:");
if docker_compose_path.exists() {
info!(" 📋 Docker Compose file ready");
match check_docker_services_status(docker_compose_path, env_file_path).await {
Ok(()) => {
}
Err(e) => {
warn!(
" ⚠️ Service status check failed: {error}",
error = e.to_string()
);
info!(" 💡 Suggested checks:");
info!(" - Docker is installed and running");
info!(" - docker-compose is available");
info!(" - Use 'docker-compose ps' to check status manually");
}
}
} else {
warn!(" ❌ Docker Compose file does not exist, service not initialized");
}
info!("💡 Status Analysis and Suggestions:");
let has_compose = docker_compose_path.exists();
let has_package = download_path
.as_ref()
.ok()
.map(|p| p.exists())
.unwrap_or(false);
if !has_compose && !has_package {
info!(" 🆕 You seem to be a first-time user");
info!(" 📝 Suggested steps:");
info!(" 1. nuwax-cli upgrade (Download Docker service package)");
info!(" 2. nuwax-cli docker-service deploy (Deploy and start services)");
} else if !has_compose && has_package {
info!(" 📦 Service package found, but not extracted yet");
info!(" 📝 Suggested steps:");
info!(" - nuwax-cli docker-service deploy (Full deployment process)");
info!(" - nuwax-cli docker-service start (Start services only)");
} else {
info!(" ✅ System files complete, all features available");
info!(" 📝 Available commands:");
info!(" - nuwax-cli docker-service start/stop/restart (Control services)");
info!(" - nuwax-cli upgrade (Upgrade services)");
info!(" - nuwax-cli backup (Create backup)");
info!(" - nuwax-cli check-update (Check client updates)");
}
Ok(())
}
pub async fn run_api_info(app: &CliApp) -> Result<()> {
let api_config = app.api_client.get_config();
info!("{}", api_config);
Ok(())
}
async fn check_docker_services_status(
compose_file_path: &std::path::Path,
env_file_path: &std::path::Path,
) -> Result<()> {
let docker_manager = DockerManager::with_project(
compose_file_path.to_path_buf(),
env_file_path.to_path_buf(),
None,
)?;
let health_checker = HealthChecker::new(Arc::new(docker_manager));
let report = health_checker.health_check().await?;
if report.is_all_healthy() {
info!(" ✅ Services are running");
} else {
warn!(" ❌ Some services are not running");
for container in report.failed_containers().iter() {
error!(
" ❌ {name}: {status}",
name = container.name,
status = format!("{:?}", container.status)
);
}
}
Ok(())
}