Skip to content

Commit

Permalink
feat: implement selecting cache cleaing logic
Browse files Browse the repository at this point in the history
  • Loading branch information
ripytide committed Dec 8, 2024
1 parent d39d095 commit 9f41156
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
8 changes: 4 additions & 4 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub enum MainSubcommand {
Sync(SyncCommand),
Unmanaged(UnmanagedCommand),
Backends(BackendsCommand),
Cache(CacheCommand),
CleanCache(CleanCacheCommand),
}

#[derive(Args)]
Expand Down Expand Up @@ -89,8 +89,8 @@ pub struct UnmanagedCommand {}
pub struct BackendsCommand {}

#[derive(Args)]
/// Clean the cache of all the backends, or the ones specified
pub struct CacheCommand {
#[arg(short, long)]
#[command(visible_alias("e"))]
/// clean the caches of all the backends, or the just those specified
pub struct CleanCacheCommand {
pub backends: Option<Vec<String>>,
}
27 changes: 21 additions & 6 deletions src/core.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use std::fs::{self, read_to_string, File};
use std::path::Path;
use std::str::FromStr;

use color_eyre::eyre::{eyre, Context, ContextCompat};
use color_eyre::Result;
use dialoguer::Confirm;
use strum::IntoEnumIterator;
use toml_edit::{Array, DocumentMut, Item, Value};

use crate::cli::{BackendsCommand, CacheCommand};
use crate::cli::{BackendsCommand, CleanCacheCommand};
use crate::prelude::*;
use crate::review::review;

Expand Down Expand Up @@ -44,7 +45,7 @@ impl MainArguments {
MainSubcommand::Sync(sync) => sync.run(&managed, &config),
MainSubcommand::Unmanaged(unmanaged) => unmanaged.run(&managed, &config),
MainSubcommand::Backends(found_backends) => found_backends.run(&config),
MainSubcommand::Cache(backends) => backends.run(&config),
MainSubcommand::CleanCache(backends) => backends.run(&config),
}
}
}
Expand Down Expand Up @@ -191,13 +192,27 @@ impl BackendsCommand {
}
}

impl CacheCommand {
impl CleanCacheCommand {
fn run(&self, config: &Config) -> Result<()> {
for backend in AnyBackend::iter() {
println!("Cleaning cache for {backend}\n\n");
let backends = match &self.backends {
Some(backends) => {
let result = backends.iter().map(|x|
AnyBackend::from_str(x)
.or(Err(eyre!("{x:?} is not a valid backend, run `metapac backends` to see a list of valid backends")))
).collect::<Result<Vec<AnyBackend>, _>>();
result?
}
None => AnyBackend::iter().collect(),
};

for backend in backends.iter() {
log::info!("cleaning cache for {backend} backend");

backend.clean_cache(config)?
}
println!("\n\nCleaned all available backends");

log::info!("cleaned caches of backends: {backends:?}");

Ok(())
}
}
Expand Down

0 comments on commit 9f41156

Please sign in to comment.