Allow to specify a path to the data file

This commit is contained in:
Quentin Boyer 2023-11-15 00:03:52 +01:00
parent 7e17fb12c9
commit 9adc650661

View file

@ -1236,6 +1236,9 @@ struct Config {
#[serde(default = "person_2")]
person_2: String,
#[serde(default)]
data_file: Option<PathBuf>,
}
impl Default for Config {
@ -1243,23 +1246,13 @@ impl Default for Config {
Self {
person_1: person_1(),
person_2: person_2(),
data_file: None,
}
}
}
fn main() -> anyhow::Result<()> {
let project_dir = ProjectDirs::from("net", "traxys", "glaurung").ok_or(anyhow!(""))?;
let state_dir = project_dir
.state_dir()
.ok_or(anyhow!("No state directory"))?;
std::fs::create_dir_all(state_dir)?;
let save_file = state_dir.join("data.json");
let save = match save_file.exists() {
false => Default::default(),
true => serde_json::from_reader(BufReader::new(File::open(&save_file)?))?,
};
let config_file = project_dir.config_dir().join("config.toml");
let config: Config = match config_file.exists() {
@ -1270,6 +1263,23 @@ fn main() -> anyhow::Result<()> {
false => Config::default(),
};
let save_file = match &config.data_file {
None => {
let state_dir = project_dir
.state_dir()
.ok_or(anyhow!("No state directory"))?;
std::fs::create_dir_all(state_dir)?;
state_dir.join("data.json")
}
Some(p) => p.clone(),
};
let save = match save_file.exists() {
false => Default::default(),
true => serde_json::from_reader(BufReader::new(File::open(&save_file)?))?,
};
let mut settings = Settings::with_flags(AppConfig {
save,
save_file,