add error handling for config

This commit is contained in:
2022-08-28 23:18:59 +02:00
parent 9c306211c6
commit 9f6b5bb28f
2 changed files with 12 additions and 5 deletions

View File

@@ -22,8 +22,12 @@ pub struct Host {
>
}
pub fn read() -> Config {
let yaml = fs::read_to_string(Path::new("config.yaml")).unwrap();
let config: Config = serde_yaml::from_str(&yaml).unwrap();
return config;
pub fn read() -> Result<Config, ()> {
let yaml = fs::read_to_string(Path::new("config.yaml")).unwrap_or("".to_string());
let config: Config = match serde_yaml::from_str(&yaml) {
Ok(c) => c,
Err(_) => return Err(())
};
Ok(config)
}

View File

@@ -64,7 +64,10 @@ fn generate_authorized_keys(host_keys: Vec<String>) -> String {
fn main() {
let mut hosts: Vec<Host> = vec![];
let config = config::read();
let config = match config::read() {
Ok(c) => c,
Err(_) => return println!("Error: `config.yaml` not found!")
};
for host in &config.hosts {
for (user_name, user_data) in &host.users {