Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Make the "wasmer cache" command use the same cache directory as the rest of the CLI #4160

Merged
merged 2 commits into from
Aug 17, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}