up/src/config.rs
2024-12-23 17:47:33 -08:00

47 lines
1.1 KiB
Rust

use std::{io, fs};
use std::path::PathBuf;
use std::collections::HashMap;
use expanduser::expanduser;
use serde::Deserialize;
use serde_yaml;
pub fn load_config() -> Config {
Config::new("~/.up.yml")
}
#[derive(Deserialize)]
pub struct Config {
download_path: String,
upload_path: String,
pub web_port: u16,
pub shell_port: u16,
shells: HashMap<String, String>
}
impl Config {
pub fn new(path: &str) -> Self {
let exp_path = expanduser(path)
.expect(&format!("Failed to expand path, {}", path));
let file = fs::File::open(&exp_path)
.expect(&format!("Failed to open config path, {}", exp_path.display()));
let reader = io::BufReader::new(file);
serde_yaml::from_reader(reader).unwrap()
}
pub fn get_download_path(&self) -> PathBuf {
expanduser(self.download_path.clone()).unwrap()
}
pub fn get_upload_path(&self) -> PathBuf {
expanduser(self.upload_path.clone()).unwrap()
}
pub fn get_shell<S: Into<String>>(&self, key: S) -> Option<String> {
let key_str = key.into();
self.shells.get(&key_str).cloned()
}
}