nuwax-cli 1.0.123

Docker service management and upgrade CLI
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
use anyhow::{Context, Result};
use chrono::DateTime;
use rust_i18n::t;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::time::{Duration, Instant};
use tokio::io::AsyncWriteExt;
use tokio_stream::StreamExt;
use tracing::{debug, error, info, warn};

/// GitHub 仓库信息从 Cargo.toml 中的 repository 字段解析
/// 在编译时从环境变量 CARGO_PKG_REPOSITORY 中提取
fn parse_github_repo() -> (&'static str, &'static str) {
    const REPOSITORY: &str = env!("CARGO_PKG_REPOSITORY");

    // 解析 GitHub URL,支持格式: https://github.com/owner/repo
    if let Some(path) = REPOSITORY.strip_prefix("https://github.com/")
        && let Some((owner, repo)) = path.split_once('/')
    {
        // 移除可能的 .git 后缀
        let repo = repo.trim_end_matches(".git");
        return (owner, repo);
    }

    // 如果解析失败,抛出编译错误
    panic!(
        "{}",
        t!("check_update.parse_repo_error", repository = REPOSITORY)
    );
}

//cli 命令工具请求的地址 (阿里云 OSS)
pub const CLI_API_URL: &str =
    "https://nuwa-packages.oss-rg-china-mainland.aliyuncs.com/nuwax-cli/latest/latest.json";

/// 获取完整的 CLI API URL
pub fn get_cli_api_url() -> String {
    CLI_API_URL.to_string()
}

use crate::cli::CheckUpdateCommand;

/// GitHub Release API 响应结构
#[derive(Debug, Deserialize)]
pub struct GitHubRelease {
    pub tag_name: String,
    #[allow(dead_code)]
    pub name: String,
    pub body: String,
    #[allow(dead_code)]
    pub draft: bool,
    #[allow(dead_code)]
    pub prerelease: bool,
    pub published_at: String,
    #[allow(dead_code)]
    #[serde(default)]
    pub html_url: Option<String>,
    pub assets: Vec<GitHubAsset>,
}

#[derive(Debug, Deserialize)]
pub struct GitHubAsset {
    pub name: String,
    #[allow(dead_code)]
    pub size: u64,
    #[allow(dead_code)]
    pub download_count: u64,
    pub browser_download_url: String,
    #[allow(dead_code)]
    pub content_type: String,
}

/// Tauri updater API 响应结构
#[derive(Debug, Deserialize)]
pub struct TauriUpdaterResponse {
    pub version: String,
    pub notes: String,
    pub pub_date: String,
    pub platforms: HashMap<String, TauriPlatformInfo>,
}

#[derive(Debug, Deserialize)]
pub struct TauriPlatformInfo {
    pub url: String,
}

/// 版本信息
#[derive(Debug, Serialize)]
pub struct VersionInfo {
    pub current_version: String,
    pub latest_version: String,
    pub is_update_available: bool,
    pub release_notes: String,
    pub download_url: Option<String>,
    pub published_at: String,
}

/// 更新源配置
#[derive(Debug, Clone)]
pub enum UpdateSource {
    /// 版本检查服务器(优先)
    VersionServer,
    /// GitHub API(备用)
    GitHub,
}

/// 更新源管理器
pub struct UpdateSourceManager {
    sources: Vec<UpdateSource>,
}

