Mercurial > hg > Members > anatofuz > ie-virsh
changeset 24:e8ba0f63c227
remove static variable
author | AnaTofuZ <anatofuz@gmail.com> |
---|---|
date | Tue, 03 Nov 2020 18:37:04 +0900 |
parents | 78f246ce5235 |
children | da27437a94b0 |
files | src/command.rs src/main.rs src/user.rs src/virsh.rs src/xml.rs |
diffstat | 5 files changed, 46 insertions(+), 34 deletions(-) [+] |
line wrap: on
line diff
--- a/src/command.rs Tue Nov 03 17:44:19 2020 +0900 +++ b/src/command.rs Tue Nov 03 18:37:04 2020 +0900 @@ -1,7 +1,8 @@ use super::virsh; use super::xml; +use super::user; -pub fn list(user_name: &'static str) { +pub fn list(user_name: &str) { let (ldump_msg, vm_list_strs) = virsh::get_vm_list(user_name); println!("{}", ldump_msg.info_msg); println!("{}", ldump_msg.border_line); @@ -10,56 +11,56 @@ } } -pub fn define(user_name: &'static str, vm_name: String) { - let vm_name = generate_vm_name(user_name, &vm_name); - let vm_arg = xml::GenerateVMArg::new(user_name, &vm_name, false); +pub fn define(user : &user::User , vm_name: String) { + let vm_name = generate_vm_name(&user.Name, &vm_name); + let vm_arg = xml::GenerateVMArg::new(&user.Name, &vm_name, false); let result = vm_arg.generate(); if result.is_ok() { virsh::command_require_vm_name(result.unwrap(), "define") } } -pub fn start(user_name: &'static str, vm_name: String) { +pub fn start(user_name: &str, vm_name: String) { virsh::command_require_vm_name(get_vm_name(user_name, &vm_name), "start"); } -pub fn shutdown(user_name: &'static str, vm_name: String) { +pub fn shutdown(user_name: &str, vm_name: String) { exec_cmd_from_name_or_id(user_name, &vm_name, "shutdown") } -pub fn destroy(user_name: &'static str, vm_name: String) { +pub fn destroy(user_name: &str, vm_name: String) { exec_cmd_from_name_or_id(user_name, &vm_name, "destroy") } -pub fn console(user_name: &'static str, vm_name: String) { +pub fn console(user_name: &str, vm_name: String) { exec_cmd_from_name_or_id(user_name, &vm_name, "console") } -pub fn vncdisplay(user_name: &'static str, vm_name: String) { +pub fn vncdisplay(user_name: &str, vm_name: String) { exec_cmd_from_name_or_id(user_name, &vm_name, "vncdisplay") } -pub fn ttyconsole(user_name: &'static str, vm_name: String) { +pub fn ttyconsole(user_name: &str, vm_name: String) { exec_cmd_from_name_or_id(user_name, &vm_name, "ttyconsole") } -pub fn dumpxml(user_name: &'static str, vm_name: String) { +pub fn dumpxml(user_name: &str, vm_name: String) { exec_cmd_from_name_or_id(user_name, &vm_name, "dumpxml") } -pub fn undefine(user_name: &'static str, vm_name: String) { +pub fn undefine(user_name: &str, vm_name: String) { exec_cmd_from_name_or_id(user_name, &vm_name, "undefine") } -pub fn domiflist(user_name: &'static str, vm_name: String) { +pub fn domiflist(user_name: &str, vm_name: String) { exec_cmd_from_name_or_id(user_name, &vm_name, "domiflist") } -pub fn dominfo(user_name: &'static str, vm_name: String) { +pub fn dominfo(user_name: &str, vm_name: String) { exec_cmd_from_name_or_id(user_name, &vm_name, "dominfo") } -fn exec_cmd_from_name_or_id(user_name: &'static str, name_or_id: &str, command: &str) { +fn exec_cmd_from_name_or_id(user_name: &str, name_or_id: &str, command: &str) { if name_or_id.parse::<u8>().is_ok() { let vm_name_or_none = get_vm_name_from_id(user_name, &name_or_id); if let Some(vm_name) = vm_name_or_none { @@ -69,7 +70,7 @@ virsh::command_require_vm_name(get_vm_name(user_name, name_or_id), command); } -fn get_vm_name_from_id(user_name: &'static str, arg_id: &str) -> Option<String> { +fn get_vm_name_from_id(user_name: &str, arg_id: &str) -> Option<String> { let (_, vm_list_strs) = virsh::get_vm_list(user_name); for vm in vm_list_strs { let mut splitted = vm.split_whitespace(); @@ -82,7 +83,7 @@ None } -fn get_vm_name(user_name: &'static str, vm_name: &str) -> String { +fn get_vm_name(user_name: &str, vm_name: &str) -> String { let vm_name = match vm_name.parse::<u8>() { Ok(_) => format!("{}-{}", user_name, vm_name), Err(_) => vm_name.to_string(), @@ -95,7 +96,7 @@ } -fn generate_vm_name(user_name:&'static str, vm_name: &str) -> String { +fn generate_vm_name(user_name:&str, vm_name: &str) -> String { if vm_name.starts_with(user_name) { return vm_name.to_string(); }
--- a/src/main.rs Tue Nov 03 17:44:19 2020 +0900 +++ b/src/main.rs Tue Nov 03 18:37:04 2020 +0900 @@ -112,62 +112,67 @@ match opts.subcmd { SubCommand::List(_) => { user::set_root_id(); - command::list(user_name); + command::list(&user_name); } SubCommand::Start(arg) => { user::set_root_id(); - command::start(user_name, arg.name); + command::start(&user_name, arg.name); } SubCommand::Define(arg) => { user::set_root_id(); - command::define(user_name, arg.name); + let user = user::User{ + Uid: uid, + Gid: gid, + Name: user_name.to_string() + }; + command::define(&user, arg.name); } SubCommand::Shutdown(arg) => { user::set_root_id(); - command::shutdown(user_name, arg.name); + command::shutdown(&user_name, arg.name); } SubCommand::Console(arg) => { user::set_root_id(); - command::console(user_name, arg.name); + command::console(&user_name, arg.name); } SubCommand::Destroy(arg) => { user::set_root_id(); - command::destroy(user_name, arg.name); + command::destroy(&user_name, arg.name); } SubCommand::VNCDisplay(arg) => { user::set_root_id(); - command::vncdisplay(user_name, arg.name); + command::vncdisplay(&user_name, arg.name); } SubCommand::Ttyconsole(arg) => { user::set_root_id(); - command::ttyconsole(user_name, arg.name); + command::ttyconsole(&user_name, arg.name); } SubCommand::Dumpxml(arg) => { user::set_root_id(); - command::dumpxml(user_name, arg.name); + command::dumpxml(&user_name, arg.name); } SubCommand::Undefine(arg) => { user::set_root_id(); - command::undefine(user_name, arg.name); + command::undefine(&user_name, arg.name); } SubCommand::Domiflist(arg) => { user::set_root_id(); - command::domiflist(user_name, arg.name); + command::domiflist(&user_name, arg.name); } SubCommand::Dominfo(arg) => { user::set_root_id(); - command::dominfo(user_name, arg.name); + command::dominfo(&user_name, arg.name); } _ => {}
--- a/src/user.rs Tue Nov 03 17:44:19 2020 +0900 +++ b/src/user.rs Tue Nov 03 18:37:04 2020 +0900 @@ -1,8 +1,14 @@ -pub fn getlogin(uid: u32) -> &'static str { +pub struct User { + pub Uid: u32, + pub Gid: u32, + pub Name: String +} + +pub fn getlogin(uid: u32) -> String { use std::ffi::CStr; let user_passwd = unsafe { nix::libc::getpwuid(uid) }; let c_str = unsafe { CStr::from_ptr((*user_passwd).pw_name) }; - c_str.to_str().unwrap() + c_str.to_str().unwrap().to_string() } pub fn getuid() -> u32 {
--- a/src/virsh.rs Tue Nov 03 17:44:19 2020 +0900 +++ b/src/virsh.rs Tue Nov 03 18:37:04 2020 +0900 @@ -6,7 +6,7 @@ pub border_line: String, } -pub fn get_vm_list(user_name: &'static str) -> (ListDumpMsg, Vec<String>) { +pub fn get_vm_list(user_name: &str) -> (ListDumpMsg, Vec<String>) { let output = Command::new("virsh") .arg("list") .arg("--all")
--- a/src/xml.rs Tue Nov 03 17:44:19 2020 +0900 +++ b/src/xml.rs Tue Nov 03 18:37:04 2020 +0900 @@ -39,7 +39,7 @@ } impl GenerateVMArg { - pub fn new(user_name: &'static str, vm_name: &str, is_debug: bool) -> GenerateVMArg { + pub fn new(user_name: &str, vm_name: &str, is_debug: bool) -> GenerateVMArg { let year = user_name.chars().skip(1).take(2).collect::<String>(); let affilication = if year.parse::<u8>().is_ok() { // /etc/libvirt/qemu/e19/e195729