From 420d4931ffe428cc0f5affc3a2abd67728dc7275 Mon Sep 17 00:00:00 2001 From: myyrakle Date: Mon, 1 Apr 2024 01:16:10 +0900 Subject: [PATCH] =?UTF-8?q?[#83]=20config=20=EA=B2=BD=EB=A1=9C=20=EB=B0=8F?= =?UTF-8?q?=20init=20=EC=84=A4=EC=A0=95=20=EC=9E=AC=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 ++-- src/config.rs | 0 src/constants/mod.rs | 19 +++++++++++++++ src/executor/config/global.rs | 12 ++++++++-- src/executor/executor.rs | 45 +++++++++++++++++++++++++++++------ src/executor/util.rs | 3 ++- src/main.rs | 1 - src/utils/mod.rs | 1 - src/utils/path.rs | 25 ------------------- src/utils/predule.rs | 1 - 10 files changed, 71 insertions(+), 40 deletions(-) delete mode 100644 src/config.rs delete mode 100644 src/utils/path.rs diff --git a/README.md b/README.md index 252869a1..399e5d4c 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ cargo를 사용한다. cargo install rrdb ``` -- 플랫폼별 처리 (Linux) +- 플랫폼별 초기화 (Linux) 심볼릭 링크를 생성하고 초기화를 수행합니다. @@ -25,7 +25,7 @@ sudo ln -s /home/$USER/.cargo/bin/rrdb /usr/bin/rrdb sudo rrdb init ``` -- 플랫폼별 처리 (Windows) +- 플랫폼별 초기화 (Windows) powershell을 관리자 권한으로 실행하고 다음 명령어를 수행합니다. diff --git a/src/config.rs b/src/config.rs deleted file mode 100644 index e69de29b..00000000 diff --git a/src/constants/mod.rs b/src/constants/mod.rs index 309ae6fd..d9b19573 100644 --- a/src/constants/mod.rs +++ b/src/constants/mod.rs @@ -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 경로 추가 diff --git a/src/executor/config/global.rs b/src/executor/config/global.rs index 0c87af23..3167b996 100644 --- a/src/executor/config/global.rs +++ b/src/executor/config/global.rs @@ -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(), + } } } diff --git a/src/executor/executor.rs b/src/executor/executor.rs index f791a066..762bf073 100644 --- a/src/executor/executor.rs +++ b/src/executor/executor.rs @@ -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; @@ -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 @@ -35,9 +39,19 @@ 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(); @@ -45,8 +59,25 @@ impl Executor { 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(()) } diff --git a/src/executor/util.rs b/src/executor/util.rs index abb7e4ca..c12eab59 100644 --- a/src/executor/util.rs +++ b/src/executor/util.rs @@ -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()) } diff --git a/src/main.rs b/src/main.rs index 5aa2f20a..1e514a9d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,5 @@ pub mod ast; pub mod command; -pub mod config; pub mod constants; pub mod errors; pub mod executor; diff --git a/src/utils/mod.rs b/src/utils/mod.rs index 424d8452..f97fc628 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -1,5 +1,4 @@ pub mod collection; -pub mod path; pub mod float; pub mod macos; pub mod predule; diff --git a/src/utils/path.rs b/src/utils/path.rs deleted file mode 100644 index dba61c76..00000000 --- a/src/utils/path.rs +++ /dev/null @@ -1,25 +0,0 @@ -// 운영 체제에 종속적인 형태로, 파일 저장경로 등에 대한 값을 환경변수로 저장합니다. -// Windows, Linux, MacOS를 위주로 지원합니다. - -use std::{path::PathBuf, str::FromStr}; - -// 운영체제별 기본 저장 경로를 반환합니다. -// 현재는 Windows, Linux만 지원합니다. -#[allow(unreachable_code)] -pub fn get_target_basepath() -> PathBuf { - #[cfg(target_os = "windows")] - { - return PathBuf::from_str("C:\\Program Files\\rrdb").unwrap(); - } - - #[cfg(target_os = "linux")] - { - return PathBuf::from_str("/var/lib/rrdb").unwrap(); - } - - // #[cfg(target_os = "macos")] - // { - // } - - unimplemented!("Not supported OS"); -} diff --git a/src/utils/predule.rs b/src/utils/predule.rs index 53ef6ad1..ecd759e9 100644 --- a/src/utils/predule.rs +++ b/src/utils/predule.rs @@ -1,3 +1,2 @@ -pub use super::path::*; pub use super::float::*; pub use super::macos::*;