changeset 32:ae7ba39dfcbe

return the template vec
author AnaTofuZ <anatofuz@gmail.com>
date Sat, 21 Nov 2020 17:14:56 +0900
parents 26111ba2fea1
children ba66504b5256 0376dbfaa7ed
files src/command.rs src/virsh.rs
diffstat 2 files changed, 29 insertions(+), 22 deletions(-) [+]
line wrap: on
line diff
--- a/src/command.rs	Sat Nov 21 16:59:29 2020 +0900
+++ b/src/command.rs	Sat Nov 21 17:14:56 2020 +0900
@@ -2,11 +2,7 @@
 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";
+use std::io;
 
 pub fn list(user_name: &str) {
     let (ldump_msg, vm_list_strs) = virsh::get_vm_list(user_name);
@@ -17,24 +13,11 @@
     }
 }
 
-pub fn templates() -> io::Result<()> {
-    let mut entries = fs::read_dir(TEMPLATE_DIR)?
-        .map(|res| {
-            res.map(|e| {
-                e.path()
-                    .display()
-                    .to_string()
-                    .replace(TEMPLATE_DIR, "")
-                    .replace(TEMPLATE_SUFFIX, "")
-                    .replace(TEMPLATE_FILE_EXTENSION, "")
-            })
-        })
-        .collect::<Result<Vec<_>, io::Error>>()?;
+pub fn templates() -> Result<(), io::Error> {
+    let templates_list = virsh::get_template_list()?;
 
-    entries.sort();
-
-    for entry in entries {
-        println!("{}", entry);
+    for name in templates_list {
+        println!("{}", name);
     }
 
     Ok(())
--- a/src/virsh.rs	Sat Nov 21 16:59:29 2020 +0900
+++ b/src/virsh.rs	Sat Nov 21 17:14:56 2020 +0900
@@ -1,11 +1,35 @@
 use std::io::{self, Write};
 use std::process::Command;
 
+use std::fs;
+
+const TEMPLATE_DIR: &str = "/ie-ryukyu/kvm/images/templates/";
+const TEMPLATE_SUFFIX: &str = "template-";
+const TEMPLATE_FILE_EXTENSION: &str = ".qcow2";
+
 pub struct ListDumpMsg {
     pub info_msg: String,
     pub border_line: String,
 }
 
+pub fn get_template_list() -> Result<Vec<String>, io::Error> {
+    let mut entries = fs::read_dir(TEMPLATE_DIR)?
+        .map(|res| {
+            res.map(|e| {
+                e.path()
+                    .display()
+                    .to_string()
+                    .replace(TEMPLATE_DIR, "")
+                    .replace(TEMPLATE_SUFFIX, "")
+                    .replace(TEMPLATE_FILE_EXTENSION, "")
+            })
+        })
+        .collect::<Result<Vec<_>, io::Error>>()?;
+
+    entries.sort();
+    Ok(entries)
+}
+
 pub fn get_vm_list(user_name: &str) -> (ListDumpMsg, Vec<String>) {
     let output = Command::new("virsh")
         .arg("list")