use std::{env, io};
use virt::connect::{Connect, ConnectAuth, ConnectCredential};
use virt::error::clear_error_callback;
use virt::sys;
fn main() {
clear_error_callback();
let uri = env::args().nth(1);
fn callback(creds: &mut Vec<ConnectCredential>) {
for cred in creds {
let mut input = String::new();
println!("{}:", cred.prompt);
match cred.typed as u32 {
sys::VIR_CRED_AUTHNAME => {
io::stdin().read_line(&mut input).expect("");
cred.result = Some(String::from(input.trim()));
}
sys::VIR_CRED_PASSPHRASE => {
io::stdin().read_line(&mut input).expect("");
cred.result = Some(String::from(input.trim()));
}
_ => {
panic!("Should not be here...");
}
}
}
}
let mut auth = ConnectAuth::new(
vec![sys::VIR_CRED_AUTHNAME, sys::VIR_CRED_PASSPHRASE],
callback,
);
println!("Attempting to connect to hypervisor: '{uri:?}'...");
let mut conn = match Connect::open_auth(uri.as_deref(), &mut auth, 0) {
Ok(c) => {
println!("Connected");
c
}
Err(e) => panic!("Not connected: {e}"),
};
if let Err(e) = conn.close() {
panic!("Failed to disconnect from hypervisor: {e}");
}
}