tidepool-version-manager 0.1.5

Version management for Tidepool toolkit
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
// Go version management module
use crate::{
    downloader::{Downloader, ProgressReporter},
    symlink::{get_symlink_target, is_symlink, remove_symlink_dir, symlink_dir},
    InstallRequest, ListInstalledRequest, RuntimeStatus, StatusRequest, SwitchRequest,
    UninstallRequest, VersionInfo, VersionList, VersionManager,
};
use log::{debug, info, warn};
use sha2::{Digest, Sha256};
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use tokio::io::AsyncReadExt;

/// Detailed information about a Go version
#[derive(Debug, Clone)]
pub struct GoVersionInfo {
    /// Version number (e.g., "1.21.0")
    pub version: String,
    /// Operating system (e.g., "linux", "windows", "darwin")
    pub os: String,
    /// Architecture (e.g., "amd64", "arm64", "386")
    pub arch: String,
    /// File extension (e.g., "tar.gz", "zip")
    pub extension: String,
    /// Complete filename (e.g., "go1.21.0.linux-amd64.tar.gz")
    pub filename: String,
    /// Download URL
    pub download_url: String,
    /// Official SHA256 checksum
    pub sha256: Option<String>,
    /// File size in bytes
    pub size: Option<u64>,
    /// Whether it's installed
    pub is_installed: bool,
    /// Whether it's cached
    pub is_cached: bool,
    /// Local installation path (if installed)
    pub install_path: Option<PathBuf>,
    /// Cache file path (if cached)
    pub cache_path: Option<PathBuf>,
}

pub struct GoManager {}

impl Default for GoManager {
    fn default() -> Self {
        Self::new()
    }
}

impl GoManager {
    #[must_use]
    pub fn new() -> Self {
        Self {}
    }
    /// Extract archive to specified directory (public method)
    ///
    /// # Errors
    ///
    /// Returns an error if:
    /// - The archive file cannot be opened
    /// - The archive format is invalid
    /// - Files cannot be extracted to the target directory
    /// - File I/O operations fail
    pub fn extract_archive(&self, archive_path: &Path, extract_to: &Path) -> Result<(), String> {
        #[cfg(target_os = "windows")]
        {
            Self::extract_zip(archive_path, extract_to)
        }

        #[cfg(not(target_os = "windows"))]
        {
            self.extract_tar_gz(archive_path, extract_to)
        }
    }
    /// 跨平台版本切换实现(统一使用 symlink 模块)
    ///
    /// # Errors
    ///
    /// Returns an error if:
    /// - The requested version directory does not exist
    /// - Unable to remove existing current version link
    /// - Unable to create new version link
    /// - System file operations fail
    pub fn switch_version(&self, version: &str, base_dir: &Path) -> Result<(), String> {
        let version_path = base_dir.join(version);
        let current_path = base_dir.join("current");

        if !version_path.exists() {
            return Err(format!("Go version {version} is not installed"));
        }

        // 验证Go安装的完整性
        #[cfg(target_os = "windows")]
        let go_binary = version_path.join("bin").join("go.exe");
        #[cfg(not(target_os = "windows"))]
        let go_binary = version_path.join("bin").join("go");

        if !go_binary.exists() {
            return Err(format!(
                "Invalid Go installation: missing go binary in {}",
                version_path.display()
            ));
        }

        debug!("Creating link for Go version {version}");

        // 如果链接路径已存在,先删除
        if current_path.exists() {
            debug!("Current link exists, removing first");
            if is_symlink(&current_path) {
                remove_symlink_dir(&current_path)
                    .map_err(|e| format!("Failed to remove existing link: {e}"))?;
            } else if current_path.is_dir() {
                std::fs::remove_dir_all(&current_path)
                    .map_err(|e| format!("Failed to remove existing directory: {e}"))?;
            } else {
                std::fs::remove_file(&current_path)
                    .map_err(|e| format!("Failed to remove existing file: {e}"))?;
            }
        }

        // 使用基础的符号链接接口
        symlink_dir(&version_path, &current_path)
            .map_err(|e| format!("Failed to create link: {e}"))?;

        info!("Successfully created link for Go version {version}");

        // 验证链接是否正确创建
        if !current_path.exists() {
            return Err("Link does not exist after creation".to_string());
        }

        if !go_binary.exists() {
            return Err("Link target is invalid: missing go binary".to_string());
        }

        debug!("Successfully created link for Go version {version}");
        Ok(())
    }
    /// 获取当前活跃的Go版本(通过符号链接)
    #[must_use]
    pub fn get_current_version(&self, base_dir: &Path) -> Option<String> {
        // 首先尝试从符号链接获取(跨平台支持)
        if let Some(target) = self.get_link_target(base_dir) {
            return target
                .file_name()
                .and_then(|name| name.to_str())
                .map(std::string::ToString::to_string);
        }

        // 最后尝试从环境变量推断
        if let Ok(goroot) = std::env::var("GOROOT") {
            let goroot_path = PathBuf::from(goroot);
            if goroot_path.starts_with(base_dir) {
                return goroot_path
                    .file_name()
                    .and_then(|name| name.to_str())
                    .map(std::string::ToString::to_string);
            }
        }

        None
    }
    /// 获取符号链接指向的目标路径(跨平台)
    #[must_use]
    pub fn get_link_target(&self, base_dir: &Path) -> Option<PathBuf> {
        let link_path = base_dir.join("current");
        get_symlink_target(&link_path)
    }

