agent: Use ok_or_else instead of match for Option -> Result

Using ok_or is clearer than match.

Signed-off-by: Tim Zhang <tim@hyper.sh>
This commit is contained in:
Tim Zhang 2020-10-13 19:14:43 +08:00
parent 0dce817ebb
commit 1d8def6663
3 changed files with 22 additions and 29 deletions

View File

@ -612,12 +612,7 @@ fn do_init_child(cwfd: RawFd) -> Result<()> {
let exec_file = Path::new(&args[0]); let exec_file = Path::new(&args[0]);
log_child!(cfd_log, "process command: {:?}", &args); log_child!(cfd_log, "process command: {:?}", &args);
if !exec_file.exists() { if !exec_file.exists() {
match find_file(exec_file) { find_file(exec_file).ok_or_else(|| anyhow!("the file {} is not exist", &args[0]))?;
Some(_) => (),
None => {
return Err(anyhow!("the file {} is not exist", &args[0]));
}
}
} }
// notify parent that the child's ready to start // notify parent that the child's ready to start
@ -1047,10 +1042,10 @@ fn do_exec(args: &[String]) -> ! {
fn update_namespaces(logger: &Logger, spec: &mut Spec, init_pid: RawFd) -> Result<()> { fn update_namespaces(logger: &Logger, spec: &mut Spec, init_pid: RawFd) -> Result<()> {
info!(logger, "updating namespaces"); info!(logger, "updating namespaces");
let linux = match spec.linux.as_mut() { let linux = spec
None => return Err(anyhow!("Spec didn't contain linux field")), .linux
Some(l) => l, .as_mut()
}; .ok_or_else(|| anyhow!("Spec didn't contain linux field"))?;
let namespaces = linux.namespaces.as_mut_slice(); let namespaces = linux.namespaces.as_mut_slice();
for namespace in namespaces.iter_mut() { for namespace in namespaces.iter_mut() {

View File

@ -204,10 +204,10 @@ fn update_spec_device_list(device: &Device, spec: &mut Spec) -> Result<()> {
)); ));
} }
let linux = match spec.linux.as_mut() { let linux = spec
None => return Err(anyhow!("Spec didn't container linux field")), .linux
Some(l) => l, .as_mut()
}; .ok_or_else(|| anyhow!("Spec didn't container linux field"))?;
if !Path::new(&device.vm_path).exists() { if !Path::new(&device.vm_path).exists() {
return Err(anyhow!("vm_path:{} doesn't exist", device.vm_path)); return Err(anyhow!("vm_path:{} doesn't exist", device.vm_path));
@ -365,10 +365,10 @@ pub fn update_device_cgroup(spec: &mut Spec) -> Result<()> {
let major = stat::major(rdev) as i64; let major = stat::major(rdev) as i64;
let minor = stat::minor(rdev) as i64; let minor = stat::minor(rdev) as i64;
let linux = match spec.linux.as_mut() { let linux = spec
None => return Err(anyhow!("Spec didn't container linux field")), .linux
Some(l) => l, .as_mut()
}; .ok_or_else(|| anyhow!("Spec didn't container linux field"))?;
if linux.resources.is_none() { if linux.resources.is_none() {
linux.resources = Some(LinuxResources::default()); linux.resources = Some(LinuxResources::default());

View File

@ -446,15 +446,14 @@ pub fn add_storages(
"subsystem" => "storage", "subsystem" => "storage",
"storage-type" => handler_name.to_owned())); "storage-type" => handler_name.to_owned()));
let handler = match STORAGEHANDLERLIST.get(&handler_name.as_str()) { let handler = STORAGEHANDLERLIST
None => { .get(&handler_name.as_str())
return Err(anyhow!( .ok_or_else(|| {
anyhow!(
"Failed to find the storage handler {}", "Failed to find the storage handler {}",
storage.driver.to_owned() storage.driver.to_owned()
)); )
} })?;
Some(f) => f,
};
let mount_point = match handler(&logger, &storage, sandbox.clone()) { let mount_point = match handler(&logger, &storage, sandbox.clone()) {
// Todo need to rollback the mounted storage if err met. // Todo need to rollback the mounted storage if err met.
@ -659,10 +658,9 @@ pub fn remove_mounts(mounts: &Vec<String>) -> Result<()> {
fn ensure_destination_exists(destination: &str, fs_type: &str) -> Result<()> { fn ensure_destination_exists(destination: &str, fs_type: &str) -> Result<()> {
let d = Path::new(destination); let d = Path::new(destination);
if !d.exists() { if !d.exists() {
let dir = match d.parent() { let dir = d
Some(d) => d, .parent()
None => return Err(anyhow!("mount destination {} doesn't exist", destination)), .ok_or_else(|| anyhow!("mount destination {} doesn't exist", destination))?;
};
if !dir.exists() { if !dir.exists() {
fs::create_dir_all(dir).context(format!("create dir all failed on {:?}", dir))?; fs::create_dir_all(dir).context(format!("create dir all failed on {:?}", dir))?;
} }