-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
new: added command-line option for config file path (#233)
- Loading branch information
Showing
8 changed files
with
200 additions
and
43 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ members = [".", "crate/encstr"] | |
[workspace.package] | ||
repository = "https://github.com/pamburus/hl" | ||
authors = ["Pavel Ivanov <[email protected]>"] | ||
version = "0.29.0-alpha.4" | ||
version = "0.29.0-alpha.5" | ||
edition = "2021" | ||
license = "MIT" | ||
|
||
|
@@ -40,7 +40,7 @@ chrono = { version = "0.4", default-features = false, features = [ | |
"std", | ||
] } | ||
chrono-tz = { version = "0", features = ["serde"] } | ||
clap = { version = "4", features = ["wrap_help", "derive", "env"] } | ||
clap = { version = "4", features = ["wrap_help", "derive", "env", "string"] } | ||
clap_complete = "4" | ||
closure = "0" | ||
collection_macros = "0" | ||
|
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,31 +1,51 @@ | ||
// std imports | ||
use std::sync::Mutex; | ||
|
||
// third-party imports | ||
use once_cell::sync::Lazy; | ||
use platform_dirs::AppDirs; | ||
|
||
// local imports | ||
use crate::settings::Settings; | ||
use crate::{error::Result, settings::Settings}; | ||
|
||
// --- | ||
|
||
pub const APP_NAME: &str = "hl"; | ||
|
||
static CONFIG: Lazy<Settings> = Lazy::new(load); | ||
static DEFAULT: Lazy<Settings> = Lazy::new(Settings::default); | ||
|
||
pub fn get() -> &'static Settings { | ||
&CONFIG | ||
/// Get the default settings. | ||
pub fn default() -> &'static Settings { | ||
Default::default() | ||
} | ||
|
||
pub fn default() -> &'static Settings { | ||
&DEFAULT | ||
/// Load settings from the given file or the default configuration file per platform. | ||
pub fn load(path: String) -> Result<Settings> { | ||
if path.is_empty() { | ||
return Ok(Default::default()); | ||
} | ||
|
||
Settings::load(&path) | ||
} | ||
|
||
pub fn app_dirs() -> AppDirs { | ||
AppDirs::new(Some(APP_NAME), true).unwrap() | ||
/// Get the application platform-specific directories. | ||
pub fn app_dirs() -> Option<AppDirs> { | ||
AppDirs::new(Some(APP_NAME), true) | ||
} | ||
|
||
// --- | ||
pub mod global { | ||
use super::*; | ||
|
||
static PENDING: Mutex<Option<Settings>> = Mutex::new(None); | ||
static RESOLVED: Lazy<Settings> = Lazy::new(|| PENDING.lock().unwrap().take().unwrap_or_default()); | ||
|
||
/// Call initialize before any calls to get otherwise it will have no effect. | ||
pub fn initialize(settings: Settings) { | ||
*PENDING.lock().unwrap() = Some(settings); | ||
} | ||
|
||
fn load() -> Settings { | ||
Settings::load(&app_dirs()).unwrap() | ||
/// Get the resolved settings. | ||
/// If initialized was called before, then a clone of those settings will be returned. | ||
/// Otherwise, the default settings will be returned. | ||
pub fn get() -> &'static Settings { | ||
&RESOLVED | ||
} | ||
} |
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
Oops, something went wrong.