    /// 获取符号链接指向的目标路径(跨平台)
    #[must_use]
    pub fn get_symlink_target(&self, base_dir: &Path) -> Option<PathBuf> {
        self.get_link_target(base_dir)
    }

    /// 获取链接状态信息(跨平台)
    #[must_use]
    pub fn get_symlink_info(&self, base_dir: &Path) -> String {
        let link_path = base_dir.join("current");

        if !link_path.exists() {
            return "No symlink found".to_string();
        }

        if let Some(target) = get_symlink_target(&link_path) {
            return format!("Symlink: {} -> {}", link_path.display(), target.display());
        }

        "Symlink exists but target unknown".to_string()
    }

    /// 解压 ZIP 文件 (Windows)
    #[cfg(target_os = "windows")]
    fn extract_zip(zip_path: &Path, extract_to: &Path) -> Result<(), String> {
        use std::fs::File;
        use std::io::BufReader;

        let file = File::open(zip_path).map_err(|e| format!("Failed to open zip file: {e}"))?;
        let reader = BufReader::new(file);

        let mut archive =
            zip::ZipArchive::new(reader).map_err(|e| format!("Failed to read zip archive: {e}"))?;

        for i in 0..archive.len() {
            let mut file = archive
                .by_index(i)
                .map_err(|e| format!("Failed to access file in archive: {e}"))?;
            let Some(file_path) = file.enclosed_name() else { continue }; // Skip the top-level "go" directory and extract its contents directly
            let Ok(relative_path) = file_path.strip_prefix("go") else {
                continue; // Skip files not in the "go" directory
            };

            // Skip empty path (the "go" directory itself)
            if relative_path.as_os_str().is_empty() {
                continue;
            }

            let outpath = extract_to.join(relative_path);

            if file.name().ends_with('/') {
                // Directory
                std::fs::create_dir_all(&outpath)
                    .map_err(|e| format!("Failed to create directory: {e}"))?;
            } else {
                // File
                if let Some(p) = outpath.parent() {
                    if !p.exists() {
                        std::fs::create_dir_all(p)
                            .map_err(|e| format!("Failed to create parent directory: {e}"))?;
                    }
                }

                let mut outfile = File::create(&outpath)
                    .map_err(|e| format!("Failed to create output file: {e}"))?;

                std::io::copy(&mut file, &mut outfile)
                    .map_err(|e| format!("Failed to extract file: {e}"))?;
            }
        }

        Ok(())
    }