/// 将 Tauri updater 格式转换为 GitHub Release 格式
fn convert_tauri_to_github_release(tauri_response: TauriUpdaterResponse) -> GitHubRelease {
    use tracing::debug;

    // 将平台信息转换为 assets
    let assets: Vec<GitHubAsset> = tauri_response
        .platforms
        .into_iter()
        .map(|(platform, info)| {
            // 从URL中提取文件名
            let name = info
                .url
                .split('/')
                .next_back()
                .unwrap_or(&platform)
                .to_string();

            debug!(
                "Converting platform asset: platform={}, name={}, url={}",
                platform, name, info.url
            );

            // 根据文件扩展名推断content_type
            let content_type = if name.ends_with(".tar.gz") || name.ends_with(".tgz") {
                "application/gzip".to_string()
            } else if name.ends_with(".zip") {
                "application/zip".to_string()
            } else if name.ends_with(".msi") {
                "application/x-msi".to_string()
            } else if name.ends_with(".AppImage") {
                "application/x-executable".to_string()
            } else {
                "application/octet-stream".to_string()
            };

            GitHubAsset {
                name: format!("{platform}|{name}"), // 包含平台信息以便调试
                size: 0,                            // Tauri format doesn't include size
                download_count: 0,                  // Tauri format doesn't include download count
                browser_download_url: info.url,
                content_type,
            }
        })
        .collect();

    GitHubRelease {
        tag_name: tauri_response.version.clone(),
        name: format!("Release {}", tauri_response.version),
        body: tauri_response.notes,
        draft: false,
        prerelease: false,
        published_at: tauri_response.pub_date,
        html_url: None,
        assets,
    }
}

impl UpdateSourceManager {
    /// 创建默认的更新源管理器(版本检查服务器优先,GitHub 备用)
    pub fn new() -> Self {
        Self {
            sources: vec![UpdateSource::VersionServer, UpdateSource::GitHub],
        }
    }

    /// 获取版本信息,按优先级尝试各个源
    pub async fn fetch_latest_version(&self) -> Result<GitHubRelease> {
        let mut last_error = None;

        for source in &self.sources {
            match source {
                UpdateSource::VersionServer => {
                    info!("📡 Trying version check server API...");
                    match self.fetch_from_version_server().await {
                        Ok(release) => {
                            info!("✅ Version check server API success");
                            return Ok(release);
                        }
                        Err(e) => {
                            warn!(
                                "⚠️ Version check server API failed: {error}",
                                error = e.to_string()
                            );
                            last_error = Some(e);
                        }
                    }
                }
                UpdateSource::GitHub => {
                    info!("📡 Trying GitHub API...");
                    match self.fetch_from_github().await {
                        Ok(release) => {
                            info!("✅ GitHub API success");
                            return Ok(release);
                        }
                        Err(e) => {
                            warn!("⚠️ GitHub API failed: {error}", error = e.to_string());
                            last_error = Some(e);
                        }
                    }
                }
            }
        }

        Err(last_error
            .unwrap_or_else(|| anyhow::anyhow!("{}", t!("check_update.all_sources_failed"))))
    }

    /// 从版本检查服务器获取版本信息
    async fn fetch_from_version_server(&self) -> Result<GitHubRelease> {
        let client = reqwest::Client::new();
        let url = get_cli_api_url();

        info!("📡 Checking latest version: {url}", url = url.as_str());

        let response = client
            .get(&url)
            .header("User-Agent", format!("nuwax-cli/{}", get_current_version()))
            .timeout(std::time::Duration::from_secs(10))
            .send()
            .await
            .context(t!("check_update.connect_server_failed"))?;

        if !response.status().is_success() {
            let status = response.status();
            let error_text = response.text().await.unwrap_or_default();
            return Err(anyhow::anyhow!(
                "{}",
                t!(
                    "check_update.server_api_failed",
                    status = status.to_string(),
                    error = error_text
                )
            ));
        }

        // 先尝试解析为 Tauri updater 格式
        let tauri_response: TauriUpdaterResponse = response
            .json()
            .await
            .context(t!("check_update.parse_server_response_failed"))?;
        let release = convert_tauri_to_github_release(tauri_response);
        Ok(release)
    }

