changeset 59:d6ef7ea91ff4

fix console cmd
author AnaTofuZ <anatofuz@gmail.com>
date Tue, 24 Nov 2020 22:04:38 +0900
parents 7b658cee53be
children 27bb45204d6d
files src/command.rs src/main.rs src/virsh.rs
diffstat 3 files changed, 17 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/src/command.rs	Tue Nov 24 12:06:21 2020 +0900
+++ b/src/command.rs	Tue Nov 24 22:04:38 2020 +0900
@@ -63,19 +63,25 @@
 }
 
 pub fn common(user_name: &str, vm_name: &str, command: &str) {
-    exec_cmd_from_name_or_id(user_name, vm_name, command)
+    let vm_name = exec_cmd_from_name_or_id(user_name, vm_name);
+    virsh::command_require_vm_name(&vm_name, command);
+}
+
+pub fn console(user_name: &str, vm_name: &str) {
+    let vm_name = exec_cmd_from_name_or_id(user_name, vm_name);
+    virsh::exec_virsh_console(&vm_name);
 }
 
 pub fn dumpvncpasswd(_user_name: &str, _vm_name: &str) {}
 
-fn exec_cmd_from_name_or_id(user_name: &str, name_or_id: &str, command: &str) {
+fn exec_cmd_from_name_or_id(user_name: &str, name_or_id: &str) -> String {
     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 {
-            return virsh::command_require_vm_name(&vm_name, command);
+            return vm_name;
         }
     }
-    virsh::command_require_vm_name(&get_vm_name(user_name, name_or_id), command);
+    get_vm_name(user_name, name_or_id)
 }
 
 fn get_vm_name_from_id(user_name: &str, arg_id: &str) -> Option<String> {
--- a/src/main.rs	Tue Nov 24 12:06:21 2020 +0900
+++ b/src/main.rs	Tue Nov 24 22:04:38 2020 +0900
@@ -152,7 +152,7 @@
 
         SubCommand::Console(arg) => {
             user::set_root_id();
-            command::common(&user_name, &arg.name, "console");
+            command::console(&user_name, &arg.name);
         }
 
         SubCommand::Destroy(arg) => {
--- a/src/virsh.rs	Tue Nov 24 12:06:21 2020 +0900
+++ b/src/virsh.rs	Tue Nov 24 22:04:38 2020 +0900
@@ -1,4 +1,5 @@
 use std::io::{self, Write};
+use std::os::unix::process::CommandExt;
 use std::process::Command;
 
 use std::env;
@@ -69,6 +70,11 @@
     io::stderr().write_all(&output.stderr).unwrap();
 }
 
+pub fn exec_virsh_console(vm_name: &str) {
+    let err = Command::new("virsh").arg("console").arg(vm_name).exec();
+    println!("after exec !!{}", err);
+}
+
 pub fn generate_qemu_from_template(vm_path_string: &str, vm_name: &str, template_path: &str) {
     let vm_path = Path::new(&vm_path_string);
     let ok = env::set_current_dir(&vm_path);