    /// 解压 tar.gz 文件 (Unix)
    #[cfg(not(target_os = "windows"))]
    fn extract_tar_gz(&self, tar_gz_path: &Path, extract_to: &Path) -> Result<(), String> {
        use flate2::read::GzDecoder;
        use std::fs::File;
        use tar::Archive;

        let file =
            File::open(tar_gz_path).map_err(|e| format!("Failed to open tar.gz file: {}", e))?;

        let gz = GzDecoder::new(file);
        let mut archive = Archive::new(gz);

        // Extract all entries
        for entry in
            archive.entries().map_err(|e| format!("Failed to read archive entries: {}", e))?
        {
            let mut entry = entry.map_err(|e| format!("Failed to read archive entry: {}", e))?;

            let entry_path =
                entry.path().map_err(|e| format!("Failed to get entry path: {}", e))?;

            // Skip the top-level "go" directory and extract its contents directly
            let relative_path = if let Ok(stripped) = entry_path.strip_prefix("go") {
                stripped
            } else {
                continue; // Skip files not in the "go" directory
            };

            // Skip empty path (the "go" directory itself)
            if relative_path.as_os_str().is_empty() {
                continue;
            }

            let target_path = extract_to.join(relative_path);

            // Create parent directories if needed
            if let Some(parent) = target_path.parent() {
                std::fs::create_dir_all(parent)
                    .map_err(|e| format!("Failed to create parent directory: {}", e))?;
            }

            // Extract the entry to the target path
            entry.unpack(&target_path).map_err(|e| format!("Failed to extract entry: {}", e))?;
        }

        Ok(())
    }
    /// 计算文件的 SHA256 哈希值
    ///
    /// # Errors
    ///
    /// Returns an error if:
    /// - The file cannot be opened
    /// - File I/O operations fail during reading
    /// - The hash calculation process fails
    pub async fn calculate_file_hash(&self, file_path: &Path) -> Result<String, String> {
        let mut file = tokio::fs::File::open(file_path)
            .await
            .map_err(|e| format!("Failed to open file for hash calculation: {e}"))?;

        let mut hasher = Sha256::new();
        let mut buffer = vec![0u8; 8192]; // 8KB 缓冲区

        loop {
            let bytes_read =
                file.read(&mut buffer).await.map_err(|e| format!("Error reading file: {e}"))?;

            if bytes_read == 0 {
                break;
            }

            hasher.update(&buffer[..bytes_read]);
        }

        let result = hasher.finalize();
        Ok(format!("{result:x}"))
    }