    /// 从GitHub获取版本信息
    async fn fetch_from_github(&self) -> Result<GitHubRelease> {
        let repo = GitHubRepo::default();
        let client = reqwest::Client::new();
        let url = repo.latest_release_url();

        info!("📡 Checking latest version: {url}", url = url.as_str());

        let response = client
            .get(&url)
            .header("User-Agent", format!("nuwax-cli/{}", get_current_version()))
            .timeout(std::time::Duration::from_secs(15))
            .send()
            .await
            .context(t!("check_update.connect_github_failed"))?;

        if !response.status().is_success() {
            let status = response.status();
            let error_text = response.text().await.unwrap_or_default();
            return Err(anyhow::anyhow!(
                "{}",
                t!(
                    "check_update.github_api_failed",
                    status = status.to_string(),
                    error = error_text
                )
            ));
        }

        let release: GitHubRelease = response
            .json()
            .await
            .context(t!("check_update.parse_github_response_failed"))?;
        Ok(release)
    }
}

/// GitHub仓库配置
pub struct GitHubRepo {
    pub owner: String,
    pub repo: String,
}

impl GitHubRepo {
    pub fn new(owner: &str, repo: &str) -> Self {
        Self {
            owner: owner.to_string(),
            repo: repo.to_string(),
        }
    }

    /// 创建默认的仓库配置(从 Cargo.toml 读取)
    pub fn default() -> Self {
        let (owner, repo) = parse_github_repo();
        Self::new(owner, repo)
    }

    /// 获取最新release API URL
    pub fn latest_release_url(&self) -> String {
        format!(
            "https://api.github.com/repos/{}/{}/releases/latest",
            self.owner, self.repo
        )
    }
}

/// 获取当前版本
pub fn get_current_version() -> String {
    format!("v{}", env!("CARGO_PKG_VERSION"))
}

/// 从更新源获取最新版本信息
pub async fn fetch_latest_version_multi_source() -> Result<GitHubRelease> {
    let source_manager = UpdateSourceManager::new();
    source_manager.fetch_latest_version().await
}

/// 比较版本号
pub fn compare_versions(current: &str, latest: &str) -> std::cmp::Ordering {
    // 简单的版本比较,假设版本格式为 v1.2.3 或 1.2.3
    let normalize_version = |v: &str| -> String { v.trim_start_matches('v').to_string() };

    let current_norm = normalize_version(current);
    let latest_norm = normalize_version(latest);

    // 使用语义版本比较(简化版)
    let parse_version = |v: &str| -> Vec<u32> {
        v.split('.')
            .map(|s| s.parse::<u32>().unwrap_or(0))
            .collect()
    };

    let current_parts = parse_version(&current_norm);
    let latest_parts = parse_version(&latest_norm);

    current_parts.cmp(&latest_parts)
}

/// 检查更新
pub async fn check_for_updates() -> Result<VersionInfo> {
    // 添加详细的调试日志
    info!("🔍 Starting nuwax-cli update check...");

    let current_version = get_current_version();
    info!(
        "📋 Current detected version: {version}",
        version = current_version
    );
    info!("🌐 Fetching latest version info...");

    let latest_release = fetch_latest_version_multi_source().await?;
    let latest_version = latest_release.tag_name.clone();
    info!(
        "📋 Server latest version: {version}",
        version = latest_version
    );

    // 版本比较
    let comparison = compare_versions(&current_version, &latest_version);
    info!(
        "📊 Version comparison result: {result} ({current} vs {latest})",
        result = format!("{:?}", comparison),
        current = current_version,
        latest = latest_version
    );

    let is_update_available = comparison == std::cmp::Ordering::Less;
    if is_update_available {
        info!(
            "✅ New version available! Need to upgrade from {current} to {latest}",
            current = current_version,
            latest = latest_version
        );
    } else {
        info!(
            "✅ Current version is latest: {current} = {latest}",
            current = current_version,
            latest = latest_version
        );
    }

    // 查找适合当前平台的下载链接
    debug!("🔍 Finding platform-specific download package...");
    let download_url = find_platform_asset(&latest_release.assets);
    if let Some(url) = &download_url {
        debug!("📦 Found platform-specific package: {url}", url = url);
    } else {
        warn!("⚠️ No platform-specific download package found");
    }

    let version_info = VersionInfo {
        current_version,
        latest_version,
        is_update_available,
        release_notes: latest_release.body,
        download_url,
        published_at: latest_release.published_at,
    };

    info!(
        "✅ Version check complete, update available: {available}",
        available = is_update_available
    );
    Ok(version_info)
}

