Skip to content

Commit

Permalink
[#83] config 경로 및 init 설정 재구현
Browse files Browse the repository at this point in the history
  • Loading branch information
myyrakle committed Jun 20, 2024
1 parent 4679807 commit 420d493
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 40 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ cargo를 사용한다.
cargo install rrdb
```

- 플랫폼별 처리 (Linux)
- 플랫폼별 초기화 (Linux)

심볼릭 링크를 생성하고 초기화를 수행합니다.

Expand All @@ -25,7 +25,7 @@ sudo ln -s /home/$USER/.cargo/bin/rrdb /usr/bin/rrdb
sudo rrdb init
```

- 플랫폼별 처리 (Windows)
- 플랫폼별 초기화 (Windows)

powershell을 관리자 권한으로 실행하고 다음 명령어를 수행합니다.

Expand Down
Empty file removed src/config.rs
Empty file.
19 changes: 19 additions & 0 deletions src/constants/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,20 @@
pub mod predule;

// 기본 데이터베이스 이름
pub const DEFAULT_DATABASE_NAME: &str = "rrdb";

// 기본 설정파일 이름.
pub const DEFAULT_CONFIG_FILENAME: &str = "rrdb.config";

// 기본 Data 디렉터리 이름
pub const DEFAULT_DATA_DIRNAME: &str = "data";

// 운영체제별 기본 저장 경로를 반환합니다.
#[cfg(target_os = "linux")]
pub const DEFAULT_CONFIG_BASEPATH: &str = "/var/lib/rrdb";

#[cfg(target_os = "windows")]
pub const DEFAULT_CONFIG_BASEPATH: &str = "C:\\Program Files\\rrdb";

// #[cfg(target_os = "macos")]
// TODO: MacOS 경로 추가
12 changes: 10 additions & 2 deletions src/executor/config/global.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
use serde::{Deserialize, Serialize};

#[derive(Deserialize, Serialize, Debug, Clone)]
pub struct GlobalConfig {}
pub struct GlobalConfig {
pub port: u32,
pub host: String,
pub data_directory: String,
}

#[allow(clippy::derivable_impls)]
impl std::default::Default for GlobalConfig {
fn default() -> Self {
Self {}
Self {
port: 55555,
host: "0.0.0.0".to_string(),
data_directory: "data".to_string(),
}
}
}
45 changes: 38 additions & 7 deletions src/executor/executor.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
use std::path::PathBuf;

use crate::ast::ddl::create_database::CreateDatabaseQuery;
use crate::ast::{DDLStatement, DMLStatement, OtherStatement, SQLStatement};
use crate::constants::{
DEFAULT_CONFIG_BASEPATH, DEFAULT_CONFIG_FILENAME, DEFAULT_DATABASE_NAME, DEFAULT_DATA_DIRNAME,
};
use crate::errors::execute_error::ExecuteError;
use crate::errors::RRDBError;
use crate::executor::predule::ExecuteResult;
use crate::logger::predule::Logger;
use crate::utils::path::get_target_basepath;

use super::config::global::GlobalConfig;

Expand All @@ -23,8 +27,8 @@ impl Executor {

// 기본 설정파일 세팅
pub async fn init(&self) -> Result<(), RRDBError> {
// 루트 디렉터리 생성 (없다면)
let base_path = get_target_basepath();
// 1. 루트 디렉터리 생성 (없다면)
let base_path = PathBuf::from(DEFAULT_CONFIG_BASEPATH);
if let Err(error) = tokio::fs::create_dir(base_path.clone()).await {
if error.kind() == std::io::ErrorKind::AlreadyExists {
// Do Nothing
Expand All @@ -35,18 +39,45 @@ impl Executor {
}
}

// 전역 설정파일 생성
// 2. 전역 설정파일 생성 (없다면)
let mut global_path = base_path.clone();
global_path.push("global.config");
global_path.push(DEFAULT_CONFIG_FILENAME);

if let Err(error) = tokio::fs::create_dir(global_path.parent().unwrap().to_path_buf()).await
{
if error.kind() == std::io::ErrorKind::AlreadyExists {
// Do Nothing
} else {
return Err(ExecuteError::new(error.to_string()));
}
}

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 {
return Err(ExecuteError::new(error.to_string()));
}

self.create_database(CreateDatabaseQuery::builder().set_name("rrdb".into()))
.await?;
// 3. 데이터 디렉터리 생성 (없다면)
let mut data_path = base_path.clone();
data_path.push(DEFAULT_DATA_DIRNAME);

if let Err(error) = tokio::fs::create_dir(data_path).await {
if error.kind() == std::io::ErrorKind::AlreadyExists {
// Do Nothing
} else {
return Err(ExecuteError::new(error.to_string()));
}
}

// 4. 기본 데이터베이스 생성 (rrdb)
self.create_database(
CreateDatabaseQuery::builder()
.set_name(DEFAULT_DATABASE_NAME.into())
.set_if_not_exists(true),
)
.await?;

Ok(())
}
Expand Down
3 changes: 2 additions & 1 deletion src/executor/util.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use std::path::PathBuf;

use crate::{executor::predule::Executor, utils::path::get_target_basepath};
use crate::executor::predule::Executor;

impl Executor {
// 데이터 저장 경로를 지정합니다.
pub fn get_base_path(&self) -> PathBuf {
PathBuf::from(get_target_basepath())
}
Expand Down
1 change: 0 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
pub mod ast;
pub mod command;
pub mod config;
pub mod constants;
pub mod errors;
pub mod executor;
Expand Down
1 change: 0 additions & 1 deletion src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
pub mod collection;
pub mod path;
pub mod float;
pub mod macos;
pub mod predule;
25 changes: 0 additions & 25 deletions src/utils/path.rs

This file was deleted.

1 change: 0 additions & 1 deletion src/utils/predule.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
pub use super::path::*;
pub use super::float::*;
pub use super::macos::*;

0 comments on commit 420d493

Please sign in to comment.