    /// 获取 Go 版本的官方 SHA256 校验和
    async fn get_official_checksum(
        &self,
        version: &str,
        os: &str,
        arch: &str,
        extension: &str,
    ) -> Result<String, String> {
        let client = reqwest::Client::new();
        let checksums_url = "https://go.dev/dl/?mode=json&include=all".to_string();

        debug!("Fetching official checksums: {checksums_url}");

        let response = client
            .get(&checksums_url)
            .send()
            .await
            .map_err(|e| format!("Failed to fetch checksums: {e}"))?;

        let versions: serde_json::Value =
            response.json().await.map_err(|e| format!("Failed to parse checksum data: {e}"))?;

        let filename = format!("go{version}.{os}-{arch}.{extension}");
        debug!("Looking for checksum for file: {filename}");

        // 查找对应版本和文件的校验和
        if let Some(releases) = versions.as_array() {
            for release in releases {
                if let Some(version_str) = release.get("version").and_then(|v| v.as_str()) {
                    if version_str == format!("go{version}") {
                        if let Some(files) = release.get("files").and_then(|f| f.as_array()) {
                            for file in files {
                                if let Some(file_name) =
                                    file.get("filename").and_then(|f| f.as_str())
                                {
                                    if file_name == filename {
                                        if let Some(sha256) =
                                            file.get("sha256").and_then(|s| s.as_str())
                                        {
                                            debug!("Found official checksum: {sha256}");
                                            return Ok(sha256.to_string());
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

        Err(format!("Official checksum not found for Go {version} ({filename})"))
    }

    /// 校验下载文件的完整性
    async fn verify_file_integrity(
        &self,
        file_path: &Path,
        version: &str,
        os: &str,
        arch: &str,
        extension: &str,
    ) -> Result<(), String> {
        debug!("Starting file integrity verification: {}", file_path.display());

        // 计算下载文件的哈希值
        let file_hash = self.calculate_file_hash(file_path).await?;
        debug!("File hash: {file_hash}");

        // 获取官方校验和
        let official_hash = self.get_official_checksum(version, os, arch, extension).await?;
        debug!("Official hash: {official_hash}");

        // 比较哈希值
        if file_hash.to_lowercase() == official_hash.to_lowercase() {
            info!("File integrity verification passed");
            Ok(())
        } else {
            Err(format!(
                "File integrity verification failed!\nExpected: {official_hash}\nActual: {file_hash}"
            ))
        }
    }
    /// 验证缓存文件的完整性
    /// 检查文件是否存在、非空,并且可以被读取
    #[must_use]
    pub fn validate_cache_file(&self, file_path: &Path) -> bool {
        if !file_path.exists() {
            return false;
        }

        match std::fs::metadata(file_path) {
            Ok(metadata) => {
                // 文件大小检查:至少应该有一些内容(比如大于1KB)
                if metadata.len() < 1024 {
                    return false;
                }

                // 尝试打开文件以确保它可读
                std::fs::File::open(file_path).is_ok()
            }
            Err(_) => false,
        }
    }
    /// 获取 Go 版本的详细信息
    ///
    /// # Errors
    ///
    /// Returns an error if:
    /// - Invalid version format
    /// - Network request fails
    /// - Version not available for the current platform
    /// - File size cannot be determined
    pub async fn get_version_info(
        &self,
        version: &str,
        install_dir: &Path,
        cache_dir: &Path,
    ) -> Result<GoVersionInfo, String> {
        use crate::downloader::Downloader;

        // 确定当前平台信息
        let (os, arch) = if cfg!(target_os = "windows") {
            ("windows", if cfg!(target_arch = "x86_64") { "amd64" } else { "386" })
        } else if cfg!(target_os = "macos") {
            ("darwin", if cfg!(target_arch = "x86_64") { "amd64" } else { "arm64" })
        } else {
            ("linux", if cfg!(target_arch = "x86_64") { "amd64" } else { "386" })
        };

        let extension = if cfg!(target_os = "windows") { "zip" } else { "tar.gz" };
        let filename = format!("go{version}.{os}-{arch}.{extension}");
        let download_url = format!("https://go.dev/dl/{filename}");

        // 检查是否已安装
        let install_path = install_dir.join(version);
        let is_installed = install_path.exists() && {
            let go_binary = if cfg!(target_os = "windows") {
                install_path.join("bin").join("go.exe")
            } else {
                install_path.join("bin").join("go")
            };
            go_binary.exists()
        };

        // 检查是否已缓存
        let cache_path = cache_dir.join(&filename);
        let is_cached = cache_path.exists() && self.validate_cache_file(&cache_path);

        // 获取官方校验和
        let sha256 = self.get_official_checksum(version, os, arch, extension).await.ok();

        // 获取文件大小
        let size = if is_cached {
            // 如果已缓存,从本地文件获取大小
            debug!("Getting size from cached file: {}", cache_path.display());
            std::fs::metadata(&cache_path).ok().map(|m| m.len())
        } else {
            // 如果未缓存,尝试通过网络获取文件大小
            debug!("Getting size from network for: {download_url}");
            let downloader = Downloader::new();
            match downloader.get_file_size(&download_url).await {
                Ok(size) => {
                    debug!("Successfully got file size from network: {size} bytes");
                    Some(size)
                }
                Err(e) => {
                    debug!("Failed to get file size from network: {e}");
                    None
                }
            }
        };

        Ok(GoVersionInfo {
            version: version.to_string(),
            os: os.to_string(),
            arch: arch.to_string(),
            extension: extension.to_string(),
            filename,
            download_url,
            sha256,
            size,
            is_installed,
            is_cached,
            install_path: if is_installed { Some(install_path) } else { None },
            cache_path: if is_cached { Some(cache_path) } else { None },
        })
    }
}

#[async_trait::async_trait]
impl VersionManager for GoManager {
    /// 安装指定版本的Go
    #[allow(clippy::too_many_lines)]
    async fn install(&self, request: InstallRequest) -> Result<VersionInfo, String> {
        let version = &request.version;
        let install_dir = &request.install_dir;
        let download_dir = &request.download_dir;
        let force = request.force;

        // Check if install directory exists and is accessible
        if !install_dir.exists() {
            return Err("Install directory does not exist".to_string());
        }

        if !install_dir.is_dir() {
            return Err("Install path is not a directory".to_string());
        }

        // 创建版本目录
        let version_dir = install_dir.join(version);
        if version_dir.exists() && !force {
            return Err(format!("Go version {version} is already installed"));
        }

        // 如果强制安装且版本目录存在,删除现有目录
        if force && version_dir.exists() {
            std::fs::remove_dir_all(&version_dir)
                .map_err(|e| format!("Failed to remove existing version directory: {e}"))?;
        }

        std::fs::create_dir_all(&version_dir)
            .map_err(|e| format!("Failed to create version directory: {e}"))?;

        // 构建下载URL
        let (os, arch) = if cfg!(target_os = "windows") {
            ("windows", if cfg!(target_arch = "x86_64") { "amd64" } else { "386" })
        } else if cfg!(target_os = "macos") {
            ("darwin", if cfg!(target_arch = "x86_64") { "amd64" } else { "arm64" })
        } else {
            ("linux", if cfg!(target_arch = "x86_64") { "amd64" } else { "386" })
        };

        let extension = if cfg!(target_os = "windows") { "zip" } else { "tar.gz" };
        let download_url = format!("https://go.dev/dl/go{version}.{os}-{arch}.{extension}");

        // 设置下载文件名和路径
        let archive_name = format!("go{version}.{os}-{arch}.{extension}");

        // 验证下载目录存在
        if !download_dir.exists() {
            return Err(format!(
                "Download directory does not exist: {}. Please ensure the download directory is created before installation.",
                download_dir.display()
            ));
        }

        if !download_dir.is_dir() {
            return Err(format!("Download path is not a directory: {}", download_dir.display()));
        }

        let download_path = download_dir.join(&archive_name);

        // 检查缓存文件是否已存在
        let need_download = if force {
            // 强制模式:删除现有缓存文件并重新下载
            if download_path.exists() {
                debug!("Force mode: removing existing cached file");
                std::fs::remove_file(&download_path)
                    .map_err(|e| format!("Failed to remove cached file: {e}"))?;
            }
            debug!("Force mode: downloading Go {version} from {download_url}");
            true
        } else if download_path.exists() {
            debug!("Found cached file: {}", download_path.display());

            // 使用更严格的完整性验证
            if self.validate_cache_file(&download_path) {
                let metadata = std::fs::metadata(&download_path).unwrap();
                debug!("Using valid cached file (size: {} bytes)", metadata.len());
                false
            } else {
                debug!("Cached file appears to be corrupted or incomplete, will re-download");
                true
            }
        } else {
            debug!("Downloading Go {version} from {download_url}");
            true
        };

        // 只有在需要时才下载文件
        if need_download {
            // 使用内置下载器下载
            let downloader = Downloader::new();

            // 先获取文件大小,然后创建正确大小的进度报告器
            let file_size =
                downloader.get_file_size(&download_url).await.map_err(|e| format!("{e}"))?;
            let progress_reporter = ProgressReporter::new(file_size);

            downloader
                .download(&download_url, &download_path, Some(progress_reporter))
                .await
                .map_err(|e| format!("{e}"))?;

            // 下载完成后立即校验文件完整性
            debug!("Verifying downloaded file integrity");
            self.verify_file_integrity(&download_path, version, os, arch, extension)
                .await
                .map_err(|e| {
                    // 校验失败时删除损坏的文件
                    if download_path.exists() {
                        let _ = std::fs::remove_file(&download_path);
                        warn!(
                            "Verification failed, deleted corrupted file: {}",
                            download_path.display()
                        );
                    }
                    format!("File integrity verification failed: {e}")
                })?;
        } else {
            // 即使是缓存文件,也要进行完整性校验
            debug!("Verifying cached file integrity");
            self.verify_file_integrity(&download_path, version, os, arch, extension)
                .await
                .map_err(|e| {
                    // 校验失败时删除损坏的缓存文件
                    if download_path.exists() {
                        let _ = std::fs::remove_file(&download_path);
                        warn!(
                            "Cached file verification failed, deleted: {}",
                            download_path.display()
                        );
                    }
                    format!(
                        "Cached file integrity verification failed, recommend re-downloading: {e}"
                    )
                })?;
        }

        debug!("Extracting Go {} to {}", version, version_dir.display());

        // 解压下载的文件
        self.extract_archive(&download_path, &version_dir)?;

        // 如果使用下载目录,下载完成后可以选择保留或删除压缩包
        // 这里我们保留压缩包以便下次使用(由调用方决定缓存策略)

        info!("Go {} installed successfully", version);

        // Return version info
        Ok(VersionInfo { version: version.to_string(), install_path: version_dir })
    }
    /// 切换到指定版本
    fn switch_to(&self, request: SwitchRequest) -> Result<(), String> {
        let version = &request.version;
        let base_dir = &request.base_dir;
        self.switch_version(version, base_dir)
    }

    /// 卸载指定版本
    fn uninstall(&self, request: UninstallRequest) -> Result<(), String> {
        let version = &request.version;
        let base_dir = &request.base_dir;
        let version_path = base_dir.join(version);

        if !version_path.exists() {
            return Err(format!("Go version {version} is not installed"));
        }

        // 检查要卸载的版本是否为当前活跃版本
        if let Some(current_version) = self.get_current_version(base_dir) {
            if current_version == *version {
                return Err(format!(
                    "Cannot uninstall Go {version} as it is currently active. Please switch to another version or clear the current symlink first."
                ));
            }
        }

        std::fs::remove_dir_all(&version_path)
            .map_err(|e| format!("Failed to remove Go {version}: {e}"))?;

        Ok(())
    }

    /// 列出已安装的版本
    ///
    /// 扫描基础目录中的所有子目录,返回包含有效Go安装的版本目录列表。
    /// 自动排除:
    /// - `current` 目录(符号链接)
    /// - 没有 `bin/go` 或 `bin/go.exe` 的目录
    /// - 非目录文件
    ///
    /// 返回的版本列表按字母顺序排序。
    fn list_installed(&self, request: ListInstalledRequest) -> Result<VersionList, String> {
        let base_dir = &request.base_dir;
        if !base_dir.exists() {
            return Err(format!("Base directory does not exist: {}", base_dir.display()));
        }

        let mut versions = Vec::new();

        match std::fs::read_dir(base_dir) {
            Ok(entries) => {
                for entry in entries.flatten() {
                    let name = entry.file_name().to_string_lossy().to_string();

                    // 跳过current目录(符号链接)
                    if name == "current" {
                        continue;
                    }

                    // 检查是否为目录且包含Go二进制文件
                    if entry.path().is_dir() {
                        let go_binary_name =
                            if cfg!(target_os = "windows") { "go.exe" } else { "go" };
                        let go_binary = entry.path().join("bin").join(go_binary_name);
                        if go_binary.exists() {
                            versions.push(name);
                        }
                    }
                }
            }
            Err(e) => return Err(format!("Failed to read directory: {e}")),
        }

        versions.sort();
        let total_count = versions.len();

        Ok(VersionList { versions, total_count })
    }

    /// 列出可用的版本(实时获取)
    async fn list_available(&self) -> Result<VersionList, String> {
        // 从Go官方API获取版本列表
        let url = "https://go.dev/dl/?mode=json";

        let resp = reqwest::get(url).await.map_err(|e| format!("{e}"))?;
        let releases: serde_json::Value = resp.json().await.map_err(|e| format!("{e}"))?;

        let mut versions = Vec::new();

        if let Some(array) = releases.as_array() {
            for release in array {
                if let Some(version_str) = release["version"].as_str() {
                    // 去掉 "go" 前缀
                    if let Some(version) = version_str.strip_prefix("go") {
                        versions.push(version.to_string());
                    }
                }
            }
        }

        // 只返回稳定版本,过滤掉 beta 和 rc 版本
        versions.retain(|v| !v.contains("beta") && !v.contains("rc"));

        // 倒序排列,最新版本在前
        versions.sort_by(|a, b| {
            // 简单的版本比较
            let a_parts: Vec<u32> = a.split('.').filter_map(|s| s.parse().ok()).collect();
            let b_parts: Vec<u32> = b.split('.').filter_map(|s| s.parse().ok()).collect();
            b_parts.cmp(&a_parts)
        });

        let total_count = versions.len();

        Ok(VersionList { versions, total_count })
    }

    /// 获取当前状态
    fn status(&self, request: StatusRequest) -> Result<RuntimeStatus, String> {
        let base_dir = request.base_dir.as_deref();
        let mut environment_vars = HashMap::new();

        let goroot = std::env::var("GOROOT").unwrap_or_else(|_| "Not set".to_string());
        let gopath = std::env::var("GOPATH").unwrap_or_else(|_| "Not set".to_string());

        environment_vars.insert("GOROOT".to_string(), goroot.clone());
        environment_vars.insert("GOPATH".to_string(), gopath);
        let mut current_version = None;
        let mut install_path = None;
        #[cfg(target_os = "windows")]
        let mut link_info = None;
        #[cfg(not(target_os = "windows"))]
        let link_info = None;

        if let Some(base_dir) = base_dir {
            current_version = self.get_current_version(base_dir);

            if let Some(ref version) = current_version {
                install_path = Some(base_dir.join(version));
            }
            #[cfg(target_os = "windows")]
            {
                link_info = Some(self.get_symlink_info(base_dir));
            }
        }

        Ok(RuntimeStatus { current_version, install_path, environment_vars, link_info })
    }
}