/// 查找适合当前平台的资源
fn find_platform_asset(assets: &[GitHubAsset]) -> Option<String> {
    let os = std::env::consts::OS;
    let arch = std::env::consts::ARCH;

    info!(
        "🖥️ Detected platform: OS={os}, ARCH={arch}",
        os = os,
        arch = arch
    );
    info!("📦 Available assets: {count}", count = assets.len());

    // 构建目标平台键(兼容 Tauri updater 格式)
    let target_platform = match (os, arch) {
        ("windows", "x86_64") => "windows-x86_64",
        ("windows", "x86") => "windows-x86",
        ("linux", "x86_64") => "linux-x86_64",
        ("linux", "aarch64") => "linux-aarch64",
        ("macos", "x86_64") => "darwin-x86_64",
        ("macos", "aarch64") => "darwin-aarch64",
        _ => return None,
    };

    info!(
        "🎯 Target platform key: {platform}",
        platform = target_platform
    );

    // 首先尝试精确匹配平台键
    for (index, asset) in assets.iter().enumerate() {
        info!(
            "📋 Checking asset[{index}]: name={name}, size={size}, url={url}",
            index = index,
            name = asset.name,
            size = asset.size,
            url = asset.browser_download_url
        );

        // 检查是否包含平台键
        if asset.name.contains(target_platform) {
            info!("✅ Found exact platform match: {name}", name = asset.name);
            return Some(asset.browser_download_url.clone());
        }
    }

    // 如果没有精确匹配,尝试从文件名匹配
    let platform_patterns = match (os, arch) {
        ("windows", "x86_64") => vec!["windows", "win64", "x86_64-pc-windows", "x64"],
        ("windows", "x86") => vec!["windows", "win32", "i686-pc-windows", "x86"],
        ("linux", "x86_64") => vec!["linux", "x86_64-unknown-linux", "x64", "amd64"],
        ("linux", "aarch64") => vec!["linux", "aarch64-unknown-linux", "arm64", "aarch64"],
        ("macos", "x86_64") => vec!["macos", "darwin", "x86_64-apple-darwin", "x64"],
        ("macos", "aarch64") => vec![
            "macos",
            "darwin",
            "aarch64-apple-darwin",
            "arm64",
            "aarch64",
        ],
        _ => vec![os, arch],
    };

    info!(
        "🎯 Platform match patterns: {patterns}",
        patterns = format!("{:?}", platform_patterns)
    );

    // 查找匹配的资源
    for (index, asset) in assets.iter().enumerate() {
        let name_lower = asset.name.to_lowercase();
        let url_lower = asset.browser_download_url.to_lowercase();

        info!(
            "🔍 Pattern match check[{index}]: name={name}, url={url}",
            index = index,
            name = asset.name,
            url = asset.browser_download_url
        );

        // 检查名称或URL是否包含平台模式
        if platform_patterns
            .iter()
            .any(|pattern| name_lower.contains(pattern) || url_lower.contains(pattern))
        {
            info!("✅ Found pattern match: {name}", name = asset.name);
            // 优先选择可执行文件
            if name_lower.contains("nuwax-cli")
                || name_lower.ends_with(".exe")
                || name_lower.ends_with(".tar.gz")
                || name_lower.ends_with(".msi")
                || name_lower.ends_with(".appimage")
            {
                info!("🎯 Selected asset: {name}", name = asset.name);
                return Some(asset.browser_download_url.clone());
            }
        }
    }

    warn!("⚠️ No matching assets found, trying to find executable...");
    // 如果没找到精确匹配,返回第一个看起来像可执行文件的资源
    for (index, asset) in assets.iter().enumerate() {
        let name = asset.name.to_lowercase();
        let is_executable = name.contains("nuwax-cli")
            || name.ends_with(".exe")
            || name.ends_with(".tar.gz")
            || name.ends_with(".msi")
            || name.ends_with(".appimage");

        info!(
            "🔍 Checking executable[{index}]: {name} -> executable: {is_executable}",
            index = index,
            name = asset.name,
            is_executable = is_executable
        );

        if is_executable {
            info!("✅ Found executable: {name}", name = asset.name);
            return Some(asset.browser_download_url.clone());
        }
    }

    warn!("❌ No executable files found");
    None
}

