mirror of
https://github.com/winapps-org/winapps.git
synced 2025-06-04 06:07:19 +02:00
fix: clean up config file & data dir loading
This commit is contained in:
parent
6b730e8cb1
commit
6ab6b73adb
@ -1,8 +1,10 @@
|
||||
pub mod quickemu;
|
||||
|
||||
use derive_new::new;
|
||||
use home::home_dir;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::io::Write;
|
||||
use std::path::PathBuf;
|
||||
use std::{
|
||||
env,
|
||||
fs::{self, File},
|
||||
@ -23,6 +25,16 @@ pub struct Config {
|
||||
host: HostConfig,
|
||||
#[new(value = "RemoteConfig::new()")]
|
||||
rdp: RemoteConfig,
|
||||
#[new(value = "VmConfig::new()")]
|
||||
vm: VmConfig,
|
||||
}
|
||||
|
||||
#[derive(new, Debug, Deserialize, Serialize)]
|
||||
pub struct VmConfig {
|
||||
#[new(value = "\"windows-10\".to_string()")]
|
||||
short_name: String,
|
||||
#[new(value = "\"windows-10-22H2\".to_string()")]
|
||||
name: String,
|
||||
}
|
||||
|
||||
#[derive(new, Debug, Deserialize, Serialize)]
|
||||
@ -43,35 +55,86 @@ pub struct RemoteConfig {
|
||||
password: String,
|
||||
}
|
||||
|
||||
pub fn load_config(path: Option<&str>) -> Config {
|
||||
let config = env::var("XDG_CONFIG_HOME").expect("Could not find the home path!");
|
||||
let default = &format!("{}{}", config, "/winapps/");
|
||||
let path = Path::new(path.unwrap_or(default));
|
||||
let config = Config::new();
|
||||
pub fn get_config_file(path: Option<&str>) -> PathBuf {
|
||||
let default = match env::var("XDG_CONFIG_HOME") {
|
||||
Ok(dir) => PathBuf::from(dir).join("winapps"),
|
||||
Err(_) => {
|
||||
println!("Couldn't read XDG_CONFIG_HOME, falling back to ~/.config");
|
||||
home_dir()
|
||||
.expect("Could not find the home path!")
|
||||
.join(".config/winapps")
|
||||
}
|
||||
};
|
||||
|
||||
let path = Path::new(path.unwrap_or(default.to_str().unwrap()));
|
||||
|
||||
if !path.exists() {
|
||||
println!("{:?} does not exist! Creating...", path.to_str());
|
||||
println!("{:?} does not exist! Creating...", path);
|
||||
fs::create_dir_all(path).expect("Failed to create directory");
|
||||
}
|
||||
|
||||
let config_file = path.join("config.toml");
|
||||
|
||||
if !config_file.exists() {
|
||||
let mut config_file =
|
||||
File::create(&config_file).expect("Failed to create configuration file");
|
||||
|
||||
let gen_config =
|
||||
toml::to_string(&config).expect("Failed to generate default configuration");
|
||||
write!(config_file, "{}", gen_config).expect("Failed to write configuration file");
|
||||
if !path.is_dir() {
|
||||
panic!("Config directory {:?} is not a directory!", path);
|
||||
}
|
||||
|
||||
let config_file = fs::read_to_string(config_file).expect("Failed to read configuration file");
|
||||
path.join("config.toml")
|
||||
}
|
||||
|
||||
pub fn load_config(path: Option<&str>) -> Config {
|
||||
let config = Config::new();
|
||||
let config_path = get_config_file(path);
|
||||
|
||||
if !config_path.exists() {
|
||||
save_config(&config, path).expect("Failed to write default configuration");
|
||||
return config;
|
||||
}
|
||||
|
||||
let config_file = fs::read_to_string(config_path).expect("Failed to read configuration file");
|
||||
let config: Config =
|
||||
toml::from_str(config_file.as_str()).expect("Failed to parse the configuration");
|
||||
|
||||
config
|
||||
}
|
||||
|
||||
pub fn save_config(config: &Config, path: Option<&str>) -> std::io::Result<()> {
|
||||
let config_path = get_config_file(path);
|
||||
let serialized_config = toml::to_string(&config).expect("Failed to serialize configuration");
|
||||
|
||||
let mut config_file = match config_path.exists() {
|
||||
true => File::open(&config_path).expect("Failed to open configuration file"),
|
||||
false => File::create(&config_path).expect("Failed to create configuration file"),
|
||||
};
|
||||
|
||||
write!(config_file, "{}", serialized_config)
|
||||
}
|
||||
|
||||
pub fn get_data_dir() -> PathBuf {
|
||||
let data_dir = match env::var("XDG_DATA_HOME") {
|
||||
Ok(dir) => PathBuf::from(dir).join("winapps"),
|
||||
Err(_) => {
|
||||
println!("Couldn't read XDG_DATA_HOME, falling back to ~/.local/share");
|
||||
home_dir()
|
||||
.expect("Could not find the home path!")
|
||||
.join(".local/share/winapps")
|
||||
}
|
||||
};
|
||||
|
||||
if !data_dir.exists() {
|
||||
let dir = data_dir.clone();
|
||||
println!(
|
||||
"Data directory {:?} does not exist! Creating...",
|
||||
dir.to_str()
|
||||
);
|
||||
fs::create_dir_all(dir).expect("Failed to create directory");
|
||||
}
|
||||
|
||||
if !data_dir.is_dir() {
|
||||
panic!("Data directory {:?} is not a directory!", data_dir);
|
||||
}
|
||||
|
||||
data_dir
|
||||
}
|
||||
|
||||
pub fn add(left: usize, right: usize) -> usize {
|
||||
left + right
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user