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
use crate::config::WasmerEnv;
use anyhow::Result;
use clap::Parser;
use std::{fs, path::Path};

#[derive(Debug, Parser)]
/// The options for the `wasmer cache` subcommand
pub struct Cache {
    #[clap(flatten)]
    env: WasmerEnv,
    /// The operation to perform.
    #[clap(subcommand)]
    cmd: Cmd,
}

impl Cache {
    /// Execute the cache command
    pub fn execute(&self) -> Result<()> {
        let cache_dir = self.env.cache_dir();

        match self.cmd {
            Cmd::Clean => {
                clean(cache_dir)?;
            }
            Cmd::Dir => {
                println!("{}", self.env.cache_dir().display());
            }
        }

        Ok(())
    }
}

#[derive(Debug, Copy, Clone, Parser)]
enum Cmd {
    /// Clear the cache
    Clean,
    /// Display the location of the cache
    Dir,
}

fn clean(cache_dir: &Path) -> Result<()> {
    if cache_dir.exists() {
        fs::remove_dir_all(cache_dir)?;
    }
    fs::create_dir_all(cache_dir)?;
    eprintln!("Wasmer cache cleaned successfully.");

    Ok(())
}