view src/virsh.rs @ 17:5477c26e6984

impl sub commands that use uid
author AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
date Mon, 02 Nov 2020 16:09:53 +0900
parents 1da4937628be
children e8ba0f63c227
line wrap: on
line source

use std::io::{self, Write};
use std::process::Command;

pub struct ListDumpMsg {
    pub info_msg: String,
    pub border_line: String,
}

pub fn get_vm_list(user_name: &'static str) -> (ListDumpMsg, Vec<String>) {
    let output = Command::new("virsh")
        .arg("list")
        .arg("--all")
        .output()
        .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 = ListDumpMsg {
        info_msg: String::from(info_msg),
        border_line: String::from(border_line),
    };

    (
        ldump_msg,
        virsh_list
            .filter(|&x| x.contains(user_name))
            .map(|x| x.to_string())
            .collect(),
    )
}

pub fn command_require_vm_name(vm_name: String, operation: &str) {
    let output = Command::new("virsh")
        .arg(operation)
        .arg(vm_name)
        .output()
        .unwrap_or_else(|_| panic!("failed to {}", operation));

    io::stdout().write_all(&output.stdout).unwrap();
    io::stderr().write_all(&output.stderr).unwrap();
}