Skip to content

Commit

Permalink
[#112] initializer에 FileSystem 교체
Browse files Browse the repository at this point in the history
  • Loading branch information
myyrakle authored and DPS0340 committed Jul 6, 2024
1 parent e0ce1b4 commit 8027c02
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 14 deletions.
45 changes: 31 additions & 14 deletions src/executor/initializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ impl Executor {
}

async fn create_top_level_directory_if_not_exists(&self) -> Result<(), RRDBError> {
let base_path = PathBuf::from(DEFAULT_CONFIG_BASEPATH);
let base_path = DEFAULT_CONFIG_BASEPATH;

if let Err(error) = tokio::fs::create_dir(base_path.clone()).await {
if let Err(error) = self.file_system.create_dir(base_path).await {
if error.kind() != std::io::ErrorKind::AlreadyExists {
println!("path {:?}", base_path.clone());
println!("path {:?}", base_path);
println!("error: {:?}", error.to_string());
return Err(ExecuteError::wrap(error.to_string()));
}
Expand All @@ -58,22 +58,28 @@ impl Executor {
}

async fn create_global_config_if_not_exists(&self) -> Result<(), RRDBError> {
let base_path = PathBuf::from(DEFAULT_CONFIG_BASEPATH);

let mut global_path = base_path.clone();
global_path.push(DEFAULT_CONFIG_FILENAME);

if let Err(error) = tokio::fs::create_dir(global_path.parent().unwrap().to_path_buf()).await
{
if let Err(error) = self.file_system.create_dir(DEFAULT_CONFIG_BASEPATH).await {
if error.kind() != std::io::ErrorKind::AlreadyExists {
return Err(ExecuteError::wrap(error.to_string()));
}
}

let base_path = PathBuf::from(DEFAULT_CONFIG_BASEPATH);

let mut global_path = base_path;
global_path.push(DEFAULT_CONFIG_FILENAME);

let global_info = GlobalConfig::default();
let global_config = toml::to_string(&global_info).unwrap();

if let Err(error) = tokio::fs::write(global_path, global_config.as_bytes()).await {
if let Err(error) = self
.file_system
.write_file(
global_path.to_str().unwrap_or_default(),
global_config.as_bytes(),
)
.await
{
return Err(ExecuteError::wrap(error.to_string()));
}

Expand All @@ -83,7 +89,7 @@ impl Executor {
async fn create_data_directory_if_not_exists(&self) -> Result<(), RRDBError> {
let data_path = self.config.data_directory.clone();

if let Err(error) = tokio::fs::create_dir(data_path).await {
if let Err(error) = self.file_system.create_dir(&data_path).await {
if error.kind() != std::io::ErrorKind::AlreadyExists {
return Err(ExecuteError::wrap(error.to_string()));
}
Expand Down Expand Up @@ -146,7 +152,14 @@ WantedBy=multi-user.target"#;
async fn create_daemon_config_if_not_exists(&self) -> Result<(), RRDBError> {
Command::new("winget").args(["install", "--accept-package-agreements", "nssm"]);

let output = Command::new("nssm.exe").args(["install", "rrdb", "C:\\Program Files\\rrdb\\rrdb.exe", "run"]).output();
let output = Command::new("nssm.exe")
.args([
"install",
"rrdb",
"C:\\Program Files\\rrdb\\rrdb.exe",
"run",
])
.output();

self.check_output_status(output)
}
Expand All @@ -156,7 +169,11 @@ WantedBy=multi-user.target"#;
base_path: PathBuf,
contents: &str,
) -> Result<(), RRDBError> {
if let Err(error) = tokio::fs::write(base_path, contents).await {
if let Err(error) = self
.file_system
.write_file(base_path.to_str().unwrap_or_default(), contents.as_bytes())
.await
{
if error.kind() != std::io::ErrorKind::AlreadyExists {
return Err(ExecuteError::wrap(error.to_string()));
}
Expand Down
5 changes: 5 additions & 0 deletions src/executor/mocking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use futures::io;
#[async_trait::async_trait]
pub trait FileSystem {
async fn create_dir(&self, path: &str) -> io::Result<()>;
async fn write_file(&self, path: &str, content: &[u8]) -> io::Result<()>;
}

pub struct RealFileSystem;
Expand All @@ -12,4 +13,8 @@ impl FileSystem for RealFileSystem {
async fn create_dir(&self, path: &str) -> io::Result<()> {
tokio::fs::create_dir(path).await
}

async fn write_file(&self, path: &str, content: &[u8]) -> io::Result<()> {
tokio::fs::write(path, content).await
}
}

0 comments on commit 8027c02

Please sign in to comment.