changeset 28:1c3b6b6ee0b1

add show template
author AnaTofuZ <anatofuz@gmail.com>
date Sat, 21 Nov 2020 16:37:58 +0900
parents c0f0181ee91a
children 0b43942c8950
files src/command.rs src/main.rs
diffstat 2 files changed, 37 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/src/command.rs	Fri Nov 20 14:01:36 2020 +0900
+++ b/src/command.rs	Sat Nov 21 16:37:58 2020 +0900
@@ -2,6 +2,12 @@
 use super::virsh;
 use super::xml;
 
+use std::{fs, io};
+
+const TEMPLATE_DIR: &str = "/ie-ryukyu/kvm/images/templates/";
+const TEMPLATE_SUFFIX: &str = "template-";
+const TEMPLATE_FILE_EXTENSION: &str = ".qcow2";
+
 pub fn list(user_name: &str) {
     let (ldump_msg, vm_list_strs) = virsh::get_vm_list(user_name);
     println!("{}", ldump_msg.info_msg);
@@ -11,6 +17,26 @@
     }
 }
 
+pub fn templates() -> io::Result<()> {
+    let mut entries = fs::read_dir(TEMPLATE_DIR)?
+        .map(|res| res.map(|e| e.path()))
+        .collect::<Result<Vec<_>, io::Error>>()?;
+
+    entries.sort();
+
+    for entry in entries {
+        let template_vm = entry
+            .display()
+            .to_string()
+            .replace(TEMPLATE_DIR, "")
+            .replace(TEMPLATE_SUFFIX, "")
+            .replace(TEMPLATE_FILE_EXTENSION, "");
+        println!("{}", template_vm);
+    }
+
+    Ok(())
+}
+
 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);
--- a/src/main.rs	Fri Nov 20 14:01:36 2020 +0900
+++ b/src/main.rs	Sat Nov 21 16:37:58 2020 +0900
@@ -23,8 +23,13 @@
     DefineGDB(DefineGDB),
     Domiflist(Domiflist),
     Dominfo(Dominfo),
+    Templates(Templates),
 }
 
+/// show templates vm
+#[derive(Clap)]
+struct Templates {}
+
 /// define the domain in which the gdb port is opened from the template XML file
 #[derive(Clap)]
 struct DefineGDB {
@@ -175,6 +180,12 @@
             command::dominfo(&user_name, arg.name);
         }
 
+        SubCommand::Templates(_) => {
+            if let Err(e) = command::templates() {
+                println!("{}", e);
+            }
+        }
+
         _ => {}
     }
 }