Merge pull request #228 from jonathandturner/sys_unwrap_refactor

Fix unwraps in sys
This commit is contained in:
Jonathan Turner 2019-07-30 13:30:11 +12:00 committed by GitHub
commit 2e84276d68
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

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);
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(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,12 +78,12 @@ 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();
user_vec.push(Spanned {
item: Value::string(user.username()),
span,
});
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,27 +95,26 @@ 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(
part.device()
.unwrap_or_else(|| OsStr::new("N/A"))
.to_string_lossy(),
),
);
let mut dict = SpannedDictBuilder::new(span);
dict.insert(
"device",
Value::string(
part.device()
.unwrap_or_else(|| OsStr::new("N/A"))
.to_string_lossy(),
),
);
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());
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,10 +189,12 @@ impl Plugin for Sys {
})
}
fn begin_filter(&mut self, callinfo: CallInfo) -> Result<Vec<ReturnValue>, ShellError> {
Ok(block_on(sysinfo(callinfo.name_span.unwrap()))
.into_iter()
.map(|x| ReturnSuccess::value(x))
.collect())
Ok(block_on(sysinfo(
callinfo.name_span.unwrap_or_else(|| Span::unknown()),
))
.into_iter()
.map(|x| ReturnSuccess::value(x))
.collect())
}
fn filter(&mut self, _: Spanned<Value>) -> Result<Vec<ReturnValue>, ShellError> {