Mercurial > hg > Members > anatofuz > ie-virsh
changeset 7:07b763b84401
rebarse use macro
author | AnaTofuZ <anatofuz@gmail.com> |
---|---|
date | Wed, 28 Oct 2020 16:32:25 +0900 |
parents | 08247e54d78d |
children | 017344e337e8 |
files | src/main.rs |
diffstat | 1 files changed, 34 insertions(+), 19 deletions(-) [+] |
line wrap: on
line diff
--- a/src/main.rs Tue Oct 27 17:17:24 2020 +0900 +++ b/src/main.rs Wed Oct 28 16:32:25 2020 +0900 @@ -3,6 +3,7 @@ use std::process::Command; #[derive(Clap)] +#[clap(version = "1.0", author = "AnaTofuZ <anatofuz@cr.ie.u-ryukyu.ac.jp>")] struct Opts { #[clap(subcommand)] subcmd: SubCommand, @@ -12,18 +13,30 @@ enum SubCommand { define(Define), shutdown(Shutdown), + list(List), } +/// define (but don't start) a domain from an template XML file #[derive(Clap)] struct Define { name: String, } +/// gracefully shutdown a domain #[derive(Clap)] struct Shutdown { name: String, } +/// list domains +#[derive(Clap)] +struct List {} + +struct list_dump_msg { + info_msg: String, + border_line: String, +} + struct VM { id: u32, name: String, @@ -31,28 +44,26 @@ } fn main() { + let opts: Opts = Opts::parse(); + let uid = getuid(); let gid = getgid(); let user_name = getlogin(uid); println!("uid: {} gid: {} name: {}", uid, gid, user_name); - let matches = App::new("ie-virsh") - .version("1.0") - .author("AnatofuZ <anatofuz@cr.ie.u-ryukyu.ac.jp>") - .about("virsh wrapper for ie members") - .get_matches(); + match opts.subcmd { + SubCommand::define(name) => {} + SubCommand::shutdown(name) => {} - /* - let opts: Opts = Opts::parse(); - - match opts.subcmd { - SubCommand::define(name) => { - }, - SubCommand::shutdown(name) => { - + SubCommand::list(list) => { + set_root_id(); + let (vm_list, list_dump_msg) = list_command(user_name); + println!("{}\n{}", list_dump_msg.info_msg, list_dump_msg.border_line); + for vm in vm_list.iter() { + println!("{} {}", vm.id, vm.name); + } } } - */ //set_root_id(); //list_command(user_name); @@ -75,7 +86,7 @@ return gid_struct.into(); } -fn list_command(user_name: &'static str) { +fn list_command(user_name: &'static str) -> (Vec<VM>, list_dump_msg) { let output = Command::new("virsh") .arg("list") .arg("--all") @@ -83,26 +94,30 @@ .expect("failed to virsh"); let virsh_list = String::from_utf8_lossy(&output.stdout); let mut virsh_list = virsh_list.split("\n"); + let info_msg = virsh_list.next().unwrap(); let border_line = virsh_list.next().unwrap(); + let ldump_msg = list_dump_msg { + info_msg: String::from(info_msg), + border_line: String::from(border_line), + }; let virsh_emit_vm_list = virsh_list.filter(|&x| x.contains(user_name)); let mut vm_list: Vec<VM> = Vec::new(); - println!("{}\n{}", info_msg, border_line); for emit_vm in virsh_emit_vm_list { let mut vm_infos = emit_vm.split(" "); let vm_status = vm_infos.next().unwrap(); let is_vm_running = if vm_status != "-" { true } else { false }; let name = vm_infos.next().unwrap(); - let vm = VM { + vm_list.push(VM { id: 0, is_vm_running: is_vm_running, name: String::from(name), - }; - vm_list.push(vm); + }); } + return (vm_list, ldump_msg); } fn set_root_id() {