summaryrefslogtreecommitdiff
path: root/src/config.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/config.rs')
-rw-r--r--src/config.rs30
1 files changed, 26 insertions, 4 deletions
diff --git a/src/config.rs b/src/config.rs
index 13dd0e4..46d2204 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -1,3 +1,5 @@
1use std::{fs::create_dir_all, io::Write, path::PathBuf};
2
1use config::{File, Map}; 3use config::{File, Map};
2use serde::{Deserialize, Serialize}; 4use serde::{Deserialize, Serialize};
3use tracing::{debug, trace}; 5use tracing::{debug, trace};
@@ -6,7 +8,6 @@ use tracing::{debug, trace};
6#[serde(default)] 8#[serde(default)]
7pub struct Config { 9pub struct Config {
8 pub root: String, 10 pub root: String,
9 pub user: Vec<String>,
10 pub directories: Vec<String>, 11 pub directories: Vec<String>,
11 pub custom_directories: Map<String, String>, 12 pub custom_directories: Map<String, String>,
12 pub device: String, 13 pub device: String,
@@ -16,7 +17,6 @@ impl Default for Config {
16 fn default() -> Self { 17 fn default() -> Self {
17 Self { 18 Self {
18 root: "/mnt/backup".to_string(), 19 root: "/mnt/backup".to_string(),
19 user: vec![],
20 directories: vec![], 20 directories: vec![],
21 custom_directories: Map::new(), 21 custom_directories: Map::new(),
22 device: gethostname::gethostname() 22 device: gethostname::gethostname()
@@ -27,10 +27,16 @@ impl Default for Config {
27} 27}
28 28
29impl Config { 29impl Config {
30 pub fn load() -> Result<Self, config::ConfigError> { 30 pub fn load(path: Option<PathBuf>) -> core::result::Result<Self, config::ConfigError> {
31 debug!("load config"); 31 debug!("load config");
32 let source = if let Some(source) = path {
33 source
34 } else {
35 Self::get_location()
36 };
37
32 let config = config::Config::builder() 38 let config = config::Config::builder()
33 .add_source(File::with_name("config.toml").required(false)) 39 .add_source(File::with_name(&source.to_string_lossy()).required(false))
34 .add_source(config::Environment::with_prefix("FXBAUP").separator("_")) 40 .add_source(config::Environment::with_prefix("FXBAUP").separator("_"))
35 .build()?; 41 .build()?;
36 42
@@ -39,4 +45,20 @@ impl Config {
39 45
40 cfg 46 cfg
41 } 47 }
48
49 pub fn generate() -> crate::error::Result<()> {
50 let loc = Self::get_location();
51 create_dir_all(loc.parent().unwrap())?;
52 let mut f = std::fs::File::create(loc)?;
53 f.write_all(toml::to_string(&Self::default())?.as_bytes())?;
54
55 Ok(())
56 }
57
58 fn get_location() -> PathBuf {
59 let mut conf_dir = dirs::config_local_dir().unwrap();
60 conf_dir.push("arbs");
61 conf_dir.push("config.toml");
62 conf_dir
63 }
42} 64}