summaryrefslogtreecommitdiff
path: root/src/config.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/config.rs')
-rw-r--r--src/config.rs28
1 files changed, 17 insertions, 11 deletions
diff --git a/src/config.rs b/src/config.rs
index 46d2204..848be19 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -4,6 +4,8 @@ use config::{File, Map};
4use serde::{Deserialize, Serialize}; 4use serde::{Deserialize, Serialize};
5use tracing::{debug, trace}; 5use tracing::{debug, trace};
6 6
7use crate::{error::{Error, Result}, packages::Manager};
8
7#[derive(Debug, Serialize, Deserialize)] 9#[derive(Debug, Serialize, Deserialize)]
8#[serde(default)] 10#[serde(default)]
9pub struct Config { 11pub struct Config {
@@ -11,6 +13,7 @@ pub struct Config {
11 pub directories: Vec<String>, 13 pub directories: Vec<String>,
12 pub custom_directories: Map<String, String>, 14 pub custom_directories: Map<String, String>,
13 pub device: String, 15 pub device: String,
16 pub package_manager: Manager,
14} 17}
15 18
16impl Default for Config { 19impl Default for Config {
@@ -22,17 +25,18 @@ impl Default for Config {
22 device: gethostname::gethostname() 25 device: gethostname::gethostname()
23 .into_string() 26 .into_string()
24 .expect("invalid hostname string"), 27 .expect("invalid hostname string"),
28 package_manager: Manager::from_sys().expect("couldn't get package manager"),
25 } 29 }
26 } 30 }
27} 31}
28 32
29impl Config { 33impl Config {
30 pub fn load(path: Option<PathBuf>) -> core::result::Result<Self, config::ConfigError> { 34 pub fn load(path: Option<PathBuf>) -> Result<Self> {
31 debug!("load config"); 35 debug!("load config");
32 let source = if let Some(source) = path { 36 let source = if let Some(source) = path {
33 source 37 source
34 } else { 38 } else {
35 Self::get_location() 39 Self::get_location()?
36 }; 40 };
37 41
38 let config = config::Config::builder() 42 let config = config::Config::builder()
@@ -40,14 +44,14 @@ impl Config {
40 .add_source(config::Environment::with_prefix("FXBAUP").separator("_")) 44 .add_source(config::Environment::with_prefix("FXBAUP").separator("_"))
41 .build()?; 45 .build()?;
42 46
43 let cfg = config.try_deserialize(); 47 let cfg = config.try_deserialize()?;
44 trace!(?cfg, "loaded config"); 48 trace!(?cfg, "loaded config");
45 49
46 cfg 50 Ok(cfg)
47 } 51 }
48 52
49 pub fn generate() -> crate::error::Result<()> { 53 pub fn generate() -> Result<()> {
50 let loc = Self::get_location(); 54 let loc = Self::get_location()?;
51 create_dir_all(loc.parent().unwrap())?; 55 create_dir_all(loc.parent().unwrap())?;
52 let mut f = std::fs::File::create(loc)?; 56 let mut f = std::fs::File::create(loc)?;
53 f.write_all(toml::to_string(&Self::default())?.as_bytes())?; 57 f.write_all(toml::to_string(&Self::default())?.as_bytes())?;
@@ -55,10 +59,12 @@ impl Config {
55 Ok(()) 59 Ok(())
56 } 60 }
57 61
58 fn get_location() -> PathBuf { 62 fn get_location() -> Result<PathBuf> {
59 let mut conf_dir = dirs::config_local_dir().unwrap(); 63 let Some(mut config_dir) = dirs::config_dir() else {
60 conf_dir.push("arbs"); 64 return Err(Error::NoSysDir);
61 conf_dir.push("config.toml"); 65 };
62 conf_dir 66 config_dir.push(env!("CARGO_PKG_NAME"));
67 config_dir.push("config.toml");
68 Ok(config_dir)
63 } 69 }
64} 70}