forked from wasmerio/wasmer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Make the "wasmer cache" command use the same cache directory as …
…the rest of the CLI (fixes wasmerio#4093)
- Loading branch information
Michael-F-Bryan
committed
Aug 17, 2023
1 parent
64ab037
commit 94fa59e
Showing
3 changed files
with
36 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters