add error handling for config
This commit is contained in:
@@ -22,8 +22,12 @@ pub struct Host {
|
|||||||
>
|
>
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn read() -> Config {
|
pub fn read() -> Result<Config, ()> {
|
||||||
let yaml = fs::read_to_string(Path::new("config.yaml")).unwrap();
|
let yaml = fs::read_to_string(Path::new("config.yaml")).unwrap_or("".to_string());
|
||||||
let config: Config = serde_yaml::from_str(&yaml).unwrap();
|
let config: Config = match serde_yaml::from_str(&yaml) {
|
||||||
return config;
|
Ok(c) => c,
|
||||||
|
Err(_) => return Err(())
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok(config)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -64,7 +64,10 @@ fn generate_authorized_keys(host_keys: Vec<String>) -> String {
|
|||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let mut hosts: Vec<Host> = vec![];
|
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 host in &config.hosts {
|
||||||
for (user_name, user_data) in &host.users {
|
for (user_name, user_data) in &host.users {
|
||||||
|
|||||||
Reference in New Issue
Block a user