-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(term): abstract file system
- Loading branch information
Showing
11 changed files
with
212 additions
and
82 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 was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,116 @@ | ||
use std::{ | ||
io::ErrorKind, | ||
path::{Path, PathBuf}, | ||
process::ExitCode, | ||
}; | ||
|
||
use anyhow::Context; | ||
use clap::Args; | ||
|
||
use crate::{application::Cache, config, filesystem::FileSystem}; | ||
|
||
/// Clean cache and logs | ||
#[derive(Args, Debug)] | ||
pub struct CleanCommand { | ||
/// Cache directory | ||
#[arg( | ||
long, | ||
default_value = config::cache::dir().to_path_buf().into_os_string(), | ||
)] | ||
cache_dir: PathBuf, | ||
} | ||
|
||
impl CleanCommand { | ||
#[allow(clippy::unused_self)] | ||
pub fn run<FS>(self, fs: &FS) -> ExitCode | ||
where | ||
FS: FileSystem + Clone, | ||
{ | ||
ExitCode::from(self.clean(fs, config::log_path().as_path())) | ||
} | ||
|
||
fn clean<FS>(self, fs: &FS, log: &Path) -> u8 | ||
where | ||
FS: FileSystem + Clone, | ||
{ | ||
if let Err(err) = self.try_clean(fs, log) { | ||
tracing::error!("{err}"); | ||
1 | ||
} else { | ||
0 | ||
} | ||
} | ||
fn try_clean<FS>(self, fs: &FS, log: &Path) -> anyhow::Result<()> | ||
where | ||
FS: FileSystem + Clone, | ||
{ | ||
let CleanCommand { cache_dir } = self; | ||
|
||
let cache = Cache::with(&cache_dir, fs.clone()); | ||
cache | ||
.clean() | ||
.map_err(anyhow::Error::from) | ||
.with_context(|| format!("path: {}", cache_dir.display()))?; | ||
|
||
// remove log | ||
match fs.remove_file(log) { | ||
Ok(()) => { | ||
tracing::info!("Remove {}", log.display()); | ||
} | ||
Err(err) => match err.kind() { | ||
ErrorKind::NotFound => {} | ||
_ => { | ||
return Err(anyhow::Error::from(err)) | ||
.with_context(|| format!("path: {}", log.display())) | ||
} | ||
}, | ||
} | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use std::io; | ||
|
||
use tempfile::{NamedTempFile, TempDir}; | ||
|
||
use crate::filesystem::{fsimpl, mock::MockFileSystem}; | ||
|
||
use super::*; | ||
|
||
#[test] | ||
fn remove_log_file() { | ||
let clean = CleanCommand { | ||
cache_dir: TempDir::new().unwrap().into_path(), | ||
}; | ||
let log_file = NamedTempFile::new().unwrap(); | ||
let exit_code = clean.clean(&fsimpl::FileSystem::new(), log_file.path()); | ||
assert_eq!(exit_code, 0); | ||
assert!(!log_file.path().exists()); | ||
} | ||
|
||
#[test] | ||
fn ignore_log_file_not_found() { | ||
let clean = CleanCommand { | ||
cache_dir: TempDir::new().unwrap().into_path(), | ||
}; | ||
let log_file = Path::new("./not_exists"); | ||
let fs = MockFileSystem::default().with_remove_errors(log_file, io::ErrorKind::NotFound); | ||
let exit_code = clean.clean(&fs, log_file); | ||
assert_eq!(exit_code, 0); | ||
} | ||
|
||
#[test] | ||
fn exit_code_on_permission_error() { | ||
let clean = CleanCommand { | ||
cache_dir: TempDir::new().unwrap().into_path(), | ||
}; | ||
let log_file = Path::new("./not_allowed"); | ||
let fs = | ||
MockFileSystem::default().with_remove_errors(log_file, io::ErrorKind::PermissionDenied); | ||
let exit_code = clean.clean(&fs, log_file); | ||
assert_eq!(exit_code, 1); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
pub mod check; | ||
pub mod clean; | ||
pub mod export; |
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 |
---|---|---|
@@ -0,0 +1,53 @@ | ||
use std::{io, path::Path}; | ||
|
||
pub trait FileSystem { | ||
fn remove_file<P: AsRef<Path>>(&self, path: P) -> io::Result<()>; | ||
} | ||
|
||
pub mod fsimpl { | ||
#[derive(Debug, Clone)] | ||
pub struct FileSystem {} | ||
|
||
impl FileSystem { | ||
pub fn new() -> Self { | ||
Self {} | ||
} | ||
} | ||
|
||
impl super::FileSystem for FileSystem { | ||
fn remove_file<P: AsRef<std::path::Path>>(&self, path: P) -> std::io::Result<()> { | ||
std::fs::remove_file(path) | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
pub(crate) mod mock { | ||
use std::{collections::HashMap, io, path::PathBuf}; | ||
|
||
#[derive(Default, Clone)] | ||
pub(crate) struct MockFileSystem { | ||
remove_errors: HashMap<PathBuf, io::ErrorKind>, | ||
} | ||
|
||
impl MockFileSystem { | ||
pub(crate) fn with_remove_errors( | ||
mut self, | ||
path: impl Into<PathBuf>, | ||
err: io::ErrorKind, | ||
) -> Self { | ||
self.remove_errors.insert(path.into(), err); | ||
self | ||
} | ||
} | ||
|
||
impl super::FileSystem for MockFileSystem { | ||
fn remove_file<P: AsRef<std::path::Path>>(&self, path: P) -> io::Result<()> { | ||
let path = path.as_ref(); | ||
match self.remove_errors.get(path) { | ||
Some(err) => Err(io::Error::from(*err)), | ||
None => 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
Oops, something went wrong.