wasmer_cli/commands/
cache.rs

1use crate::config::WasmerEnv;
2use anyhow::Result;
3use clap::Parser;
4use std::{fs, path::Path};
5
6#[derive(Debug, Parser)]
7/// The options for the `wasmer cache` subcommand
8pub struct Cache {
9    #[clap(flatten)]
10    env: WasmerEnv,
11    /// The operation to perform.
12    #[clap(subcommand)]
13    cmd: Cmd,
14}
15
16impl Cache {
17    /// Execute the cache command
18    pub fn execute(&self) -> Result<()> {
19        let cache_dir = self.env.cache_dir();
20
21        match self.cmd {
22            Cmd::Clean => {
23                clean(cache_dir)?;
24            }
25            Cmd::Dir => {
26                println!("{}", self.env.cache_dir().display());
27            }
28        }
29
30        Ok(())
31    }
32}
33
34#[derive(Debug, Copy, Clone, Parser)]
35enum Cmd {
36    /// Clear the cache
37    Clean,
38    /// Display the location of the cache
39    Dir,
40}
41
42fn clean(cache_dir: &Path) -> Result<()> {
43    if cache_dir.exists() {
44        fs::remove_dir_all(cache_dir)?;
45    }
46    fs::create_dir_all(cache_dir)?;
47    eprintln!("Wasmer cache cleaned successfully.");
48
49    Ok(())
50}