-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #56 from iawia002/config
Adjust methods of getting the config
- Loading branch information
Showing
9 changed files
with
58 additions
and
70 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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
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,30 +1,36 @@ | ||
use anyhow::{bail, Result}; | ||
use const_format::concatcp; | ||
use anyhow::{anyhow, Result}; | ||
use once_cell::sync::OnceCell; | ||
use std::path::PathBuf; | ||
|
||
const MAIN_DIR: &str = ".wacker"; | ||
const SOCK_PATH: &str = concatcp!(MAIN_DIR, "/wacker.sock"); | ||
const LOGS_DIR: &str = concatcp!(MAIN_DIR, "/logs"); | ||
const DB_PATH: &str = concatcp!(MAIN_DIR, "/db"); | ||
|
||
#[derive(Clone)] | ||
pub struct Config { | ||
pub sock_path: PathBuf, | ||
pub logs_dir: PathBuf, | ||
pub db_path: PathBuf, | ||
static SOCK_PATH: OnceCell<PathBuf> = OnceCell::new(); | ||
static LOGS_DIR: OnceCell<PathBuf> = OnceCell::new(); | ||
static DB_PATH: OnceCell<PathBuf> = OnceCell::new(); | ||
|
||
pub fn get_sock_path() -> Result<&'static PathBuf> { | ||
SOCK_PATH.get_or_try_init(|| -> Result<PathBuf> { | ||
match dirs::home_dir() { | ||
Some(home_dir) => Ok(home_dir.join(MAIN_DIR).join("wacker.sock")), | ||
None => Err(anyhow!("can't get home dir")), | ||
} | ||
}) | ||
} | ||
|
||
pub fn get_logs_dir() -> Result<&'static PathBuf> { | ||
LOGS_DIR.get_or_try_init(|| -> Result<PathBuf> { | ||
match dirs::home_dir() { | ||
Some(home_dir) => Ok(home_dir.join(MAIN_DIR).join("logs")), | ||
None => Err(anyhow!("can't get home dir")), | ||
} | ||
}) | ||
} | ||
|
||
impl Config { | ||
pub fn new() -> Result<Self> { | ||
let home_dir = dirs::home_dir(); | ||
if home_dir.is_none() { | ||
bail!("can't get home dir"); | ||
pub fn get_db_path() -> Result<&'static PathBuf> { | ||
DB_PATH.get_or_try_init(|| -> Result<PathBuf> { | ||
match dirs::home_dir() { | ||
Some(home_dir) => Ok(home_dir.join(MAIN_DIR).join("db")), | ||
None => Err(anyhow!("can't get home dir")), | ||
} | ||
let home_dir = home_dir.unwrap(); | ||
Ok(Self { | ||
sock_path: home_dir.join(SOCK_PATH), | ||
logs_dir: home_dir.join(LOGS_DIR), | ||
db_path: home_dir.join(DB_PATH), | ||
}) | ||
} | ||
}) | ||
} |
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
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