Skip to content

Commit

Permalink
Merge pull request #4160 from wasmerio/cache-dir
Browse files Browse the repository at this point in the history
fix: Make the "wasmer cache" command use the same cache directory as the rest of the CLI
  • Loading branch information
Michael Bryan authored Aug 17, 2023
2 parents 64ab037 + 1b24205 commit 66bc2b2
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 50 deletions.
1 change: 0 additions & 1 deletion lib/cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,6 @@ enum Cmd {
Publish(Publish),

/// Wasmer cache
#[clap(subcommand)]
Cache(Cache),

/// Validate a WebAssembly binary
Expand Down
63 changes: 35 additions & 28 deletions lib/cli/src/commands/cache.rs
Original file line number Diff line number Diff line change
@@ -1,44 +1,51 @@
use crate::common::get_cache_dir;
use anyhow::{Context, Result};
use std::{fs, path::Path};

use anyhow::Result;
use clap::Parser;
use std::fs;
use wasmer_registry::wasmer_env::WasmerEnv;

#[derive(Debug, Parser)]
/// The options for the `wasmer cache` subcommand
pub enum Cache {
/// Clear the cache
#[clap(name = "clean")]
Clean,

/// Display the location of the cache
#[clap(name = "dir")]
Dir,
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<()> {
match &self {
Cache::Clean => {
self.clean().context("failed to clean wasmer cache.")?;
let cache_dir = self.env.cache_dir();

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

Ok(())
}
fn clean(&self) -> Result<()> {
let cache_dir = get_cache_dir();
if cache_dir.exists() {
fs::remove_dir_all(cache_dir.clone())?;
}
fs::create_dir_all(cache_dir)?;
eprintln!("Wasmer cache cleaned successfully.");
Ok(())
}
fn dir(&self) -> Result<()> {
println!("{}", get_cache_dir().to_string_lossy());
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(())
}
22 changes: 1 addition & 21 deletions lib/cli/src/common.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
//! Common module with common used structures across different
//! commands.
use crate::VERSION;
use clap::Parser;
use std::env;
use std::path::PathBuf;

#[derive(Debug, Parser, Clone, Default)]
/// The WebAssembly features that can be passed through the
Expand Down Expand Up @@ -38,24 +36,6 @@ pub struct WasmFeatures {
pub all: bool,
}

/// Get the cache dir
pub fn get_cache_dir() -> PathBuf {
match env::var("WASMER_CACHE_DIR") {
Ok(dir) => {
let mut path = PathBuf::from(dir);
path.push(VERSION);
path
}
Err(_) => {
// We use a temporal directory for saving cache files
let mut temp_dir = env::temp_dir();
temp_dir.push("wasmer");
temp_dir.push(VERSION);
temp_dir
}
}
}

pub(crate) fn normalize_path(s: &str) -> String {
wasmer_registry::utils::normalize_path(s)
}

0 comments on commit 66bc2b2

Please sign in to comment.