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