Skip to main content

nuwax_cli/commands/
status.rs

1use std::sync::Arc;
2
3use crate::{app::CliApp, docker_service::health_check::HealthChecker};
4use anyhow::Result;
5use client_core::container::DockerManager;
6use tracing::{error, info, warn};
7
8/// 显示客户端版本信息(标题和基本信息)
9pub fn show_client_version() {
10    info!("🦆 Nuwax Cli ent Status");
11    info!("==================");
12    info!("📋 Basic Information:");
13    info!(
14        "   Client version: v{version}",
15        version = env!("CARGO_PKG_VERSION")
16    );
17}
18
19/// 显示服务状态(完整版本,包含基本信息)
20pub async fn run_status(app: &CliApp) -> Result<()> {
21    show_client_version();
22    run_status_details(app).await
23}
24
25/// 显示详细状态信息(不包含基本信息标题)
26pub async fn run_status_details(app: &CliApp) -> Result<()> {
27    // 继续显示其他基本信息
28    info!(
29        "   Docker service version: {version}",
30        version = app.config.get_docker_versions()
31    );
32    info!("   Configuration file: {file}", file = "config.toml");
33
34    // 显示客户端UUID
35    let client_uuid = app.database.get_or_create_client_uuid().await?;
36    info!("   Client UUID: {uuid}", uuid = client_uuid);
37
38    // 检查文件状态
39    info!("📁 File Status:");
40    let docker_compose_path = std::path::Path::new(&app.config.docker.compose_file);
41    let env_file_path = std::path::Path::new(&app.config.docker.env_file);
42
43    // 使用新的版本化路径检查服务包文件(自动查找 .zip 或 .tar.gz)
44    let current_version = &app.config.get_docker_versions();
45    let download_path = app.config.get_version_download_file_path(
46        current_version,
47        "full",
48        None, // 自动查找归档文件
49    );
50
51    if docker_compose_path.exists() {
52        info!(
53            "   ✅ Docker Compose file: {file}",
54            file = app.config.docker.compose_file
55        );
56    } else {
57        info!(
58            "   ❌ Docker Compose file: {file} (not exists)",
59            file = app.config.docker.compose_file
60        );
61    }
62
63    match &download_path {
64        Ok(path) => {
65            info!("   ✅ Service package file: {path}", path = path.display());
66        }
67        Err(e) => {
68            info!(
69                "   ❌ Service package file: (Error: {error})",
70                error = e.to_string()
71            );
72        }
73    }
74
75    // Docker服务状态
76    info!("🐳 Docker Service Status:");
77    if docker_compose_path.exists() {
78        info!("   📋 Docker Compose file ready");
79
80        // 检查具体的服务状态
81        match check_docker_services_status(docker_compose_path, env_file_path).await {
82            Ok(()) => {
83                // 状态检查成功,详细信息已在函数内部显示
84            }
85            Err(e) => {
86                warn!(
87                    "   ⚠️  Service status check failed: {error}",
88                    error = e.to_string()
89                );
90                info!("   💡 Suggested checks:");
91                info!("      - Docker is installed and running");
92                info!("      - docker-compose is available");
93                info!("      - Use 'docker-compose ps' to check status manually");
94            }
95        }
96    } else {
97        warn!("   ❌ Docker Compose file does not exist, service not initialized");
98    }
99
100    // 根据状态提供建议
101    info!("💡 Status Analysis and Suggestions:");
102
103    let has_compose = docker_compose_path.exists();
104    let has_package = download_path
105        .as_ref()
106        .ok()
107        .map(|p| p.exists())
108        .unwrap_or(false);
109
110    if !has_compose && !has_package {
111        info!("   🆕 You seem to be a first-time user");
112        info!("   📝 Suggested steps:");
113        info!("      1. nuwax-cli upgrade                  (Download Docker service package)");
114        info!("      2. nuwax-cli docker-service deploy    (Deploy and start services)");
115    } else if !has_compose && has_package {
116        info!("   📦 Service package found, but not extracted yet");
117        info!("   📝 Suggested steps:");
118        info!("      - nuwax-cli docker-service deploy  (Full deployment process)");
119        info!("      - nuwax-cli docker-service start   (Start services only)");
120    } else {
121        info!("   ✅ System files complete, all features available");
122        info!("   📝 Available commands:");
123        info!("      - nuwax-cli docker-service start/stop/restart  (Control services)");
124        info!("      - nuwax-cli upgrade                            (Upgrade services)");
125        info!("      - nuwax-cli backup                             (Create backup)");
126        info!("      - nuwax-cli check-update                       (Check client updates)");
127    }
128
129    Ok(())
130}
131
132/// 显示API配置信息
133pub async fn run_api_info(app: &CliApp) -> Result<()> {
134    let api_config = app.api_client.get_config();
135    info!("{}", api_config);
136    Ok(())
137}
138
139/// 检查Docker服务状态的内部辅助函数
140async fn check_docker_services_status(
141    compose_file_path: &std::path::Path,
142    env_file_path: &std::path::Path,
143) -> Result<()> {
144    let docker_manager = DockerManager::with_project(
145        compose_file_path.to_path_buf(),
146        env_file_path.to_path_buf(),
147        None,
148    )?;
149
150    let health_checker = HealthChecker::new(Arc::new(docker_manager));
151    let report = health_checker.health_check().await?;
152    if report.is_all_healthy() {
153        info!("   ✅ Services are running");
154    } else {
155        warn!("   ❌ Some services are not running");
156        for container in report.failed_containers().iter() {
157            error!(
158                "   ❌ {name}: {status}",
159                name = container.name,
160                status = format!("{:?}", container.status)
161            );
162        }
163    }
164
165    Ok(())
166}