/// 显示版本检查结果
pub fn display_version_info(version_info: &VersionInfo) {
    info!("🦆 Nuwax CLI Version Info");
    info!(
        "Current version: {version}",
        version = version_info.current_version
    );
    info!(
        "Latest version: {version}",
        version = version_info.latest_version
    );

    if version_info.is_update_available {
        info!("✅ New version available!");
        if let Some(ref url) = version_info.download_url {
            info!("Download URL: {url}", url = url);
        }

        // 显示发布说明(截取前500字符)
        if !version_info.release_notes.is_empty() {
            let notes = if version_info.release_notes.len() > 500 {
                format!("{}...", &version_info.release_notes[..500])
            } else {
                version_info.release_notes.clone()
            };
            info!(
                "Release notes:
{notes}",
                notes = notes
            );
        }

        // 解析并显示发布时间
        if let Ok(published_time) = DateTime::parse_from_rfc3339(&version_info.published_at) {
            info!(
                "Published at: {time}",
                time = published_time.format("%Y-%m-%d %H:%M:%S")
            );
        }

        info!("💡 Use the following command to install update:");
    } else {
        info!("✅ You are using the latest version!");
    }
}

/// 检查版本并决定是否需要安装
pub async fn should_install(target_version: Option<&str>, force: bool) -> Result<(String, String)> {
    let current_version = get_current_version();

    let target_version = if let Some(version) = target_version {
        version.to_string()
    } else {
        // 获取最新版本
        let latest_release = fetch_latest_version_multi_source().await?;
        latest_release.tag_name
    };

    if !force && compare_versions(&current_version, &target_version) != std::cmp::Ordering::Less {
        return Err(anyhow::anyhow!(
            "{}",
            t!(
                "check_update.already_latest_or_higher",
                current = current_version,
                target = target_version
            )
        ));
    }

    Ok((current_version, target_version))
}

