mirror of
https://github.com/house-of-vanity/khm.git
synced 2025-07-08 07:44:08 +00:00
Improve logging and error handling
This commit is contained in:
@ -3,6 +3,7 @@ use serde::{Deserialize, Serialize};
|
||||
use std::fs::File;
|
||||
use std::io::{self, BufRead, Write};
|
||||
use std::path::Path;
|
||||
use log::{info, error};
|
||||
|
||||
#[derive(Serialize, Deserialize, Clone, Debug)]
|
||||
struct SshKey {
|
||||
@ -17,15 +18,21 @@ fn read_known_hosts(file_path: &str) -> io::Result<Vec<SshKey>> {
|
||||
|
||||
let mut keys = Vec::new();
|
||||
for line in reader.lines() {
|
||||
if let Ok(line) = line {
|
||||
let parts: Vec<&str> = line.split_whitespace().collect();
|
||||
if parts.len() >= 2 {
|
||||
let server = parts[0].to_string();
|
||||
let public_key = parts[1..].join(" ");
|
||||
keys.push(SshKey { server, public_key });
|
||||
match line {
|
||||
Ok(line) => {
|
||||
let parts: Vec<&str> = line.split_whitespace().collect();
|
||||
if parts.len() >= 2 {
|
||||
let server = parts[0].to_string();
|
||||
let public_key = parts[1..].join(" ");
|
||||
keys.push(SshKey { server, public_key });
|
||||
}
|
||||
},
|
||||
Err(e) => {
|
||||
error!("Error reading line from known_hosts file: {}", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
info!("Read {} keys from known_hosts file", keys.len());
|
||||
Ok(keys)
|
||||
}
|
||||
|
||||
@ -36,6 +43,7 @@ fn write_known_hosts(file_path: &str, keys: &[SshKey]) -> io::Result<()> {
|
||||
for key in keys {
|
||||
writeln!(file, "{} {}", key.server, key.public_key)?;
|
||||
}
|
||||
info!("Wrote {} keys to known_hosts file", keys.len());
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@ -46,9 +54,9 @@ async fn send_keys_to_server(host: &str, keys: Vec<SshKey>) -> Result<(), reqwes
|
||||
let response = client.post(&url).json(&keys).send().await?;
|
||||
|
||||
if response.status().is_success() {
|
||||
println!("Keys successfully sent to server.");
|
||||
info!("Keys successfully sent to server.");
|
||||
} else {
|
||||
eprintln!(
|
||||
error!(
|
||||
"Failed to send keys to server. Status: {}",
|
||||
response.status()
|
||||
);
|
||||
@ -64,9 +72,10 @@ async fn get_keys_from_server(host: &str) -> Result<Vec<SshKey>, reqwest::Error>
|
||||
|
||||
if response.status().is_success() {
|
||||
let keys: Vec<SshKey> = response.json().await?;
|
||||
info!("Received {} keys from server", keys.len());
|
||||
Ok(keys)
|
||||
} else {
|
||||
eprintln!(
|
||||
error!(
|
||||
"Failed to get keys from server. Status: {}",
|
||||
response.status()
|
||||
);
|
||||
@ -75,21 +84,26 @@ async fn get_keys_from_server(host: &str) -> Result<Vec<SshKey>, reqwest::Error>
|
||||
}
|
||||
|
||||
pub async fn run_client(args: crate::Args) -> std::io::Result<()> {
|
||||
info!("Client mode: Reading known_hosts file");
|
||||
let keys = read_known_hosts(&args.known_hosts).expect("Failed to read known hosts file");
|
||||
|
||||
let host = args.host.expect("host is required in client mode");
|
||||
info!("Client mode: Sending keys to server at {}", host);
|
||||
send_keys_to_server(&host, keys)
|
||||
.await
|
||||
.expect("Failed to send keys to server");
|
||||
|
||||
if args.in_place {
|
||||
info!("Client mode: In-place update is enabled. Fetching keys from server.");
|
||||
let server_keys = get_keys_from_server(&host)
|
||||
.await
|
||||
.expect("Failed to get keys from server");
|
||||
|
||||
info!("Client mode: Writing updated known_hosts file");
|
||||
write_known_hosts(&args.known_hosts, &server_keys)
|
||||
.expect("Failed to write known hosts file");
|
||||
}
|
||||
|
||||
info!("Client mode: Finished operations");
|
||||
Ok(())
|
||||
}
|
||||
|
Reference in New Issue
Block a user