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
//! Local cache manager for gpack templates
//!
//! This module provides caching functionality for downloaded gpack templates.
//! The `CacheManager` handles local storage, versioning, and integrity verification
//! of cached template packs.
//!
//! ## Features
//!
//! - **Local caching**: Store downloaded packs in user cache directory
//! - **SHA256 verification**: Verify pack integrity using checksums
//! - **Version management**: Support multiple versions of the same pack
//! - **Automatic cleanup**: Remove old versions, keeping only the latest
//! - **Git integration**: Clone and checkout specific revisions
//!
//! ## Examples
//!
//! ### Creating a Cache Manager
//!
//! ```rust,no_run
//! use crate::cache::CacheManager;
//!
//! # fn main() -> crate::utils::error::Result<()> {
//! // Use default cache directory (~/.cache/ggen/gpacks)
//! let cache = CacheManager::new()?;
//!
//! // Or use a custom directory (useful for testing)
//! use std::path::PathBuf;
//! let cache = CacheManager::with_dir(PathBuf::from("/tmp/ggen-cache"))?;
//! # Ok(())
//! # }
//! ```
//!
//! ### Ensuring a Pack is Cached
//!
//! ```rust,no_run
//! use crate::cache::CacheManager;
//! use crate::registry::ResolvedPack;
//!
//! # async fn example() -> crate::utils::error::Result<()> {
//! let cache = CacheManager::new()?;
//! let resolved_pack = ResolvedPack {
//! id: "io.ggen.example".to_string(),
//! version: "1.0.0".to_string(),
//! git_url: "https://github.com/example/pack.git".to_string(),
//! git_rev: "v1.0.0".to_string(),
//! sha256: "abc123...".to_string(),
//! };
//!
//! // Download and cache the pack if not already cached
//! let cached = cache.ensure(&resolved_pack).await?;
//! println!("Cached pack at: {:?}", cached.path);
//! # Ok(())
//! # }
//! ```
//!
//! ### Listing Cached Packs
//!
//! ```rust,no_run
//! use crate::cache::CacheManager;
//!
//! # fn main() -> crate::utils::error::Result<()> {
//! let cache = CacheManager::new()?;
//! let cached_packs = cache.list_cached()?;
//!
//! for pack in cached_packs {
//! println!("{}@{}: {:?}", pack.id, pack.version, pack.path);
//! }
//! # Ok(())
//! # }
//! ```
use crate::utils::error::{Error, Result};
use git2::{FetchOptions, RemoteCallbacks, Repository};
use sha2::{Digest, Sha256};
use std::fs;
use std::path::{Path, PathBuf};
use tempfile::TempDir;
use crate::registry::ResolvedPack;
/// Local cache manager for gpacks
#[derive(Debug, Clone)]
pub struct CacheManager {
cache_dir: PathBuf,
}
/// Cached gpack information
#[derive(Debug, Clone)]
pub struct CachedPack {
pub id: String,
pub version: String,
pub path: PathBuf,
pub sha256: String,
pub manifest: Option<crate::gpack::GpackManifest>,
}
impl CacheManager {
/// Create a new cache manager
///
/// Uses the default cache directory (`~/.cache/ggen/gpacks` on Unix systems).
///
/// # Example
///
/// ```rust,no_run
/// use crate::cache::CacheManager;
///
/// # fn main() -> crate::utils::error::Result<()> {
/// let cache = CacheManager::new()?;
/// println!("Cache directory: {:?}", cache.cache_dir());
/// # Ok(())
/// # }
/// ```
pub fn new() -> Result<Self> {
let cache_dir = dirs::cache_dir()
.ok_or_else(|| Error::new("Failed to find cache directory"))?
.join("ggen")
.join("gpacks");
fs::create_dir_all(&cache_dir)
.map_err(|e| Error::with_context("Failed to create cache directory", &e.to_string()))?;
Ok(Self { cache_dir })
}
/// Create a cache manager with custom directory (for testing)
///
/// # Example
///
/// ```rust,no_run
/// use crate::cache::CacheManager;
/// use std::path::PathBuf;
///
/// # fn main() -> crate::utils::error::Result<()> {
/// let cache = CacheManager::with_dir(PathBuf::from("/tmp/ggen-cache"))?;
/// println!("Cache directory: {:?}", cache.cache_dir());
/// # Ok(())
/// # }
/// ```
pub fn with_dir(cache_dir: PathBuf) -> Result<Self> {
fs::create_dir_all(&cache_dir)
.map_err(|e| Error::with_context("Failed to create cache directory", &e.to_string()))?;
Ok(Self { cache_dir })
}
/// Get the cache directory path
///
/// # Example
///
/// ```rust,no_run
/// use crate::cache::CacheManager;
///
/// # fn main() -> crate::utils::error::Result<()> {
/// let cache = CacheManager::new()?;
/// let cache_path = cache.cache_dir();
/// println!("Cache is at: {:?}", cache_path);
/// # Ok(())
/// # }
/// ```
pub fn cache_dir(&self) -> &Path {
&self.cache_dir
}
/// Ensure a pack is cached locally
///
/// Downloads the pack from its git repository if not already cached, or
/// verifies the cached version matches the expected SHA256 checksum.
///
/// **SHA256 verification**: If `resolved_pack.sha256` is provided and not empty,
/// the cached pack's SHA256 is verified. If the checksum doesn't match, the
/// corrupted cache is automatically removed and the pack is re-downloaded.
/// If no SHA256 is provided, the cached pack is used as-is without verification.
///
/// **Automatic recovery**: If a cached pack is found but its SHA256 doesn't match
/// the expected value, the method automatically removes the corrupted cache and
/// re-downloads the pack. This ensures data integrity without manual intervention.
///
/// # Arguments
///
/// * `resolved_pack` - Pack metadata including git URL, revision, and optional SHA256
///
/// # Returns
///
/// Returns information about the cached pack, including its local path.
///
/// # Errors
///
/// Returns an error if:
/// - The pack cannot be downloaded from git
/// - The SHA256 checksum doesn't match after re-download (if provided)
/// - The cache directory cannot be accessed or created
/// - The corrupted cache cannot be removed (should not occur in normal use)
///
/// # Examples
///
/// ## Success case
///
/// ```rust,no_run
/// use crate::cache::CacheManager;
/// use crate::registry::ResolvedPack;
///
/// # async fn example() -> anyhow::Result<()> {
/// let cache = CacheManager::new()?;
/// let pack = ResolvedPack {
/// id: "io.ggen.example".to_string(),
/// version: "1.0.0".to_string(),
/// git_url: "https://github.com/example/pack.git".to_string(),
/// git_rev: "v1.0.0".to_string(),
/// sha256: "abc123...".to_string(),
/// };
///
/// let cached = cache.ensure(&pack).await?;
/// println!("Pack cached at: {:?}", cached.path);
/// # Ok(())
/// # }
/// ```
///
/// ## Error case - Invalid git URL
///
/// ```rust,no_run
/// use crate::cache::CacheManager;
/// use crate::registry::ResolvedPack;
///
/// # async fn example() -> anyhow::Result<()> {
/// let cache = CacheManager::new()?;
/// let pack = ResolvedPack {
/// id: "io.ggen.example".to_string(),
/// version: "1.0.0".to_string(),
/// git_url: "https://invalid-url-that-does-not-exist.git".to_string(),
/// git_rev: "v1.0.0".to_string(),
/// sha256: "".to_string(),
/// };
///
/// // This will fail because the git URL is invalid
/// let result = cache.ensure(&pack).await;
/// assert!(result.is_err());
/// # Ok(())
/// # }
/// ```
pub async fn ensure(&self, resolved_pack: &ResolvedPack) -> Result<CachedPack> {
let pack_dir = self
.cache_dir
.join(&resolved_pack.id)
.join(&resolved_pack.version);
// Check if already cached and valid
if pack_dir.exists() {
if let Ok(cached) = self.load_cached(&resolved_pack.id, &resolved_pack.version) {
// Verify SHA256 if provided
if !resolved_pack.sha256.is_empty() {
let actual_sha256 = self.calculate_sha256(&pack_dir)?;
if actual_sha256 == resolved_pack.sha256 {
return Ok(cached);
} else {
// SHA256 mismatch, remove and re-download
fs::remove_dir_all(&pack_dir).map_err(|e| {
Error::with_context("Failed to remove corrupted cache", &e.to_string())
})?;
}
} else {
return Ok(cached);
}
}
}
// Download the pack
self.download_pack(resolved_pack, &pack_dir).await?;
// Load and return the cached pack
self.load_cached(&resolved_pack.id, &resolved_pack.version)
}
/// Download a pack from its git repository
async fn download_pack(&self, resolved_pack: &ResolvedPack, pack_dir: &Path) -> Result<()> {
// Create parent directory
let parent_dir = pack_dir
.parent()
.ok_or_else(|| Error::new("Invalid pack path: no parent directory"))?;
fs::create_dir_all(parent_dir)
.map_err(|e| Error::with_context("Failed to create pack directory", &e.to_string()))?;
// Clone the repository
let mut fetch_options = FetchOptions::new();
let mut callbacks = RemoteCallbacks::new();
// Progress callback
callbacks.transfer_progress(|stats| {
if stats.received_objects() % 100 == 0 {
log::info!("Downloaded {} objects", stats.received_objects());
}
true
});
fetch_options.remote_callbacks(callbacks);
// Clone to temporary directory first
let temp_dir = TempDir::new().map_err(|e| {
Error::with_context("Failed to create temporary directory", &e.to_string())
})?;
let repo = Repository::clone(&resolved_pack.git_url, temp_dir.path())
.map_err(|e| Error::with_context("Failed to clone repository", &e.to_string()))?;
// Checkout specific revision
let object = repo
.revparse_single(&resolved_pack.git_rev)
.map_err(|e| Error::with_context("Failed to find revision", &e.to_string()))?;
repo.checkout_tree(&object, None)
.map_err(|e| Error::with_context("Failed to checkout revision", &e.to_string()))?;
// Move to final location
fs::rename(temp_dir.path(), pack_dir)
.map_err(|e| Error::with_context("Failed to move downloaded pack", &e.to_string()))?;
Ok(())
}
/// Load a cached pack
///
/// Returns information about a pack that is already cached locally.
///
/// # Errors
///
/// Returns an error if:
/// - The pack is not found in the cache
/// - The pack directory exists but is corrupted
/// - The manifest file cannot be read or parsed
///
/// # Examples
///
/// ## Success case
///
/// ```rust,no_run
/// use crate::cache::CacheManager;
///
/// # fn main() -> crate::utils::error::Result<()> {
/// let cache = CacheManager::new()?;
/// // Assuming pack is already cached
/// let cached = cache.load_cached("io.ggen.example", "1.0.0")?;
/// println!("Pack path: {:?}", cached.path);
/// println!("Pack SHA256: {}", cached.sha256);
/// # Ok(())
/// # }
/// ```
///
/// ## Error case - Pack not found
///
/// ```rust,no_run
/// use crate::cache::CacheManager;
///
/// # fn main() -> crate::utils::error::Result<()> {
/// let cache = CacheManager::new()?;
/// // This will fail because the pack is not cached
/// let result = cache.load_cached("nonexistent.pack", "1.0.0");
/// assert!(result.is_err());
/// # Ok(())
/// # }
/// ```
pub fn load_cached(&self, pack_id: &str, version: &str) -> Result<CachedPack> {
let pack_dir = self.cache_dir.join(pack_id).join(version);
if !pack_dir.exists() {
return Err(Error::new(&format!(
"Pack not found in cache: {}@{}",
pack_id, version
)));
}
let sha256 = self.calculate_sha256(&pack_dir)?;
// Try to load manifest
let manifest_path = pack_dir.join("gpack.toml");
let manifest = if manifest_path.exists() {
let content = fs::read_to_string(&manifest_path)
.map_err(|e| Error::with_context("Failed to read manifest", &e.to_string()))?;
Some(
toml::from_str(&content)
.map_err(|e| Error::with_context("Failed to parse manifest", &e.to_string()))?,
)
} else {
None
};
Ok(CachedPack {
id: pack_id.to_string(),
version: version.to_string(),
path: pack_dir,
sha256,
manifest,
})
}
/// Calculate SHA256 hash of a directory
fn calculate_sha256(&self, dir: &Path) -> Result<String> {
let mut hasher = Sha256::new();
// Walk directory and hash all files
for entry in walkdir::WalkDir::new(dir) {
let entry = entry.map_err(|e| {
Error::with_context("Failed to read directory entry", &e.to_string())
})?;
let path = entry.path();
if path.is_file() {
let content = fs::read(path).map_err(|e| {
Error::with_context("Failed to read file for hashing", &e.to_string())
})?;
hasher.update(&content);
}
}
Ok(format!("{:x}", hasher.finalize()))
}
/// List all cached packs
///
/// # Example
///
/// ```rust,no_run
/// use crate::cache::CacheManager;
///
/// # fn main() -> crate::utils::error::Result<()> {
/// let cache = CacheManager::new()?;
/// let cached_packs = cache.list_cached()?;
///
/// for pack in cached_packs {
/// println!("{}@{}: {:?}", pack.id, pack.version, pack.path);
/// }
/// # Ok(())
/// # }
/// ```
pub fn list_cached(&self) -> Result<Vec<CachedPack>> {
let mut packs = Vec::new();
if !self.cache_dir.exists() {
return Ok(packs);
}
for pack_entry in fs::read_dir(&self.cache_dir)
.map_err(|e| Error::with_context("Failed to read cache directory", &e.to_string()))?
{
let pack_entry = pack_entry
.map_err(|e| Error::with_context("Failed to read pack entry", &e.to_string()))?;
let pack_path = pack_entry.path();
if pack_path.is_dir() {
let pack_id = pack_entry.file_name().to_string_lossy().to_string();
// Look for version directories
for version_entry in fs::read_dir(&pack_path).map_err(|e| {
Error::with_context("Failed to read pack directory", &e.to_string())
})? {
let version_entry = version_entry.map_err(|e| {
Error::with_context("Failed to read version entry", &e.to_string())
})?;
let version_path = version_entry.path();
if version_path.is_dir() {
let version = version_entry.file_name().to_string_lossy().to_string();
if let Ok(cached) = self.load_cached(&pack_id, &version) {
packs.push(cached);
}
}
}
}
}
Ok(packs)
}
/// Remove a cached pack
///
/// # Errors
///
/// Returns an error if:
/// - The pack directory cannot be removed
/// - The cache directory cannot be accessed
///
/// # Examples
///
/// ## Success case
///
/// ```rust,no_run
/// use crate::cache::CacheManager;
///
/// # fn main() -> crate::utils::error::Result<()> {
/// let cache = CacheManager::new()?;
/// // Remove a specific version of a pack
/// cache.remove("io.ggen.example", "1.0.0")?;
/// # Ok(())
/// # }
/// ```
///
/// ## Error case - Permission denied
///
/// ```rust,no_run
/// use crate::cache::CacheManager;
///
/// # fn main() -> crate::utils::error::Result<()> {
/// let cache = CacheManager::new()?;
/// // This may fail if we don't have permission to remove the pack
/// let result = cache.remove("io.ggen.example", "1.0.0");
/// // Handle error appropriately
/// if let Err(e) = result {
/// eprintln!("Failed to remove pack: {}", e);
/// }
/// # Ok(())
/// # }
/// ```
pub fn remove(&self, pack_id: &str, version: &str) -> Result<()> {
let pack_dir = self.cache_dir.join(pack_id).join(version);
if pack_dir.exists() {
fs::remove_dir_all(&pack_dir)
.map_err(|e| Error::with_context("Failed to remove cached pack", &e.to_string()))?;
}
// Remove pack directory if empty
if let Some(pack_parent) = pack_dir.parent() {
if pack_parent.exists() && fs::read_dir(pack_parent)?.next().is_none() {
fs::remove_dir(pack_parent).map_err(|e| {
Error::with_context("Failed to remove empty pack directory", &e.to_string())
})?;
}
}
Ok(())
}
/// Clean up old versions, keeping only the latest
pub fn cleanup_old_versions(&self) -> Result<()> {
if !self.cache_dir.exists() {
return Ok(());
}
for pack_entry in fs::read_dir(&self.cache_dir)
.map_err(|e| Error::with_context("Failed to read cache directory", &e.to_string()))?
{
let pack_entry = pack_entry
.map_err(|e| Error::with_context("Failed to read pack entry", &e.to_string()))?;
let pack_path = pack_entry.path();
if pack_path.is_dir() {
let mut versions = Vec::new();
// Collect all versions
for version_entry in fs::read_dir(&pack_path).map_err(|e| {
Error::with_context("Failed to read pack directory", &e.to_string())
})? {
let version_entry = version_entry.map_err(|e| {
Error::with_context("Failed to read version entry", &e.to_string())
})?;
let version_path = version_entry.path();
if version_path.is_dir() {
let version_str = version_entry.file_name().to_string_lossy().to_string();
if let Ok(version) = semver::Version::parse(&version_str) {
versions.push((version, version_path));
}
}
}
// Sort by version and keep only the latest
versions.sort_by(|a, b| a.0.cmp(&b.0));
for (_, version_path) in versions.into_iter().rev().skip(1) {
fs::remove_dir_all(&version_path).map_err(|e| {
Error::with_context("Failed to remove old version", &e.to_string())
})?;
}
}
}
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::fs;
use tempfile::TempDir;
#[test]
fn test_cache_manager_creation() -> Result<()> {
let temp_dir = TempDir::new()
.map_err(|e| Error::with_context("Failed to create temp dir", &e.to_string()))?;
let cache_dir = temp_dir.path().to_path_buf();
let cache_manager = CacheManager::with_dir(cache_dir.clone())?;
assert_eq!(cache_manager.cache_dir(), cache_dir);
Ok(())
}
#[test]
fn test_sha256_calculation() -> Result<()> {
let temp_dir = TempDir::new()
.map_err(|e| Error::with_context("Failed to create temp dir", &e.to_string()))?;
let test_dir = temp_dir.path().join("test");
fs::create_dir_all(&test_dir)
.map_err(|e| Error::with_context("Failed to create test dir", &e.to_string()))?;
// Create test files
fs::write(test_dir.join("file1.txt"), "content1")
.map_err(|e| Error::with_context("Failed to write file1", &e.to_string()))?;
fs::write(test_dir.join("file2.txt"), "content2")
.map_err(|e| Error::with_context("Failed to write file2", &e.to_string()))?;
let cache_manager = CacheManager::with_dir(temp_dir.path().to_path_buf())?;
let sha256 = cache_manager.calculate_sha256(&test_dir)?;
assert_eq!(sha256.len(), 64);
assert!(sha256.chars().all(|c| c.is_ascii_hexdigit()));
Ok(())
}
#[test]
fn test_list_cached_empty() -> Result<()> {
let temp_dir = TempDir::new()
.map_err(|e| Error::with_context("Failed to create temp dir", &e.to_string()))?;
let cache_manager = CacheManager::with_dir(temp_dir.path().to_path_buf())?;
let cached = cache_manager.list_cached()?;
assert!(cached.is_empty());
Ok(())
}
}