1use crate::pkg::cache;
2use anyhow::{Result, anyhow};
3use colored::*;
4use std::fs;
5use std::path::PathBuf;
6
7pub fn add(files: &[PathBuf]) -> Result<()> {
8 let archive_cache_root = cache::get_archive_cache_root()?;
9 fs::create_dir_all(&archive_cache_root)?;
10
11 for file in files {
12 if !file.exists() {
13 eprintln!(
14 "{}: File not found: {}",
15 "Error".red().bold(),
16 file.display()
17 );
18 continue;
19 }
20 if !file.is_file() {
21 eprintln!("{}: Not a file: {}", "Error".red().bold(), file.display());
22 continue;
23 }
24
25 let filename = file
26 .file_name()
27 .ok_or_else(|| anyhow!("Invalid filename"))?;
28 let dest_path = archive_cache_root.join(filename);
29
30 println!("Adding {} to cache...", filename.to_string_lossy().cyan());
31 fs::copy(file, &dest_path)?;
32 }
33
34 Ok(())
35}
36
37pub fn clear(dry_run: bool) -> Result<()> {
38 crate::cmd::clean::run(dry_run)
39}
40
41pub fn list() -> Result<()> {
42 let archive_cache_root = cache::get_archive_cache_root()?;
43 if !archive_cache_root.exists() {
44 println!("Cache is empty.");
45 return Ok(());
46 }
47
48 println!("{} Archives in local cache:", "::".bold().blue());
49 let mut count = 0;
50 for entry in fs::read_dir(archive_cache_root)? {
51 let entry = entry?;
52 let path = entry.path();
53 if path.is_file() {
54 let filename = path
55 .file_name()
56 .ok_or_else(|| anyhow!("Path from read_dir has no file name: {:?}", path))?
57 .to_string_lossy();
58 let size = fs::metadata(&path)?.len();
59 println!(
60 " - {:<40} ({})",
61 filename.cyan(),
62 crate::pkg::utils::format_bytes(size)
63 );
64 count += 1;
65 }
66 }
67
68 if count == 0 {
69 println!("No archives found in cache.");
70 } else {
71 println!(
72 "
73Total: {} archives",
74 count
75 );
76 }
77
78 Ok(())
79}
80
81pub fn add_mirror(url: &str) -> Result<()> {
82 crate::pkg::config::add_cache_mirror(url)?;
83 println!("Added cache mirror '{}'.", url.cyan());
84 Ok(())
85}
86
87pub fn remove_mirror(url: &str) -> Result<()> {
88 crate::pkg::config::remove_cache_mirror(url)?;
89 println!("Removed cache mirror '{}'.", url.cyan());
90 Ok(())
91}
92
93pub fn list_mirrors() -> Result<()> {
94 let config = crate::pkg::config::read_config()?;
95 if config.cache_mirrors.is_empty() {
96 println!("No cache mirrors configured.");
97 return Ok(());
98 }
99
100 println!("{} Configured cache mirrors:", "::".bold().blue());
101 for mirror in config.cache_mirrors {
102 println!(" - {}", mirror.cyan());
103 }
104 Ok(())
105}