Skip to content

Commit 67435f8

Browse files
committed
refactor: Utility to load CPU template
There are (or will be) multiple places that load CPU template. Makes it as a utility function. Signed-off-by: Takahiro Itazuri <[email protected]>
1 parent 2476009 commit 67435f8

File tree

2 files changed

+18
-16
lines changed

2 files changed

+18
-16
lines changed

src/cpu-template-helper/src/main.rs

+9-16
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ use std::path::PathBuf;
77
use clap::{Parser, Subcommand, ValueEnum};
88
use vmm::cpu_config::templates::{GetCpuTemplate, GetCpuTemplateError};
99

10-
use crate::utils::UtilsError;
11-
1210
mod fingerprint;
1311
mod template;
1412
mod utils;
@@ -26,7 +24,7 @@ enum HelperError {
2624
/// Failed to serialize/deserialize JSON file: {0}
2725
Serde(#[from] serde_json::Error),
2826
/// {0}
29-
Utils(#[from] UtilsError),
27+
Utils(#[from] utils::UtilsError),
3028
/// {0}
3129
TemplateDump(#[from] template::dump::DumpError),
3230
/// {0}
@@ -127,12 +125,10 @@ fn run(cli: Cli) -> Result<(), HelperError> {
127125
write(output, cpu_config_json)?;
128126
}
129127
TemplateOperation::Strip { paths, suffix } => {
130-
let mut templates = Vec::with_capacity(paths.len());
131-
for path in &paths {
132-
let template_json = read_to_string(path)?;
133-
let template = serde_json::from_str(&template_json)?;
134-
templates.push(template);
135-
}
128+
let templates = paths
129+
.iter()
130+
.map(utils::load_cpu_template)
131+
.collect::<Result<Vec<_>, utils::UtilsError>>()?;
136132

137133
let stripped_templates = template::strip::strip(templates)?;
138134

@@ -144,13 +140,10 @@ fn run(cli: Cli) -> Result<(), HelperError> {
144140
}
145141
TemplateOperation::Verify { config, template } => {
146142
let config = config.map(read_to_string).transpose()?;
147-
let template = match template {
148-
Some(path) => {
149-
let template_json = read_to_string(path)?;
150-
Some(serde_json::from_str(&template_json)?)
151-
}
152-
None => None,
153-
};
143+
let template = template
144+
.as_ref()
145+
.map(utils::load_cpu_template)
146+
.transpose()?;
154147
let (vmm, vm_resources) = utils::build_microvm_from_config(config, template)?;
155148

156149
let cpu_template = vm_resources

src/cpu-template-helper/src/utils/mod.rs

+9
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
use std::ffi::OsString;
55
use std::fmt::Display;
6+
use std::fs::read_to_string;
67
use std::hash::Hash;
78
use std::io::Write;
89
use std::path::{Path, PathBuf};
@@ -70,6 +71,14 @@ pub enum UtilsError {
7071
CreateTempFile(#[from] vmm_sys_util::errno::Error),
7172
/// Failed to operate file: {0}
7273
FileIo(#[from] std::io::Error),
74+
/// Failed to serialize/deserialize JSON file: {0}
75+
Serde(#[from] serde_json::Error),
76+
}
77+
78+
pub fn load_cpu_template(path: &PathBuf) -> Result<CustomCpuTemplate, UtilsError> {
79+
let template_json = read_to_string(path)?;
80+
let template = serde_json::from_str(&template_json)?;
81+
Ok(template)
7382
}
7483

7584
// Utility function to prepare scratch kernel image and rootfs and build mock Firecracker config.

0 commit comments

Comments
 (0)