Fix unwraps in sys

This commit is contained in:
Jonathan Turner 2019-07-30 13:14:01 +12:00
parent 618be5de54
commit 583ef0da32

View file

@ -31,16 +31,16 @@ async fn cpu(span: Span) -> Option<Spanned<Value>> {
}
async fn mem(span: Span) -> Spanned<Value> {
let memory = memory::memory().await.unwrap();
let swap = memory::swap().await.unwrap();
let mut dict = SpannedDictBuilder::new(span);
if let Ok(memory) = memory::memory().await {
dict.insert("total", Value::bytes(memory.total().get()));
dict.insert("free", Value::bytes(memory.free().get()));
}
if let Ok(swap) = memory::swap().await {
dict.insert("swap total", Value::bytes(swap.total().get()));
dict.insert("swap free", Value::bytes(swap.free().get()));
}
dict.into_spanned_value()
}
@ -78,13 +78,13 @@ async fn host(span: Span) -> Spanned<Value> {
let mut users = heim::host::users();
let mut user_vec = vec![];
while let Some(user) = users.next().await {
let user = user.unwrap();
if let Ok(user) = user {
user_vec.push(Spanned {
item: Value::string(user.username()),
span,
});
}
}
let user_list = Value::List(user_vec);
dict.insert("users", user_list);
@ -95,11 +95,8 @@ async fn disks(span: Span) -> Value {
let mut output = vec![];
let mut partitions = disk::partitions_physical();
while let Some(part) = partitions.next().await {
let part = part.unwrap();
let usage = disk::usage(part.mount_point().to_path_buf()).await.unwrap();
if let Ok(part) = part {
let mut dict = SpannedDictBuilder::new(span);
dict.insert(
"device",
Value::string(
@ -111,12 +108,14 @@ async fn disks(span: Span) -> Value {
dict.insert("type", Value::string(part.file_system().as_str()));
dict.insert("mount", Value::string(part.mount_point().to_string_lossy()));
if let Ok(usage) = disk::usage(part.mount_point().to_path_buf()).await {
dict.insert("total", Value::bytes(usage.total().get()));
dict.insert("used", Value::bytes(usage.used().get()));
dict.insert("free", Value::bytes(usage.free().get()));
}
output.push(dict.into_spanned_value());
}
}
Value::List(output)
}
@ -190,7 +189,9 @@ impl Plugin for Sys {
})
}
fn begin_filter(&mut self, callinfo: CallInfo) -> Result<Vec<ReturnValue>, ShellError> {
Ok(block_on(sysinfo(callinfo.name_span.unwrap()))
Ok(block_on(sysinfo(
callinfo.name_span.unwrap_or_else(|| Span::unknown()),
))
.into_iter()
.map(|x| ReturnSuccess::value(x))
.collect())