/// 下载并安装新版本
pub async fn install_release(url: &str, version: &str) -> Result<()> {
    let client = reqwest::Client::new();

    // 创建临时目录
    let temp_dir = std::env::temp_dir().join("nuwax-cli-updates");
    std::fs::create_dir_all(&temp_dir)?;

    // 确定文件名
    let default_filename = format!("nuwax-cli-{version}");
    let filename = url.split('/').next_back().unwrap_or(&default_filename);
    let download_path = temp_dir.join(filename);

    info!(
        "📥 Downloading version {version}: {url}",
        version = version,
        url = url
    );
    info!("💾 Temp save to: {path}", path = download_path.display());

    // 下载文件
    let response = client
        .get(url)
        .header("User-Agent", format!("nuwax-cli/{}", get_current_version()))
        .send()
        .await
        .context(t!("check_update.download_failed"))?;

    if !response.status().is_success() {
        return Err(anyhow::anyhow!(
            "{}",
            t!(
                "check_update.download_http_failed",
                status = response.status()
            )
        ));
    }

    let total_size = response.content_length().unwrap_or(0);
    info!("📦 File size: {size} bytes", size = total_size);

    let mut file = tokio::fs::File::create(&download_path).await?;
    let mut stream = response.bytes_stream();
    let mut downloaded: u64 = 0;
    let started_at = Instant::now();
    let mut last_log_at = Instant::now();
    let mut last_logged_downloaded: u64 = 0;

    while let Some(chunk_result) = stream.next().await {
        let chunk = chunk_result.context(t!("check_update.download_failed"))?;
        file.write_all(&chunk).await?;
        downloaded += chunk.len() as u64;

        let now = Instant::now();
        let should_log_progress = now.duration_since(last_log_at) >= Duration::from_secs(1)
            || (total_size > 0 && downloaded >= total_size);

        if should_log_progress {
            let elapsed_secs = started_at.elapsed().as_secs_f64().max(0.001);
            let avg_speed_bps = downloaded as f64 / elapsed_secs;

            let recent_elapsed_secs = now.duration_since(last_log_at).as_secs_f64().max(0.001);
            let recent_speed_bps =
                (downloaded - last_logged_downloaded) as f64 / recent_elapsed_secs;
            let speed_bps = if recent_speed_bps > 0.0 {
                recent_speed_bps
            } else {
                avg_speed_bps
            };

            if total_size > 0 {
                let progress_percent = (downloaded as f64 / total_size as f64 * 100.0).min(100.0);
                let remaining_bytes = total_size.saturating_sub(downloaded);
                let eta_secs = if speed_bps > 0.0 {
                    (remaining_bytes as f64 / speed_bps).round() as u64
                } else {
                    0
                };

                info!(
                    "📥 Download progress: {percent:.1}% ({downloaded_mb:.1}/{total_mb:.1} MB) | {speed_mb:.2} MB/s | ETA {eta}s",
                    percent = progress_percent,
                    downloaded_mb = downloaded as f64 / 1024.0 / 1024.0,
                    total_mb = total_size as f64 / 1024.0 / 1024.0,
                    speed_mb = speed_bps / 1024.0 / 1024.0,
                    eta = eta_secs
                );
            } else {
                info!(
                    "📥 Downloading... {downloaded_mb:.1} MB | {speed_mb:.2} MB/s",
                    downloaded_mb = downloaded as f64 / 1024.0 / 1024.0,
                    speed_mb = speed_bps / 1024.0 / 1024.0
                );
            }

            last_log_at = now;
            last_logged_downloaded = downloaded;
        }
    }

    file.flush().await?;

    let total_elapsed_secs = started_at.elapsed().as_secs_f64().max(0.001);
    info!(
        "✅ Downloaded {size_mb:.1} MB in {elapsed:.1}s (avg {speed_mb:.2} MB/s)",
        size_mb = downloaded as f64 / 1024.0 / 1024.0,
        elapsed = total_elapsed_secs,
        speed_mb = (downloaded as f64 / total_elapsed_secs) / 1024.0 / 1024.0
    );

    info!("✅ Download complete, starting installation...");

    // 获取当前可执行文件路径
    let current_exe = std::env::current_exe().context(t!("check_update.cannot_get_exe_path"))?;

    info!(
        "🔧 Current executable: {path}",
        path = current_exe.display()
    );

    // 处理不同文件类型的安装
    install_downloaded_file(&download_path, &current_exe, version).await?;

    // 清理临时文件
    if let Err(e) = std::fs::remove_file(&download_path) {
        warn!(
            "Failed to cleanup temp file: {error}",
            error = e.to_string()
        );
    }

    info!(
        "🎉 Installation complete! Nuwax CLI has been updated to version {version}, run 'nuwax-cli --version' to verify",
        version = version
    );
    info!("💡 Please run 'nuwax-cli auto-upgrade-deploy run' again to complete final deployment");

    Ok(())
}

