extern crate virt;
use std::{env, io};
use virt::connect::{Connect, ConnectCredential, ConnectAuth};
fn main() {
let uri = match env::args().nth(1) {
Some(u) => u,
None => String::from("test+tcp://127.0.0.1/default"),
};
fn callback(creds: &mut Vec<ConnectCredential>) {
for cred in creds {
let mut input = String::new();
println!("{}:", cred.prompt);
match cred.typed {
::virt::connect::VIR_CRED_AUTHNAME => {
io::stdin().read_line(&mut input).expect("");
cred.result = Some(String::from(input.trim()));
}
::virt::connect::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![::virt::connect::VIR_CRED_AUTHNAME,
::virt::connect::VIR_CRED_PASSPHRASE],
callback);
println!("Attempting to connect to hypervisor: '{}'...", uri);
let mut conn = match Connect::open_auth(&uri, &mut auth, 0) {
Ok(c) => {
println!("Connected");
c
}
Err(e) => panic!("Not connected, code: {}, message: {}", e.code, e.message),
};
if let Err(e) = conn.close() {
panic!("Failed to disconnect from hypervisor: code {}, message: {}",
e.code,
e.message);
}
}