diff options
| author | Ellie Huxtable <e@elm.sh> | 2021-03-10 21:24:08 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-03-10 21:24:08 +0000 |
| commit | 61607e023fbb916f376a7070f8b1ffd6ffe16849 (patch) | |
| tree | eadc28b084e0d07f6e3fd0fcaa4c74f2f840c04c /src/settings.rs | |
| parent | Really annoyed me today, so bump version (diff) | |
| download | atuin-61607e023fbb916f376a7070f8b1ffd6ffe16849.zip | |
Add config file support (#15)
Diffstat (limited to 'src/settings.rs')
| -rw-r--r-- | src/settings.rs | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/src/settings.rs b/src/settings.rs new file mode 100644 index 00000000..a4c9f8da --- /dev/null +++ b/src/settings.rs @@ -0,0 +1,64 @@ +use std::path::PathBuf; + +use config::{Config, File}; +use directories::ProjectDirs; +use eyre::{eyre, Result}; +use std::fs; + +#[derive(Debug, Deserialize)] +pub struct LocalDatabase { + pub path: String, +} + +#[derive(Debug, Deserialize)] +pub struct Local { + pub server_address: String, + pub dialect: String, + pub db: LocalDatabase, +} + +#[derive(Debug, Deserialize)] +pub struct Settings { + pub local: Local, +} + +impl Settings { + pub fn new() -> Result<Self> { + let config_dir = ProjectDirs::from("com", "elliehuxtable", "atuin").unwrap(); + let config_dir = config_dir.config_dir(); + + fs::create_dir_all(config_dir)?; + + let mut config_file = PathBuf::new(); + config_file.push(config_dir); + config_file.push("config.toml"); + let config_file = config_file.as_path(); + + // create the config file if it does not exist + + let mut s = Config::new(); + + let db_path = ProjectDirs::from("com", "elliehuxtable", "atuin") + .ok_or_else(|| { + eyre!("could not determine db file location\nspecify one using the --db flag") + })? + .data_dir() + .join("history.db"); + + s.set_default("local.server_address", "https://atuin.elliehuxtable.com")?; + s.set_default("local.dialect", "us")?; + s.set_default("local.db.path", db_path.to_str())?; + + if config_file.exists() { + s.merge(File::with_name(config_file.to_str().unwrap()))?; + } + + // all paths should be expanded + let db_path = s.get_str("local.db.path")?; + let db_path = shellexpand::full(db_path.as_str())?; + s.set("local.db.path", db_path.to_string())?; + + s.try_into() + .map_err(|e| eyre!("failed to deserialize: {}", e)) + } +} |