/// 安装下载的文件
async fn install_downloaded_file(
    download_path: &PathBuf,
    current_exe: &PathBuf,
    version: &str,
) -> Result<()> {
    let download_name = download_path
        .file_name()
        .and_then(|n| n.to_str())
        .unwrap_or("");

    if download_name.ends_with(".tar.gz") || download_name.ends_with(".tgz") {
        // 处理压缩包
        install_from_archive(download_path, current_exe, version).await
    } else if download_name.ends_with(".exe") || download_name.contains("nuwax-cli") {
        // 直接可执行文件
        install_executable(download_path, current_exe).await
    } else {
        Err(anyhow::anyhow!(
            "{}",
            t!("check_update.unsupported_format", format = download_name)
        ))
    }
}

/// 安装可执行文件
async fn install_executable(download_path: &PathBuf, current_exe: &PathBuf) -> Result<()> {
    // 创建备份
    let backup_path = if cfg!(target_os = "windows") {
        current_exe.with_extension("exe.backup")
    } else {
        PathBuf::from(format!("{}.backup", current_exe.display()))
    };

    if let Err(e) = std::fs::copy(current_exe, &backup_path) {
        warn!("Failed to create backup: {error}", error = e.to_string());
    } else {
        info!("✅ Backup created: {path}", path = backup_path.display());
    }

    // 在 Unix 系统上设置可执行权限
    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        let mut perms = std::fs::metadata(download_path)?.permissions();
        perms.set_mode(0o755);
        std::fs::set_permissions(download_path, perms)?;
    }

    // 使用 self-replace 库进行文件替换
    info!("🔧 Replacing executable...");
    match self_replace::self_replace(download_path) {
        Ok(()) => {
            info!("✅ Executable replaced successfully");
            Ok(())
        }
        Err(e) => {
            warn!("❌ File replacement failed: {error}", error = e.to_string());

            // 尝试恢复备份
            if backup_path.exists() {
                info!("🔄 Trying to restore from backup...");
                match std::fs::copy(&backup_path, current_exe) {
                    Ok(_) => {
                        warn!("✅ Restored from backup");
                        return Err(anyhow::anyhow!(
                            "{}",
                            t!(
                                "check_update.replace_failed_restored",
                                error = e.to_string()
                            )
                        ));
                    }
                    Err(restore_err) => {
                        error!(
                            "❌ Backup restore also failed: {error}",
                            error = restore_err.to_string()
                        );
                        return Err(anyhow::anyhow!(
                            "{}",
                            t!(
                                "check_update.replace_and_restore_failed",
                                error = e.to_string(),
                                restore_error = restore_err.to_string()
                            )
                        ));
                    }
                }
            }

            Err(anyhow::anyhow!(
                "{}",
                t!("check_update.replace_failed", error = e.to_string())
            ))
        }
    }
}

/// 从压缩包安装
async fn install_from_archive(
    archive_path: &Path,
    current_exe: &PathBuf,
    _version: &str,
) -> Result<()> {
    use std::process::Command;

    let temp_dir = std::env::temp_dir().join("nuwax-cli-extract");
    std::fs::create_dir_all(&temp_dir)?;

    info!("📦 Extracting archive...");

    // 解压 tar.gz 文件
    let output = Command::new("tar")
        .args([
            "-xzf",
            &archive_path.to_string_lossy(),
            "-C",
            &temp_dir.to_string_lossy(),
        ])
        .output()
        .context(t!("check_update.extract_failed_tar"))?;

    if !output.status.success() {
        return Err(anyhow::anyhow!(
            "{}",
            t!(
                "check_update.extract_failed",
                error = String::from_utf8_lossy(&output.stderr)
            )
        ));
    }

    // 查找可执行文件
    let executable_path = find_executable_in_dir(&temp_dir)?;

    // 安装可执行文件
    install_executable(&executable_path, current_exe).await?;

    // 清理解压目录
    if let Err(e) = std::fs::remove_dir_all(&temp_dir) {
        warn!(
            "Failed to cleanup extract directory: {error}",
            error = e.to_string()
        );
    }

    Ok(())
}

