summaryrefslogtreecommitdiff
path: root/src/config.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/config.rs')
-rw-r--r--src/config.rs15
1 files changed, 9 insertions, 6 deletions
diff --git a/src/config.rs b/src/config.rs
index a9a937e..23c7796 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -5,7 +5,7 @@ use std::{
5 5
6use serde::{Deserialize, Serialize}; 6use serde::{Deserialize, Serialize};
7 7
8use crate::{devdir, error::MLE}; 8use crate::error::MLE;
9 9
10#[derive(Debug, Clone, Serialize, Deserialize)] 10#[derive(Debug, Clone, Serialize, Deserialize)]
11pub struct Cfg { 11pub struct Cfg {
@@ -20,10 +20,13 @@ pub struct Apis {
20} 20}
21 21
22impl Cfg { 22impl Cfg {
23 pub fn init(filename: &str) -> MLE<Self> { 23 pub fn init(path: Option<String>) -> MLE<Self> {
24 let configfile = dirs::config_dir().unwrap().join(filename); 24 let configfile = match path {
25 Some(p) => String::from(p),
26 None => dirs::config_dir().unwrap().join("modlist.toml").to_string_lossy().into(),
27 };
25 28
26 let mut file = match File::open(devdir(configfile.to_str().unwrap())) { 29 let mut file = match File::open(&configfile) {
27 Ok(file) => file, 30 Ok(file) => file,
28 Err(err) => { 31 Err(err) => {
29 if err.kind() == std::io::ErrorKind::NotFound { 32 if err.kind() == std::io::ErrorKind::NotFound {
@@ -35,10 +38,10 @@ impl Cfg {
35 modrinth: String::from("https://api.modrinth.com/v2/"), 38 modrinth: String::from("https://api.modrinth.com/v2/"),
36 }, 39 },
37 }; 40 };
38 let mut file = File::create(devdir(configfile.to_str().unwrap()))?; 41 let mut file = File::create(&configfile)?;
39 println!("Created config file"); 42 println!("Created config file");
40 file.write_all(toml::to_string(&default_cfg)?.as_bytes())?; 43 file.write_all(toml::to_string(&default_cfg)?.as_bytes())?;
41 File::open(devdir(configfile.to_str().unwrap()))? 44 File::open(&configfile)?
42 } else { 45 } else {
43 return Err(err.into()); 46 return Err(err.into());
44 } 47 }