tidepool-gvm 0.2.2

A simple, high-performance Go version management tool written in Rust with clean architecture and minimal dependencies
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
// Go version management module
use crate::{
    downloader::Downloader,
    symlink::{create_symlink, is_symlink, read_link, remove_symlink},
    InstallRequest, ListInstalledRequest, RuntimeStatus, StatusRequest, SwitchRequest,
    UninstallRequest, VersionList,
};
use anyhow::{anyhow, Result};
use log::info;
use std::collections::HashMap;
use std::path::{Path, PathBuf};

/// Detailed information about a Go version
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, serde::Serialize, serde::Deserialize)]
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,
    /// Whether it's the current active version
    pub is_current: 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
    #[cfg(target_os = "windows")]
    pub fn extract_archive(&self, archive_path: &Path, extract_to: &Path) -> Result<()> {
        let file = std::fs::File::open(archive_path)?;
        let mut archive = zip::ZipArchive::new(file)?;

        for i in 0..archive.len() {
            let mut file = archive.by_index(i)?;

            let outpath = extract_to.join(file.name());

            if file.name().ends_with('/') {
                std::fs::create_dir_all(&outpath)?;
            } else {
                if let Some(p) = outpath.parent() {
                    if !p.exists() {
                        std::fs::create_dir_all(p)?;
                    }
                }
                let mut outfile = std::fs::File::create(&outpath)?;
                std::io::copy(&mut file, &mut outfile)?;
            }
        }

        Ok(())
    }

    /// Extract archive to specified directory
    #[cfg(not(target_os = "windows"))]
    pub fn extract_archive(&self, archive_path: &Path, extract_to: &Path) -> Result<()> {
        let file = std::fs::File::open(archive_path)?;

        let gz = flate2::read::GzDecoder::new(file);
        let mut tar = tar::Archive::new(gz);

        tar.unpack(extract_to)?;

        Ok(())
    }

    /// Switch to a specific Go version
    pub fn switch_version(&self, version: &str, base_dir: &Path) -> Result<()> {
        let version_path = base_dir.join(version);
        let current_path = base_dir.join("current");

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

        // Remove existing symlink if it exists
        if current_path.exists() {
            remove_symlink(&current_path)?;
        }

        // Create new symlink
        create_symlink(&version_path, &current_path)?;

        info!("Switched to Go version {version}");
        Ok(())
    }

    /// Get current version
    pub fn get_current_version(&self, base_dir: &Path) -> Option<String> {
        let current_path = base_dir.join("current");
        if current_path.exists() && is_symlink(&current_path) {
            if let Ok(target) = read_link(&current_path) {
                if let Some(name) = target.file_name() {
                    return name.to_str().map(|s| s.to_string());
                }
            }
        }
        None
    }

    /// Get symlink target
    pub fn get_link_target(&self, base_dir: &Path) -> Option<PathBuf> {
        let current_path = base_dir.join("current");
        if current_path.exists() && is_symlink(&current_path) {
            read_link(&current_path).ok()
        } else {
            None
        }
    }

    /// Get symlink information
    pub fn get_symlink_info(&self, base_dir: &Path) -> String {
        let current_path = base_dir.join("current");
        if current_path.exists() && is_symlink(&current_path) {
            if let Ok(target) = read_link(&current_path) {
                return format!("{} -> {}", current_path.display(), target.display());
            }
        }
        "No symlink found".to_string()
    }

    /// Install Go version
    pub async fn install(&self, request: InstallRequest) -> Result<GoVersionInfo> {
        let version = &request.version;
        let install_dir = &request.install_dir;
        let download_dir = &request.download_dir;

        // Determine platform information
        let platform = crate::platform::PlatformInfo::detect();
        let filename = platform.archive_filename(version);
        let download_url = format!("https://go.dev/dl/{filename}");
        let archive_path = download_dir.join(&filename);

        // Download if not cached
        if !archive_path.exists() {
            info!("Downloading Go {version} from {download_url}");

            let downloader = Downloader::new();

            downloader
                .download_with_simple_progress(&download_url, &archive_path, &filename)
                .await
                .map_err(|e| anyhow::anyhow!("Download failed: {}", e))?;
        }

        // Extract archive
        let version_dir = install_dir.join(version);
        if version_dir.exists() && !request.force {
            return Err(anyhow::anyhow!("Go version {} is already installed", version));
        }

        if version_dir.exists() {
            std::fs::remove_dir_all(&version_dir)
                .map_err(|e| anyhow::anyhow!("Failed to remove existing installation: {}", e))?;
        }

        // Create a temporary directory for extraction
        let temp_extract_dir = install_dir.join(format!("{version}_temp"));

        if temp_extract_dir.exists() {
            std::fs::remove_dir_all(&temp_extract_dir)
                .map_err(|e| anyhow::anyhow!("Failed to remove temp directory: {}", e))?;
        }

        std::fs::create_dir_all(&temp_extract_dir)
            .map_err(|e| anyhow::anyhow!("Failed to create temp directory: {}", e))?;

        // Extract to the temporary directory
        info!("Extracting archive to {}", temp_extract_dir.display());
        self.extract_archive(&archive_path, &temp_extract_dir)?;

        // The official Go archive extracts into a "go" directory, which we need to rename to the version number
        let extracted_go_dir = temp_extract_dir.join("go");
        if !extracted_go_dir.exists() {
            // Clean up the temporary directory
            let _ = std::fs::remove_dir_all(&temp_extract_dir);
            return Err(anyhow::anyhow!("Expected 'go' directory not found after extraction"));
        }

        // Rename the 'go' directory to the version directory
        std::fs::rename(&extracted_go_dir, &version_dir).map_err(|e| {
            anyhow::anyhow!("Failed to rename go directory to version directory: {}", e)
        })?;

        // Clean up the temporary directory
        std::fs::remove_dir_all(&temp_extract_dir)
            .map_err(|e| anyhow::anyhow!("Failed to remove temp directory: {}", e))?;

        // Verify installation - the Go binary should now be in the bin subdirectory of the version directory
        let go_binary =
            version_dir.join("bin").join(crate::platform::PlatformInfo::go_executable_name());

        if !go_binary.exists() {
            return Err(anyhow::anyhow!(
                "Go binary not found after extraction at {}",
                go_binary.display()
            ));
        }

        info!("Successfully installed Go version {version}");

        // Automatically create or switch symlink to point to the newly installed version
        let base_dir = install_dir;
        let current_path = base_dir.join("current");
        
        // Check if current symlink already exists
        let symlink_exists = current_path.exists() && is_symlink(&current_path);
        
        if symlink_exists {
            // If symlink exists, update it to point to the new version
            remove_symlink(&current_path)
                .map_err(|e| anyhow::anyhow!("Failed to remove existing symlink: {}", e))?;
            create_symlink(&version_dir, &current_path)
                .map_err(|e| anyhow::anyhow!("Failed to create symlink: {}", e))?;
            info!("Updated symlink to point to Go version {version}");
        } else {
            // If symlink doesn't exist, create it
            create_symlink(&version_dir, &current_path)
                .map_err(|e| anyhow::anyhow!("Failed to create symlink: {}", e))?;
            info!("Created symlink pointing to Go version {version}");
        }

        Ok(GoVersionInfo {
            version: version.to_string(),
            os: platform.os,
            arch: platform.arch,
            extension: platform.extension,
            filename: filename.clone(),
            download_url,
            sha256: None, // 可以在后续版本中添加校验和验证
            size: None,   // 可以在下载时获取文件大小
            is_installed: true,
            is_cached: archive_path.exists(),
            is_current: true, // 安装后自动激活
            install_path: Some(version_dir),
            cache_path: if archive_path.exists() { Some(archive_path) } else { None },
        })
    }

    /// Switch to a version
    pub fn switch_to(&self, request: SwitchRequest) -> Result<()> {
        self.switch_version(&request.version, &request.base_dir)
    }

    /// Uninstall a version
    pub fn uninstall(&self, request: UninstallRequest) -> Result<()> {
        let version = &request.version;
        let base_dir = &request.base_dir;
        let version_path = base_dir.join(version);

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

        // Check if this is the current version
        let current_path = base_dir.join("current");
        if current_path.exists() && is_symlink(&current_path) {
            if let Ok(target) = read_link(&current_path) {
                if target == version_path {
                    return Err(anyhow::anyhow!(
                        "Cannot uninstall Go {} as it is currently active. Please switch to another version first.",
                        version
                    ));
                }
            }
        }

        // Remove the version directory
        std::fs::remove_dir_all(&version_path)
            .map_err(|e| anyhow::anyhow!("Failed to remove version directory: {}", e))?;

        info!("Successfully uninstalled Go version {version}");
        Ok(())
    }

    /// List installed versions
    pub fn list_installed(&self, request: ListInstalledRequest) -> Result<VersionList> {
        let base_dir = &request.base_dir;
        let mut versions = Vec::new();

        if !base_dir.exists() {
            return Ok(VersionList { versions, total_count: 0 });
        }

        let current_version = self.get_current_version(base_dir);

        for entry in std::fs::read_dir(base_dir)
            .map_err(|e| anyhow::anyhow!("Failed to read directory: {}", e))?
        {
            let entry =
                entry.map_err(|e| anyhow::anyhow!("Failed to read directory entry: {}", e))?;
            let path = entry.path();

            if path.is_dir() && path.file_name().is_some() {
                if let Some(name) = path.file_name().and_then(|n| n.to_str()) {
                    if name != "current" {
                        let is_current = current_version.as_ref().is_some_and(|cv| cv == name);
                        versions.push(GoVersionInfo {
                            version: name.to_string(),
                            os: std::env::consts::OS.to_string(),
                            arch: std::env::consts::ARCH.to_string(),
                            extension: String::new(),
                            filename: String::new(),
                            download_url: String::new(),
                            sha256: None,
                            size: None,
                            is_installed: true,
                            is_cached: false,
                            is_current,
                            install_path: Some(path.clone()),
                            cache_path: None,
                        });
                    }
                }
            }
        }

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

        Ok(VersionList { versions, total_count })
    }

    /// List available versions
    pub fn list_available(&self) -> Result<VersionList> {
        // This is a simplified implementation
        // In a real implementation, you would fetch the list from Go's official API
        
        // 获取当前版本以便标记
        let base_dir = dirs::home_dir().unwrap_or_else(|| std::path::PathBuf::from(".")).join(".gvm").join("versions");
        let current_version = self.get_current_version(&base_dir);
        
        let mut versions = vec![
            GoVersionInfo {
                version: "1.21.3".to_string(),
                os: "linux".to_string(),
                arch: "amd64".to_string(),
                extension: "tar.gz".to_string(),
                filename: "go1.21.3.linux-amd64.tar.gz".to_string(),
                download_url: String::new(),
                sha256: None,
                size: None,
                is_installed: false,
                is_cached: false,
                is_current: false,
                install_path: None,
                cache_path: None,
            },
            GoVersionInfo {
                version: "1.21.2".to_string(),
                os: "linux".to_string(),
                arch: "amd64".to_string(),
                extension: "tar.gz".to_string(),
                filename: "go1.21.2.linux-amd64.tar.gz".to_string(),
                download_url: String::new(),
                sha256: None,
                size: None,
                is_installed: false,
                is_cached: false,
                is_current: false,
                install_path: None,
                cache_path: None,
            },
            GoVersionInfo {
                version: "1.21.1".to_string(),
                os: "linux".to_string(),
                arch: "amd64".to_string(),
                extension: "tar.gz".to_string(),
                filename: "go1.21.1.linux-amd64.tar.gz".to_string(),
                download_url: String::new(),
                sha256: None,
                size: None,
                is_installed: false,
                is_cached: false,
                is_current: false,
                install_path: None,
                cache_path: None,
            },
        ];
        
        // 标记当前版本
        if let Some(ref current) = current_version {
            for version in &mut versions {
                version.is_current = version.version == *current;
            }
        }

        let total_count = versions.len();
        Ok(VersionList { versions, total_count })
    }

    /// Get status
    pub fn status(&self, request: StatusRequest) -> Result<RuntimeStatus> {
        let base_dir = request.base_dir.unwrap_or_else(|| {
            dirs::home_dir().unwrap_or_else(|| PathBuf::from(".")).join(".gvm").join("versions")
        });

        let current_version = self.get_current_version(&base_dir);
        let mut environment_vars = HashMap::new();

        if let Some(version) = &current_version {
            let version_path = base_dir.join(version);
            if version_path.exists() {
                environment_vars.insert("GOROOT".to_string(), version_path.display().to_string());
                environment_vars.insert(
                    "PATH".to_string(),
                    format!(
                        "{};{}",
                        version_path.join("bin").display(),
                        std::env::var("PATH").unwrap_or_default()
                    ),
                );
            }
        }

        let _link_info = if base_dir.join("current").exists() {
            Some(self.get_symlink_info(&base_dir))
        } else {
            None
        };

        let _is_installed = current_version.is_some();
        Ok(RuntimeStatus {
            current_version,
            current_path: self.get_link_target(&base_dir).map(|p| p.display().to_string()),
            environment_vars,
        })
    }

    /// Get version info
    pub fn get_version_info(
        &self,
        version: &str,
        install_dir: &Path,
        cache_dir: &Path,
    ) -> Result<GoVersionInfo> {
        let platform = crate::platform::PlatformInfo::detect();
        let filename = platform.archive_filename(version);
        let download_url = format!("https://go.dev/dl/{filename}");

        let install_path = install_dir.join(version);
        let cache_path = cache_dir.join(&filename);

        Ok(GoVersionInfo {
            version: version.to_string(),
            os: platform.os,
            arch: platform.arch,
            extension: platform.extension,
            filename: filename.clone(),
            download_url,
            sha256: None,
            size: None,
            is_installed: install_path.exists(),
            is_cached: cache_path.exists(),
            is_current: false, // 这个方法不检查当前版本状态
            install_path: if install_path.exists() { Some(install_path) } else { None },
            cache_path: if cache_path.exists() { Some(cache_path) } else { None },
        })
    }
}