/// 在目录中查找可执行文件
fn find_executable_in_dir(dir: &PathBuf) -> Result<PathBuf> {
    for entry in std::fs::read_dir(dir)? {
        let entry = entry?;
        let path = entry.path();

        if path.is_file() {
            let name = path.file_name().and_then(|n| n.to_str()).unwrap_or("");

            if name.contains("nuwax-cli") || name.ends_with(".exe") {
                return Ok(path);
            }
        }

        // 递归查找子目录
        if path.is_dir()
            && let Ok(found) = find_executable_in_dir(&path)
        {
            return Ok(found);
        }
    }

    Err(anyhow::anyhow!(
        "{}",
        t!("check_update.no_executable_in_archive")
    ))
}

/// 处理 check-update 命令
pub async fn handle_check_update_command(command: CheckUpdateCommand) -> Result<()> {
    match command {
        CheckUpdateCommand::Check => {
            info!("🔍 Checking Nuwax CLI updates...");

            match check_for_updates().await {
                Ok(version_info) => {
                    display_version_info(&version_info);
                }
                Err(e) => {
                    warn!("❌ Check update failed: {error}", error = e.to_string());
                    info!(
                        "Current version: {version}",
                        version = get_current_version()
                    );
                    info!("💡 Possible reasons:");
                    info!("   - Network connection issue");
                    info!("   - Version check server temporarily unavailable");
                    info!("   - GitHub API temporarily unavailable");
                    info!("   - Project has not released any version yet");
                    return Err(e);
                }
            }
        }

        CheckUpdateCommand::Install { version, force } => {
            info!("🚀 Starting Nuwax CLI installation...");

            // 检查是否需要安装
            let (current_version, target_version) =
                match should_install(version.as_deref(), force).await {
                    Ok(versions) => versions,
                    Err(e) => {
                        if force {
                            warn!("⚠️  {}", e);
                            info!("🔧 Continuing installation with --force flag...");
                            // 如果强制安装但没指定版本,返回错误
                            if version.is_none() {
                                return Err(anyhow::anyhow!(
                                    "{}",
                                    t!("check_update.force_needs_version")
                                ));
                            }
                            (get_current_version(), version.as_ref().unwrap().clone())
                        } else {
                            warn!("❌ {}", e);
                            return Err(e);
                        }
                    }
                };

            info!(
                "Preparing to update from {current} to {target}",
                current = current_version,
                target = target_version
            );

            // 获取指定版本的下载链接
            let download_url = if let Some(ref ver) = version {
                // 指定了版本,需要获取该版本的信息
                get_version_download_url(ver).await?
            } else {
                // 没有指定版本,获取最新版本的下载链接
                let version_info = check_for_updates().await?;
                version_info.download_url.ok_or_else(|| {
                    anyhow::anyhow!("{}", t!("check_update.no_platform_download_url"))
                })?
            };

            info!(
                "📥 Starting download and install version {version}...",
                version = target_version
            );

            match install_release(&download_url, &target_version).await {
                Ok(_) => {
                    info!("🎉 Installation successful!");
                    info!("Please restart terminal to verify installation");
                }
                Err(e) => {
                    warn!("❌ Installation failed: {error}", error = e.to_string());
                    info!("💡 Possible solutions:");
                    info!("   - Check network connection");
                    info!("   - Ensure sufficient disk space");
                    info!("   - Run with administrator privileges");
                    return Err(e);
                }
            }
        }
    }

    Ok(())
}

/// 获取指定版本的下载链接
async fn get_version_download_url(version: &str) -> Result<String> {
    // 这里应该获取指定版本的release信息
    // 为了简化,我们先使用最新版本,后续可以扩展支持获取指定版本
    let version_info = check_for_updates().await?;

    version_info.download_url.ok_or_else(|| {
        anyhow::anyhow!(
            "{}",
            t!("check_update.no_version_download_url", version = version)
        )
    })
}