main: Move VmParams creation into a dedicated function

This brings more modularity to the code, which will be helpful when we
will later test the CLI and OpenAPI generate the same VmConfig output.

Signed-off-by: Sebastien Boeuf <sebastien.boeuf@intel.com>
This commit is contained in:
Sebastien Boeuf 2019-12-12 18:12:42 +01:00
parent 17a167dbb6
commit 43bd0e53c4
4 changed files with 48 additions and 39 deletions

1
Cargo.lock generated
View File

@ -1114,6 +1114,7 @@ dependencies = [
"acpi_tables 0.1.0",
"anyhow 1.0.25 (registry+https://github.com/rust-lang/crates.io-index)",
"arch 0.1.0",
"clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)",
"devices 0.1.0",
"epoll 4.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
"kvm-bindings 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",

View File

@ -287,28 +287,7 @@ fn main() {
)
.get_matches();
// These .unwrap()s cannot fail as there is a default value defined
let cpus = cmd_arguments.value_of("cpus").unwrap();
let memory = cmd_arguments.value_of("memory").unwrap();
let rng = cmd_arguments.value_of("rng").unwrap();
let serial = cmd_arguments.value_of("serial").unwrap();
let kernel = cmd_arguments.value_of("kernel");
let cmdline = cmd_arguments.value_of("cmdline");
let disks: Option<Vec<&str>> = cmd_arguments.values_of("disk").map(|x| x.collect());
let net: Option<Vec<&str>> = cmd_arguments.values_of("net").map(|x| x.collect());
let console = cmd_arguments.value_of("console").unwrap();
let fs: Option<Vec<&str>> = cmd_arguments.values_of("fs").map(|x| x.collect());
let pmem: Option<Vec<&str>> = cmd_arguments.values_of("pmem").map(|x| x.collect());
let devices: Option<Vec<&str>> = cmd_arguments.values_of("device").map(|x| x.collect());
let vhost_user_net: Option<Vec<&str>> = cmd_arguments
.values_of("vhost-user-net")
.map(|x| x.collect());
let vhost_user_blk: Option<Vec<&str>> = cmd_arguments
.values_of("vhost-user-blk")
.map(|x| x.collect());
let vsock: Option<Vec<&str>> = cmd_arguments.values_of("vsock").map(|x| x.collect());
let vm_params = config::VmParams::from_arg_matches(&cmd_arguments);
let log_level = match cmd_arguments.occurrences_of("v") {
0 => LevelFilter::Error,
@ -334,23 +313,7 @@ fn main() {
.map(|()| log::set_max_level(log_level))
.expect("Expected to be able to setup logger");
let vm_config = match config::VmConfig::parse(config::VmParams {
cpus,
memory,
kernel,
cmdline,
disks,
net,
rng,
fs,
pmem,
serial,
console,
devices,
vhost_user_net,
vhost_user_blk,
vsock,
}) {
let vm_config = match config::VmConfig::parse(vm_params) {
Ok(config) => config,
Err(e) => {
println!("Failed parsing parameters {:?}", e);

View File

@ -12,6 +12,7 @@ mmio_support = ["vm-virtio/mmio_support"]
cmos = ["devices/cmos"]
[dependencies]
clap = "2.33.0"
acpi_tables = { path = "../acpi_tables", optional = true }
anyhow = "1.0"
arch = { path = "../arch" }

View File

@ -5,6 +5,7 @@
extern crate vm_virtio;
use clap::ArgMatches;
use net_util::MacAddr;
use std::convert::From;
use std::net::AddrParseError;
@ -106,6 +107,49 @@ pub struct VmParams<'a> {
pub vsock: Option<Vec<&'a str>>,
}
impl<'a> VmParams<'a> {
pub fn from_arg_matches(args: &'a ArgMatches) -> Self {
// These .unwrap()s cannot fail as there is a default value defined
let cpus = args.value_of("cpus").unwrap();
let memory = args.value_of("memory").unwrap();
let rng = args.value_of("rng").unwrap();
let serial = args.value_of("serial").unwrap();
let kernel = args.value_of("kernel");
let cmdline = args.value_of("cmdline");
let disks: Option<Vec<&str>> = args.values_of("disk").map(|x| x.collect());
let net: Option<Vec<&str>> = args.values_of("net").map(|x| x.collect());
let console = args.value_of("console").unwrap();
let fs: Option<Vec<&str>> = args.values_of("fs").map(|x| x.collect());
let pmem: Option<Vec<&str>> = args.values_of("pmem").map(|x| x.collect());
let devices: Option<Vec<&str>> = args.values_of("device").map(|x| x.collect());
let vhost_user_net: Option<Vec<&str>> =
args.values_of("vhost-user-net").map(|x| x.collect());
let vhost_user_blk: Option<Vec<&str>> =
args.values_of("vhost-user-blk").map(|x| x.collect());
let vsock: Option<Vec<&str>> = args.values_of("vsock").map(|x| x.collect());
VmParams {
cpus,
memory,
kernel,
cmdline,
disks,
net,
rng,
fs,
pmem,
serial,
console,
devices,
vhost_user_net,
vhost_user_blk,
vsock,
}
}
}
fn parse_size(size: &str) -> Result<u64> {
let s = size.trim();