forked from extern/nushell
Move sys, ps, fetch, post to internal commands (#3983)
* Move sys, ps, fetch, post to internal commands * Remove old plugins * clippy Co-authored-by: JT <jonatha.d.turner@gmail.com>
This commit is contained in:
@ -17,6 +17,7 @@ mod platform;
|
||||
mod random;
|
||||
mod shells;
|
||||
mod strings;
|
||||
mod system;
|
||||
mod viewers;
|
||||
|
||||
pub use charting::*;
|
||||
@ -55,6 +56,7 @@ pub use platform::*;
|
||||
pub use random::*;
|
||||
pub use shells::*;
|
||||
pub use strings::*;
|
||||
pub use system::*;
|
||||
pub use viewers::*;
|
||||
|
||||
#[cfg(test)]
|
||||
|
369
crates/nu-command/src/commands/network/fetch.rs
Normal file
369
crates/nu-command/src/commands/network/fetch.rs
Normal file
@ -0,0 +1,369 @@
|
||||
use crate::prelude::*;
|
||||
use base64::encode;
|
||||
use nu_engine::WholeStreamCommand;
|
||||
use nu_errors::ShellError;
|
||||
use nu_protocol::{CommandAction, ReturnSuccess, ReturnValue, Value};
|
||||
use nu_protocol::{Signature, SyntaxShape, UntaggedValue};
|
||||
use nu_source::{AnchorLocation, Span, Tag};
|
||||
use std::path::PathBuf;
|
||||
use std::str::FromStr;
|
||||
|
||||
pub struct Command;
|
||||
|
||||
impl WholeStreamCommand for Command {
|
||||
fn name(&self) -> &str {
|
||||
"fetch"
|
||||
}
|
||||
|
||||
fn signature(&self) -> Signature {
|
||||
Signature::build("fetch")
|
||||
.desc("Load from a URL into a cell, convert to table if possible (avoid by appending '--raw').")
|
||||
.required(
|
||||
"URL",
|
||||
SyntaxShape::String,
|
||||
"the URL to fetch the contents from",
|
||||
)
|
||||
.named(
|
||||
"user",
|
||||
SyntaxShape::Any,
|
||||
"the username when authenticating",
|
||||
Some('u'),
|
||||
)
|
||||
.named(
|
||||
"password",
|
||||
SyntaxShape::Any,
|
||||
"the password when authenticating",
|
||||
Some('p'),
|
||||
)
|
||||
.switch("raw", "fetch contents as text rather than a table", Some('r'))
|
||||
.filter()
|
||||
}
|
||||
|
||||
fn usage(&self) -> &str {
|
||||
"Fetch the contents from a URL (HTTP GET operation)."
|
||||
}
|
||||
|
||||
fn run_with_actions(&self, args: CommandArgs) -> Result<ActionStream, ShellError> {
|
||||
run_fetch(args)
|
||||
}
|
||||
|
||||
fn examples(&self) -> Vec<Example> {
|
||||
vec![
|
||||
Example {
|
||||
description: "Fetch content from url.com",
|
||||
example: "fetch url.com",
|
||||
result: None,
|
||||
},
|
||||
Example {
|
||||
description: "Fetch content from url.com, with username and password",
|
||||
example: "fetch -u myuser -p mypass url.com",
|
||||
result: None,
|
||||
},
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
fn run_fetch(args: CommandArgs) -> Result<ActionStream, ShellError> {
|
||||
let mut fetch_helper = Fetch::new();
|
||||
|
||||
fetch_helper.setup(args)?;
|
||||
|
||||
let runtime = tokio::runtime::Runtime::new()?;
|
||||
Ok(vec![runtime.block_on(fetch(
|
||||
&fetch_helper.path.clone().ok_or_else(|| {
|
||||
ShellError::labeled_error(
|
||||
"internal error: path not set",
|
||||
"path not set",
|
||||
&fetch_helper.tag,
|
||||
)
|
||||
})?,
|
||||
fetch_helper.has_raw,
|
||||
fetch_helper.user.clone(),
|
||||
fetch_helper.password,
|
||||
))]
|
||||
.into_iter()
|
||||
.into_action_stream())
|
||||
|
||||
//fetch.setup(callinfo)?;
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct Fetch {
|
||||
pub path: Option<Value>,
|
||||
pub tag: Tag,
|
||||
pub has_raw: bool,
|
||||
pub user: Option<String>,
|
||||
pub password: Option<String>,
|
||||
}
|
||||
|
||||
impl Fetch {
|
||||
pub fn new() -> Fetch {
|
||||
Fetch {
|
||||
path: None,
|
||||
tag: Tag::unknown(),
|
||||
has_raw: false,
|
||||
user: None,
|
||||
password: None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn setup(&mut self, args: CommandArgs) -> Result<(), ShellError> {
|
||||
self.path = Some({
|
||||
args.req(0).map_err(|_| {
|
||||
ShellError::labeled_error(
|
||||
"No file or directory specified",
|
||||
"for command",
|
||||
&args.name_tag(),
|
||||
)
|
||||
})?
|
||||
});
|
||||
self.tag = args.name_tag();
|
||||
|
||||
self.has_raw = args.has_flag("raw");
|
||||
|
||||
self.user = args.get_flag("user")?;
|
||||
|
||||
self.password = args.get_flag("password")?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn fetch(
|
||||
path: &Value,
|
||||
has_raw: bool,
|
||||
user: Option<String>,
|
||||
password: Option<String>,
|
||||
) -> ReturnValue {
|
||||
let path_str = path.as_string()?;
|
||||
let path_span = path.tag.span;
|
||||
|
||||
let result = helper(&path_str, path_span, has_raw, user, password).await;
|
||||
|
||||
if let Err(e) = result {
|
||||
return Err(e);
|
||||
}
|
||||
let (file_extension, value) = result?;
|
||||
|
||||
let file_extension = if has_raw {
|
||||
None
|
||||
} else {
|
||||
// If the extension could not be determined via mimetype, try to use the path
|
||||
// extension. Some file types do not declare their mimetypes (such as bson files).
|
||||
file_extension.or_else(|| path_str.split('.').last().map(String::from))
|
||||
};
|
||||
|
||||
if let Some(extension) = file_extension {
|
||||
Ok(ReturnSuccess::Action(CommandAction::AutoConvert(
|
||||
value, extension,
|
||||
)))
|
||||
} else {
|
||||
ReturnSuccess::value(value)
|
||||
}
|
||||
}
|
||||
|
||||
// Helper function that actually goes to retrieve the resource from the url given
|
||||
// The Option<String> return a possible file extension which can be used in AutoConvert commands
|
||||
async fn helper(
|
||||
location: &str,
|
||||
span: Span,
|
||||
has_raw: bool,
|
||||
user: Option<String>,
|
||||
password: Option<String>,
|
||||
) -> std::result::Result<(Option<String>, Value), ShellError> {
|
||||
let url = match url::Url::parse(location) {
|
||||
Ok(u) => u,
|
||||
Err(e) => {
|
||||
return Err(ShellError::labeled_error(
|
||||
format!("Incomplete or incorrect url:\n{:?}", e),
|
||||
"expected a full url",
|
||||
span,
|
||||
));
|
||||
}
|
||||
};
|
||||
|
||||
let login = match (user, password) {
|
||||
(Some(user), Some(password)) => Some(encode(&format!("{}:{}", user, password))),
|
||||
(Some(user), _) => Some(encode(&format!("{}:", user))),
|
||||
_ => None,
|
||||
};
|
||||
|
||||
let client = http_client();
|
||||
let mut request = client.get(url);
|
||||
|
||||
if let Some(login) = login {
|
||||
request = request.header("Authorization", format!("Basic {}", login));
|
||||
}
|
||||
|
||||
let generate_error = |t: &str, e: reqwest::Error, span: &Span| {
|
||||
ShellError::labeled_error(
|
||||
format!("Could not load {} from remote url: {:?}", t, e),
|
||||
"could not load",
|
||||
span,
|
||||
)
|
||||
};
|
||||
let tag = Tag {
|
||||
span,
|
||||
anchor: Some(AnchorLocation::Url(location.to_string())),
|
||||
};
|
||||
|
||||
match request.send().await {
|
||||
Ok(r) => match r.headers().get("content-type") {
|
||||
Some(content_type) => {
|
||||
let content_type = content_type.to_str().map_err(|e| {
|
||||
ShellError::labeled_error(e.to_string(), "MIME type were invalid", &tag)
|
||||
})?;
|
||||
let content_type = mime::Mime::from_str(content_type).map_err(|_| {
|
||||
ShellError::labeled_error(
|
||||
format!("MIME type unknown: {}", content_type),
|
||||
"given unknown MIME type",
|
||||
span,
|
||||
)
|
||||
})?;
|
||||
match (content_type.type_(), content_type.subtype()) {
|
||||
(mime::APPLICATION, mime::XML) => Ok((
|
||||
Some("xml".to_string()),
|
||||
UntaggedValue::string(
|
||||
r.text()
|
||||
.await
|
||||
.map_err(|e| generate_error("text", e, &span))?,
|
||||
)
|
||||
.into_value(tag),
|
||||
)),
|
||||
(mime::APPLICATION, mime::JSON) => Ok((
|
||||
Some("json".to_string()),
|
||||
UntaggedValue::string(
|
||||
r.text()
|
||||
.await
|
||||
.map_err(|e| generate_error("text", e, &span))?,
|
||||
)
|
||||
.into_value(tag),
|
||||
)),
|
||||
(mime::APPLICATION, mime::OCTET_STREAM) => {
|
||||
let buf: Vec<u8> = r
|
||||
.bytes()
|
||||
.await
|
||||
.map_err(|e| generate_error("binary", e, &span))?
|
||||
.to_vec();
|
||||
Ok((None, UntaggedValue::binary(buf).into_value(tag)))
|
||||
}
|
||||
(mime::IMAGE, mime::SVG) => Ok((
|
||||
Some("svg".to_string()),
|
||||
UntaggedValue::string(
|
||||
r.text()
|
||||
.await
|
||||
.map_err(|e| generate_error("svg", e, &span))?,
|
||||
)
|
||||
.into_value(tag),
|
||||
)),
|
||||
(mime::IMAGE, image_ty) => {
|
||||
let buf: Vec<u8> = r
|
||||
.bytes()
|
||||
.await
|
||||
.map_err(|e| generate_error("image", e, &span))?
|
||||
.to_vec();
|
||||
Ok((
|
||||
Some(image_ty.to_string()),
|
||||
UntaggedValue::binary(buf).into_value(tag),
|
||||
))
|
||||
}
|
||||
(mime::TEXT, mime::HTML) => Ok((
|
||||
Some("html".to_string()),
|
||||
UntaggedValue::string(
|
||||
r.text()
|
||||
.await
|
||||
.map_err(|e| generate_error("text", e, &span))?,
|
||||
)
|
||||
.into_value(tag),
|
||||
)),
|
||||
(mime::TEXT, mime::CSV) => Ok((
|
||||
Some("csv".to_string()),
|
||||
UntaggedValue::string(
|
||||
r.text()
|
||||
.await
|
||||
.map_err(|e| generate_error("text", e, &span))?,
|
||||
)
|
||||
.into_value(tag),
|
||||
)),
|
||||
(mime::TEXT, mime::PLAIN) => {
|
||||
let path_extension = url::Url::parse(location)
|
||||
.map_err(|_| {
|
||||
ShellError::labeled_error(
|
||||
format!("Cannot parse URL: {}", location),
|
||||
"cannot parse",
|
||||
span,
|
||||
)
|
||||
})?
|
||||
.path_segments()
|
||||
.and_then(|segments| segments.last())
|
||||
.and_then(|name| if name.is_empty() { None } else { Some(name) })
|
||||
.and_then(|name| {
|
||||
PathBuf::from(name)
|
||||
.extension()
|
||||
.map(|name| name.to_string_lossy().to_string())
|
||||
});
|
||||
|
||||
Ok((
|
||||
path_extension,
|
||||
UntaggedValue::string(
|
||||
r.text()
|
||||
.await
|
||||
.map_err(|e| generate_error("text", e, &span))?,
|
||||
)
|
||||
.into_value(tag),
|
||||
))
|
||||
}
|
||||
(_ty, _sub_ty) if has_raw => {
|
||||
let raw_bytes = r.bytes().await;
|
||||
let raw_bytes = match raw_bytes {
|
||||
Ok(r) => r,
|
||||
Err(e) => {
|
||||
return Err(ShellError::labeled_error(
|
||||
"error with raw_bytes",
|
||||
e.to_string(),
|
||||
&span,
|
||||
));
|
||||
}
|
||||
};
|
||||
|
||||
// For unsupported MIME types, we do not know if the data is UTF-8,
|
||||
// so we get the raw body bytes and try to convert to UTF-8 if possible.
|
||||
match std::str::from_utf8(&raw_bytes) {
|
||||
Ok(response_str) => {
|
||||
Ok((None, UntaggedValue::string(response_str).into_value(tag)))
|
||||
}
|
||||
Err(_) => Ok((
|
||||
None,
|
||||
UntaggedValue::binary(raw_bytes.to_vec()).into_value(tag),
|
||||
)),
|
||||
}
|
||||
}
|
||||
(ty, sub_ty) => Err(ShellError::unimplemented(format!(
|
||||
"Not yet supported MIME type: {} {}",
|
||||
ty, sub_ty
|
||||
))),
|
||||
}
|
||||
}
|
||||
// TODO: Should this return "nothing" or Err?
|
||||
None => Ok((
|
||||
None,
|
||||
UntaggedValue::string("No content type found".to_owned()).into_value(tag),
|
||||
)),
|
||||
},
|
||||
Err(e) => Err(ShellError::labeled_error(
|
||||
"url could not be opened",
|
||||
e.to_string(),
|
||||
span,
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
// Only panics if the user agent is invalid but we define it statically so either
|
||||
// it always or never fails
|
||||
#[allow(clippy::unwrap_used)]
|
||||
fn http_client() -> reqwest::Client {
|
||||
reqwest::Client::builder()
|
||||
.user_agent("nushell")
|
||||
.build()
|
||||
.unwrap()
|
||||
}
|
@ -1,3 +1,12 @@
|
||||
mod url_;
|
||||
#[cfg(feature = "fetch")]
|
||||
mod fetch;
|
||||
#[cfg(feature = "fetch")]
|
||||
pub use fetch::Command as Fetch;
|
||||
|
||||
#[cfg(feature = "post")]
|
||||
mod post;
|
||||
#[cfg(feature = "post")]
|
||||
pub use post::Command as Post;
|
||||
|
||||
mod url_;
|
||||
pub use url_::*;
|
||||
|
620
crates/nu-command/src/commands/network/post.rs
Normal file
620
crates/nu-command/src/commands/network/post.rs
Normal file
@ -0,0 +1,620 @@
|
||||
use crate::prelude::*;
|
||||
use base64::encode;
|
||||
use mime::Mime;
|
||||
use nu_engine::WholeStreamCommand;
|
||||
use nu_errors::{CoerceInto, ShellError};
|
||||
use nu_protocol::{
|
||||
CommandAction, Primitive, ReturnSuccess, ReturnValue, UnspannedPathMember, UntaggedValue, Value,
|
||||
};
|
||||
use nu_protocol::{Signature, SyntaxShape};
|
||||
use nu_source::{AnchorLocation, Tag, TaggedItem};
|
||||
use num_traits::cast::ToPrimitive;
|
||||
use std::path::PathBuf;
|
||||
use std::str::FromStr;
|
||||
|
||||
pub struct Command;
|
||||
|
||||
impl WholeStreamCommand for Command {
|
||||
fn name(&self) -> &str {
|
||||
"post"
|
||||
}
|
||||
|
||||
fn signature(&self) -> Signature {
|
||||
Signature::build("post")
|
||||
.desc("Post content to a URL and retrieve data as a table if possible.")
|
||||
.required("path", SyntaxShape::Any, "the URL to post to")
|
||||
.required("body", SyntaxShape::Any, "the contents of the post body")
|
||||
.named(
|
||||
"user",
|
||||
SyntaxShape::Any,
|
||||
"the username when authenticating",
|
||||
Some('u'),
|
||||
)
|
||||
.named(
|
||||
"password",
|
||||
SyntaxShape::Any,
|
||||
"the password when authenticating",
|
||||
Some('p'),
|
||||
)
|
||||
.named(
|
||||
"content-type",
|
||||
SyntaxShape::Any,
|
||||
"the MIME type of content to post",
|
||||
Some('t'),
|
||||
)
|
||||
.named(
|
||||
"content-length",
|
||||
SyntaxShape::Any,
|
||||
"the length of the content being posted",
|
||||
Some('l'),
|
||||
)
|
||||
.switch(
|
||||
"raw",
|
||||
"return values as a string instead of a table",
|
||||
Some('r'),
|
||||
)
|
||||
.filter()
|
||||
}
|
||||
|
||||
fn usage(&self) -> &str {
|
||||
"Post a body to a URL (HTTP POST operation)."
|
||||
}
|
||||
|
||||
fn run_with_actions(&self, args: CommandArgs) -> Result<ActionStream, ShellError> {
|
||||
run_post(args)
|
||||
}
|
||||
|
||||
fn examples(&self) -> Vec<Example> {
|
||||
vec![
|
||||
Example {
|
||||
description: "Post content to url.com",
|
||||
example: "post url.com 'body'",
|
||||
result: None,
|
||||
},
|
||||
Example {
|
||||
description: "Post content to url.com, with username and password",
|
||||
example: "post -u myuser -p mypass url.com 'body'",
|
||||
result: None,
|
||||
},
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
fn run_post(args: CommandArgs) -> Result<ActionStream, ShellError> {
|
||||
let mut helper = Post::new();
|
||||
|
||||
helper.setup(args)?;
|
||||
|
||||
let runtime = tokio::runtime::Runtime::new()?;
|
||||
Ok(vec![runtime.block_on(post_helper(
|
||||
&helper.path.clone().ok_or_else(|| {
|
||||
ShellError::labeled_error("expected a 'path'", "expected a 'path'", &helper.tag)
|
||||
})?,
|
||||
helper.has_raw,
|
||||
&helper.body.clone().ok_or_else(|| {
|
||||
ShellError::labeled_error("expected a 'body'", "expected a 'body'", &helper.tag)
|
||||
})?,
|
||||
helper.user.clone(),
|
||||
helper.password.clone(),
|
||||
&helper.headers,
|
||||
))]
|
||||
.into_iter()
|
||||
.into_action_stream())
|
||||
|
||||
//fetch.setup(callinfo)?;
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub enum HeaderKind {
|
||||
ContentType(String),
|
||||
ContentLength(String),
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct Post {
|
||||
pub path: Option<Value>,
|
||||
pub has_raw: bool,
|
||||
pub body: Option<Value>,
|
||||
pub user: Option<String>,
|
||||
pub password: Option<String>,
|
||||
pub headers: Vec<HeaderKind>,
|
||||
pub tag: Tag,
|
||||
}
|
||||
|
||||
impl Post {
|
||||
pub fn new() -> Post {
|
||||
Post {
|
||||
path: None,
|
||||
has_raw: false,
|
||||
body: None,
|
||||
user: None,
|
||||
password: None,
|
||||
headers: vec![],
|
||||
tag: Tag::default(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn setup(&mut self, args: CommandArgs) -> Result<(), ShellError> {
|
||||
self.path = Some({
|
||||
args.req(0).map_err(|_| {
|
||||
ShellError::labeled_error(
|
||||
"No file or directory specified",
|
||||
"for command",
|
||||
&args.name_tag(),
|
||||
)
|
||||
})?
|
||||
});
|
||||
|
||||
self.body = {
|
||||
let file = args.req(1).map_err(|_| {
|
||||
ShellError::labeled_error("No body specified", "for command", &args.name_tag())
|
||||
})?;
|
||||
Some(file)
|
||||
};
|
||||
|
||||
self.tag = args.name_tag();
|
||||
|
||||
self.has_raw = args.has_flag("raw");
|
||||
|
||||
self.user = args.get_flag("user")?;
|
||||
|
||||
self.password = args.get_flag("password")?;
|
||||
|
||||
self.headers = get_headers(&args)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn post_helper(
|
||||
path: &Value,
|
||||
has_raw: bool,
|
||||
body: &Value,
|
||||
user: Option<String>,
|
||||
password: Option<String>,
|
||||
headers: &[HeaderKind],
|
||||
) -> ReturnValue {
|
||||
let path_tag = path.tag.clone();
|
||||
let path_str = path.as_string()?;
|
||||
|
||||
let (file_extension, contents, contents_tag) =
|
||||
post(&path_str, body, user, password, headers, path_tag.clone()).await?;
|
||||
|
||||
let file_extension = if has_raw {
|
||||
None
|
||||
} else {
|
||||
// If the extension could not be determined via mimetype, try to use the path
|
||||
// extension. Some file types do not declare their mimetypes (such as bson files).
|
||||
file_extension.or_else(|| path_str.split('.').last().map(String::from))
|
||||
};
|
||||
|
||||
let tagged_contents = contents.into_value(&contents_tag);
|
||||
|
||||
if let Some(extension) = file_extension {
|
||||
Ok(ReturnSuccess::Action(CommandAction::AutoConvert(
|
||||
tagged_contents,
|
||||
extension,
|
||||
)))
|
||||
} else {
|
||||
ReturnSuccess::value(tagged_contents)
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn post(
|
||||
location: &str,
|
||||
body: &Value,
|
||||
user: Option<String>,
|
||||
password: Option<String>,
|
||||
headers: &[HeaderKind],
|
||||
tag: Tag,
|
||||
) -> Result<(Option<String>, UntaggedValue, Tag), ShellError> {
|
||||
if location.starts_with("http:") || location.starts_with("https:") {
|
||||
let login = match (user, password) {
|
||||
(Some(user), Some(password)) => Some(encode(&format!("{}:{}", user, password))),
|
||||
(Some(user), _) => Some(encode(&format!("{}:", user))),
|
||||
_ => None,
|
||||
};
|
||||
let response = match body {
|
||||
Value {
|
||||
value: UntaggedValue::Primitive(Primitive::String(body_str)),
|
||||
..
|
||||
} => {
|
||||
let mut s = http_client().post(location).body(body_str.to_string());
|
||||
if let Some(login) = login {
|
||||
s = s.header("Authorization", format!("Basic {}", login));
|
||||
}
|
||||
|
||||
for h in headers {
|
||||
s = match h {
|
||||
HeaderKind::ContentType(ct) => s.header("Content-Type", ct),
|
||||
HeaderKind::ContentLength(cl) => s.header("Content-Length", cl),
|
||||
};
|
||||
}
|
||||
|
||||
s.send().await
|
||||
}
|
||||
Value {
|
||||
value: UntaggedValue::Primitive(Primitive::Binary(b)),
|
||||
..
|
||||
} => {
|
||||
let mut s = http_client().post(location).body(Vec::from(&b[..]));
|
||||
if let Some(login) = login {
|
||||
s = s.header("Authorization", format!("Basic {}", login));
|
||||
}
|
||||
s.send().await
|
||||
}
|
||||
Value { value, tag } => {
|
||||
match value_to_json_value(&value.clone().into_untagged_value()) {
|
||||
Ok(json_value) => match serde_json::to_string(&json_value) {
|
||||
Ok(result_string) => {
|
||||
let mut s = http_client().post(location).body(result_string);
|
||||
|
||||
if let Some(login) = login {
|
||||
s = s.header("Authorization", format!("Basic {}", login));
|
||||
}
|
||||
s.send().await
|
||||
}
|
||||
_ => {
|
||||
return Err(ShellError::labeled_error(
|
||||
"Could not automatically convert table",
|
||||
"needs manual conversion",
|
||||
tag,
|
||||
));
|
||||
}
|
||||
},
|
||||
_ => {
|
||||
return Err(ShellError::labeled_error(
|
||||
"Could not automatically convert table",
|
||||
"needs manual conversion",
|
||||
tag,
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
match response {
|
||||
Ok(r) => match r.headers().get("content-type") {
|
||||
Some(content_type) => {
|
||||
let content_type = content_type.to_str().map_err(|e| {
|
||||
ShellError::labeled_error(e.to_string(), "MIME type were invalid", &tag)
|
||||
})?;
|
||||
let content_type = Mime::from_str(content_type).map_err(|_| {
|
||||
ShellError::labeled_error(
|
||||
format!("Unknown MIME type: {}", content_type),
|
||||
"unknown MIME type",
|
||||
&tag,
|
||||
)
|
||||
})?;
|
||||
match (content_type.type_(), content_type.subtype()) {
|
||||
(mime::APPLICATION, mime::XML) => Ok((
|
||||
Some("xml".to_string()),
|
||||
UntaggedValue::string(r.text().await.map_err(|_| {
|
||||
ShellError::labeled_error(
|
||||
"Could not load text from remote url",
|
||||
"could not load",
|
||||
&tag,
|
||||
)
|
||||
})?),
|
||||
Tag {
|
||||
anchor: Some(AnchorLocation::Url(location.to_string())),
|
||||
span: tag.span,
|
||||
},
|
||||
)),
|
||||
(mime::APPLICATION, mime::JSON) => Ok((
|
||||
Some("json".to_string()),
|
||||
UntaggedValue::string(r.text().await.map_err(|_| {
|
||||
ShellError::labeled_error(
|
||||
"Could not load text from remote url",
|
||||
"could not load",
|
||||
&tag,
|
||||
)
|
||||
})?),
|
||||
Tag {
|
||||
anchor: Some(AnchorLocation::Url(location.to_string())),
|
||||
span: tag.span,
|
||||
},
|
||||
)),
|
||||
(mime::APPLICATION, mime::OCTET_STREAM) => {
|
||||
let buf: Vec<u8> = r
|
||||
.bytes()
|
||||
.await
|
||||
.map_err(|_| {
|
||||
ShellError::labeled_error(
|
||||
"Could not load binary file",
|
||||
"could not load",
|
||||
&tag,
|
||||
)
|
||||
})?
|
||||
.to_vec();
|
||||
Ok((
|
||||
None,
|
||||
UntaggedValue::binary(buf),
|
||||
Tag {
|
||||
anchor: Some(AnchorLocation::Url(location.to_string())),
|
||||
span: tag.span,
|
||||
},
|
||||
))
|
||||
}
|
||||
(mime::IMAGE, image_ty) => {
|
||||
let buf: Vec<u8> = r
|
||||
.bytes()
|
||||
.await
|
||||
.map_err(|_| {
|
||||
ShellError::labeled_error(
|
||||
"Could not load image file",
|
||||
"could not load",
|
||||
&tag,
|
||||
)
|
||||
})?
|
||||
.to_vec();
|
||||
Ok((
|
||||
Some(image_ty.to_string()),
|
||||
UntaggedValue::binary(buf),
|
||||
Tag {
|
||||
anchor: Some(AnchorLocation::Url(location.to_string())),
|
||||
span: tag.span,
|
||||
},
|
||||
))
|
||||
}
|
||||
(mime::TEXT, mime::HTML) => Ok((
|
||||
Some("html".to_string()),
|
||||
UntaggedValue::string(r.text().await.map_err(|_| {
|
||||
ShellError::labeled_error(
|
||||
"Could not load text from remote url",
|
||||
"could not load",
|
||||
&tag,
|
||||
)
|
||||
})?),
|
||||
Tag {
|
||||
anchor: Some(AnchorLocation::Url(location.to_string())),
|
||||
span: tag.span,
|
||||
},
|
||||
)),
|
||||
(mime::TEXT, mime::PLAIN) => {
|
||||
let path_extension = url::Url::parse(location)
|
||||
.map_err(|_| {
|
||||
ShellError::labeled_error(
|
||||
format!("could not parse URL: {}", location),
|
||||
"could not parse URL",
|
||||
&tag,
|
||||
)
|
||||
})?
|
||||
.path_segments()
|
||||
.and_then(|segments| segments.last())
|
||||
.and_then(|name| if name.is_empty() { None } else { Some(name) })
|
||||
.and_then(|name| {
|
||||
PathBuf::from(name)
|
||||
.extension()
|
||||
.map(|name| name.to_string_lossy().to_string())
|
||||
});
|
||||
|
||||
Ok((
|
||||
path_extension,
|
||||
UntaggedValue::string(r.text().await.map_err(|_| {
|
||||
ShellError::labeled_error(
|
||||
"Could not load text from remote url",
|
||||
"could not load",
|
||||
&tag,
|
||||
)
|
||||
})?),
|
||||
Tag {
|
||||
anchor: Some(AnchorLocation::Url(location.to_string())),
|
||||
span: tag.span,
|
||||
},
|
||||
))
|
||||
}
|
||||
(ty, sub_ty) => Ok((
|
||||
None,
|
||||
UntaggedValue::string(format!(
|
||||
"Not yet supported MIME type: {} {}",
|
||||
ty, sub_ty
|
||||
)),
|
||||
Tag {
|
||||
anchor: Some(AnchorLocation::Url(location.to_string())),
|
||||
span: tag.span,
|
||||
},
|
||||
)),
|
||||
}
|
||||
}
|
||||
None => Ok((
|
||||
None,
|
||||
UntaggedValue::string("No content type found".to_owned()),
|
||||
Tag {
|
||||
anchor: Some(AnchorLocation::Url(location.to_string())),
|
||||
span: tag.span,
|
||||
},
|
||||
)),
|
||||
},
|
||||
Err(_) => Err(ShellError::labeled_error(
|
||||
"URL could not be opened",
|
||||
"url not found",
|
||||
tag,
|
||||
)),
|
||||
}
|
||||
} else {
|
||||
Err(ShellError::labeled_error(
|
||||
"Expected a url",
|
||||
"needs a url",
|
||||
tag,
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
// FIXME FIXME FIXME
|
||||
// Ultimately, we don't want to duplicate to-json here, but we need to because there isn't an easy way to call into it, yet
|
||||
pub fn value_to_json_value(v: &Value) -> Result<serde_json::Value, ShellError> {
|
||||
Ok(match &v.value {
|
||||
UntaggedValue::Primitive(Primitive::Boolean(b)) => serde_json::Value::Bool(*b),
|
||||
UntaggedValue::Primitive(Primitive::Filesize(b)) => serde_json::Value::Number(
|
||||
serde_json::Number::from(b.to_u64().expect("What about really big numbers")),
|
||||
),
|
||||
UntaggedValue::Primitive(Primitive::Duration(i)) => serde_json::Value::Number(
|
||||
serde_json::Number::from_f64(
|
||||
i.to_f64().expect("TODO: What about really big decimals?"),
|
||||
)
|
||||
.ok_or_else(|| {
|
||||
ShellError::labeled_error(
|
||||
"Can not convert big decimal to f64",
|
||||
"cannot convert big decimal to f64",
|
||||
&v.tag,
|
||||
)
|
||||
})?,
|
||||
),
|
||||
UntaggedValue::Primitive(Primitive::Date(d)) => serde_json::Value::String(d.to_string()),
|
||||
UntaggedValue::Primitive(Primitive::EndOfStream) => serde_json::Value::Null,
|
||||
UntaggedValue::Primitive(Primitive::BeginningOfStream) => serde_json::Value::Null,
|
||||
UntaggedValue::Primitive(Primitive::Decimal(f)) => serde_json::Value::Number(
|
||||
serde_json::Number::from_f64(
|
||||
f.to_f64().expect("TODO: What about really big decimals?"),
|
||||
)
|
||||
.ok_or_else(|| {
|
||||
ShellError::labeled_error(
|
||||
"Can not convert big decimal to f64",
|
||||
"cannot convert big decimal to f64",
|
||||
&v.tag,
|
||||
)
|
||||
})?,
|
||||
),
|
||||
UntaggedValue::Primitive(Primitive::Int(i)) => {
|
||||
serde_json::Value::Number(serde_json::Number::from(*i))
|
||||
}
|
||||
UntaggedValue::Primitive(Primitive::BigInt(i)) => {
|
||||
serde_json::Value::Number(serde_json::Number::from(CoerceInto::<i64>::coerce_into(
|
||||
i.tagged(&v.tag),
|
||||
"converting to JSON number",
|
||||
)?))
|
||||
}
|
||||
UntaggedValue::Primitive(Primitive::Nothing) => serde_json::Value::Null,
|
||||
UntaggedValue::Primitive(Primitive::GlobPattern(s)) => serde_json::Value::String(s.clone()),
|
||||
UntaggedValue::Primitive(Primitive::String(s)) => serde_json::Value::String(s.clone()),
|
||||
UntaggedValue::Primitive(Primitive::ColumnPath(path)) => serde_json::Value::Array(
|
||||
path.iter()
|
||||
.map(|x| match &x.unspanned {
|
||||
UnspannedPathMember::String(string) => {
|
||||
Ok(serde_json::Value::String(string.clone()))
|
||||
}
|
||||
UnspannedPathMember::Int(int) => {
|
||||
Ok(serde_json::Value::Number(serde_json::Number::from(*int)))
|
||||
}
|
||||
})
|
||||
.collect::<Result<Vec<serde_json::Value>, ShellError>>()?,
|
||||
),
|
||||
UntaggedValue::Primitive(Primitive::FilePath(s)) => {
|
||||
serde_json::Value::String(s.display().to_string())
|
||||
}
|
||||
|
||||
UntaggedValue::Table(l) => serde_json::Value::Array(json_list(l)?),
|
||||
#[cfg(feature = "dataframe")]
|
||||
UntaggedValue::DataFrame(_) | UntaggedValue::FrameStruct(_) => {
|
||||
return Err(ShellError::labeled_error(
|
||||
"Cannot convert data struct",
|
||||
"Cannot convert data struct",
|
||||
&v.tag,
|
||||
))
|
||||
}
|
||||
UntaggedValue::Error(e) => return Err(e.clone()),
|
||||
UntaggedValue::Block(_) | UntaggedValue::Primitive(Primitive::Range(_)) => {
|
||||
serde_json::Value::Null
|
||||
}
|
||||
UntaggedValue::Primitive(Primitive::Binary(b)) => {
|
||||
let mut output = vec![];
|
||||
|
||||
for item in b.iter() {
|
||||
output.push(serde_json::Value::Number(
|
||||
serde_json::Number::from_f64(*item as f64).ok_or_else(|| {
|
||||
ShellError::labeled_error(
|
||||
"Cannot create number from from f64",
|
||||
"cannot created number from f64",
|
||||
&v.tag,
|
||||
)
|
||||
})?,
|
||||
));
|
||||
}
|
||||
serde_json::Value::Array(output)
|
||||
}
|
||||
UntaggedValue::Row(o) => {
|
||||
let mut m = serde_json::Map::new();
|
||||
for (k, v) in o.entries.iter() {
|
||||
m.insert(k.clone(), value_to_json_value(v)?);
|
||||
}
|
||||
serde_json::Value::Object(m)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
fn json_list(input: &[Value]) -> Result<Vec<serde_json::Value>, ShellError> {
|
||||
let mut out = vec![];
|
||||
|
||||
for value in input {
|
||||
out.push(value_to_json_value(value)?);
|
||||
}
|
||||
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
fn get_headers(args: &CommandArgs) -> Result<Vec<HeaderKind>, ShellError> {
|
||||
let mut headers = vec![];
|
||||
|
||||
match extract_header_value(args, "content-type") {
|
||||
Ok(h) => {
|
||||
if let Some(ct) = h {
|
||||
headers.push(HeaderKind::ContentType(ct))
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
return Err(e);
|
||||
}
|
||||
};
|
||||
|
||||
match extract_header_value(args, "content-length") {
|
||||
Ok(h) => {
|
||||
if let Some(cl) = h {
|
||||
headers.push(HeaderKind::ContentLength(cl))
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
return Err(e);
|
||||
}
|
||||
};
|
||||
|
||||
Ok(headers)
|
||||
}
|
||||
|
||||
fn extract_header_value(args: &CommandArgs, key: &str) -> Result<Option<String>, ShellError> {
|
||||
if args.has_flag(key) {
|
||||
let tagged = args.get_flag(key)?;
|
||||
let val = match tagged {
|
||||
Some(Value {
|
||||
value: UntaggedValue::Primitive(Primitive::String(s)),
|
||||
..
|
||||
}) => s,
|
||||
Some(Value { tag, .. }) => {
|
||||
return Err(ShellError::labeled_error(
|
||||
format!("{} not in expected format. Expected string.", key),
|
||||
"post error",
|
||||
tag,
|
||||
));
|
||||
}
|
||||
_ => {
|
||||
return Err(ShellError::labeled_error(
|
||||
format!("{} not in expected format. Expected string.", key),
|
||||
"post error",
|
||||
Tag::unknown(),
|
||||
));
|
||||
}
|
||||
};
|
||||
return Ok(Some(val));
|
||||
}
|
||||
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
// Only panics if the user agent is invalid but we define it statically so either
|
||||
// it always or never fails
|
||||
#[allow(clippy::unwrap_used)]
|
||||
fn http_client() -> reqwest::Client {
|
||||
reqwest::Client::builder()
|
||||
.user_agent("nushell")
|
||||
.build()
|
||||
.unwrap()
|
||||
}
|
9
crates/nu-command/src/commands/system/mod.rs
Normal file
9
crates/nu-command/src/commands/system/mod.rs
Normal file
@ -0,0 +1,9 @@
|
||||
#[cfg(feature = "ps")]
|
||||
mod ps;
|
||||
#[cfg(feature = "ps")]
|
||||
pub use ps::Command as Ps;
|
||||
|
||||
#[cfg(feature = "sys")]
|
||||
mod sys;
|
||||
#[cfg(feature = "sys")]
|
||||
pub use sys::Command as Sys;
|
84
crates/nu-command/src/commands/system/ps.rs
Normal file
84
crates/nu-command/src/commands/system/ps.rs
Normal file
@ -0,0 +1,84 @@
|
||||
use crate::prelude::*;
|
||||
use nu_errors::ShellError;
|
||||
use nu_protocol::{Signature, TaggedDictBuilder, UntaggedValue};
|
||||
use sysinfo::{ProcessExt, System, SystemExt};
|
||||
|
||||
pub struct Command;
|
||||
|
||||
impl WholeStreamCommand for Command {
|
||||
fn name(&self) -> &str {
|
||||
"ps"
|
||||
}
|
||||
|
||||
fn signature(&self) -> Signature {
|
||||
Signature::build("ps")
|
||||
.desc("View information about system processes.")
|
||||
.switch(
|
||||
"long",
|
||||
"list all available columns for each entry",
|
||||
Some('l'),
|
||||
)
|
||||
.filter()
|
||||
}
|
||||
|
||||
fn usage(&self) -> &str {
|
||||
"View information about system processes."
|
||||
}
|
||||
|
||||
fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
|
||||
run_ps(args)
|
||||
}
|
||||
|
||||
fn examples(&self) -> Vec<Example> {
|
||||
vec![Example {
|
||||
description: "List the system processes",
|
||||
example: "ps",
|
||||
result: None,
|
||||
}]
|
||||
}
|
||||
}
|
||||
|
||||
fn run_ps(args: CommandArgs) -> Result<OutputStream, ShellError> {
|
||||
let long = args.has_flag("long");
|
||||
let mut sys = System::new_all();
|
||||
sys.refresh_all();
|
||||
|
||||
let mut output = vec![];
|
||||
|
||||
let result: Vec<_> = sys.processes().iter().map(|x| *x.0).collect();
|
||||
|
||||
for pid in result.into_iter() {
|
||||
if let Some(result) = sys.process(pid) {
|
||||
let mut dict = TaggedDictBuilder::new(args.name_tag());
|
||||
dict.insert_untagged("pid", UntaggedValue::int(pid as i64));
|
||||
dict.insert_untagged("name", UntaggedValue::string(result.name()));
|
||||
dict.insert_untagged(
|
||||
"status",
|
||||
UntaggedValue::string(format!("{:?}", result.status())),
|
||||
);
|
||||
dict.insert_untagged(
|
||||
"cpu",
|
||||
UntaggedValue::decimal_from_float(result.cpu_usage() as f64, args.name_tag().span),
|
||||
);
|
||||
dict.insert_untagged("mem", UntaggedValue::filesize(result.memory() * 1000));
|
||||
dict.insert_untagged(
|
||||
"virtual",
|
||||
UntaggedValue::filesize(result.virtual_memory() * 1000),
|
||||
);
|
||||
|
||||
if long {
|
||||
if let Some(parent) = result.parent() {
|
||||
dict.insert_untagged("parent", UntaggedValue::int(parent as i64));
|
||||
} else {
|
||||
dict.insert_untagged("parent", UntaggedValue::nothing());
|
||||
}
|
||||
dict.insert_untagged("exe", UntaggedValue::filepath(result.exe()));
|
||||
dict.insert_untagged("command", UntaggedValue::string(result.cmd().join(" ")));
|
||||
}
|
||||
|
||||
output.push(dict.into_value());
|
||||
}
|
||||
}
|
||||
|
||||
Ok(output.into_iter().into_output_stream())
|
||||
}
|
248
crates/nu-command/src/commands/system/sys.rs
Normal file
248
crates/nu-command/src/commands/system/sys.rs
Normal file
@ -0,0 +1,248 @@
|
||||
use crate::prelude::*;
|
||||
use nu_errors::ShellError;
|
||||
use nu_protocol::{Signature, TaggedDictBuilder, UntaggedValue};
|
||||
use sysinfo::{ComponentExt, DiskExt, NetworkExt, ProcessorExt, System, SystemExt, UserExt};
|
||||
|
||||
pub struct Command;
|
||||
|
||||
impl WholeStreamCommand for Command {
|
||||
fn name(&self) -> &str {
|
||||
"sys"
|
||||
}
|
||||
|
||||
fn signature(&self) -> Signature {
|
||||
Signature::build("sys")
|
||||
.desc("View information about the current system.")
|
||||
.filter()
|
||||
}
|
||||
|
||||
fn usage(&self) -> &str {
|
||||
"View information about the system."
|
||||
}
|
||||
|
||||
fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
|
||||
run_sys(args)
|
||||
}
|
||||
|
||||
fn examples(&self) -> Vec<Example> {
|
||||
vec![Example {
|
||||
description: "Show info about the system",
|
||||
example: "sys",
|
||||
result: None,
|
||||
}]
|
||||
}
|
||||
}
|
||||
|
||||
fn run_sys(args: CommandArgs) -> Result<OutputStream, ShellError> {
|
||||
let tag = args.name_tag();
|
||||
let mut sys = System::new();
|
||||
|
||||
let mut sysinfo = TaggedDictBuilder::with_capacity(&tag, 6);
|
||||
|
||||
if let Some(host) = host(&mut sys, tag.clone()) {
|
||||
sysinfo.insert_value("host", host);
|
||||
}
|
||||
if let Some(cpus) = cpu(&mut sys, tag.clone()) {
|
||||
sysinfo.insert_value("cpu", cpus);
|
||||
}
|
||||
if let Some(disks) = disks(&mut sys, tag.clone()) {
|
||||
sysinfo.insert_value("disks", disks);
|
||||
}
|
||||
if let Some(mem) = mem(&mut sys, tag.clone()) {
|
||||
sysinfo.insert_value("mem", mem);
|
||||
}
|
||||
if let Some(temp) = temp(&mut sys, tag.clone()) {
|
||||
sysinfo.insert_value("temp", temp);
|
||||
}
|
||||
if let Some(net) = net(&mut sys, tag) {
|
||||
sysinfo.insert_value("net", net);
|
||||
}
|
||||
|
||||
Ok(vec![sysinfo.into_value()].into_iter().into_output_stream())
|
||||
}
|
||||
|
||||
pub fn trim_cstyle_null(s: String) -> String {
|
||||
s.trim_matches(char::from(0)).to_string()
|
||||
}
|
||||
|
||||
pub fn disks(sys: &mut System, tag: Tag) -> Option<UntaggedValue> {
|
||||
sys.refresh_disks();
|
||||
sys.refresh_disks_list();
|
||||
|
||||
let mut output = vec![];
|
||||
for disk in sys.disks() {
|
||||
let mut dict = TaggedDictBuilder::new(&tag);
|
||||
dict.insert_untagged(
|
||||
"device",
|
||||
UntaggedValue::string(trim_cstyle_null(disk.name().to_string_lossy().to_string())),
|
||||
);
|
||||
dict.insert_untagged(
|
||||
"type",
|
||||
UntaggedValue::string(trim_cstyle_null(
|
||||
String::from_utf8_lossy(disk.file_system()).to_string(),
|
||||
)),
|
||||
);
|
||||
dict.insert_untagged("mount", UntaggedValue::filepath(disk.mount_point()));
|
||||
dict.insert_untagged("total", UntaggedValue::filesize(disk.total_space()));
|
||||
dict.insert_untagged("free", UntaggedValue::filesize(disk.available_space()));
|
||||
output.push(dict.into_value());
|
||||
}
|
||||
if !output.is_empty() {
|
||||
Some(UntaggedValue::Table(output))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
pub fn net(sys: &mut System, tag: Tag) -> Option<UntaggedValue> {
|
||||
sys.refresh_networks();
|
||||
sys.refresh_networks_list();
|
||||
|
||||
let mut output = vec![];
|
||||
for (iface, data) in sys.networks() {
|
||||
let mut dict = TaggedDictBuilder::new(&tag);
|
||||
dict.insert_untagged(
|
||||
"name",
|
||||
UntaggedValue::string(trim_cstyle_null(iface.to_string())),
|
||||
);
|
||||
dict.insert_untagged("sent", UntaggedValue::filesize(data.total_transmitted()));
|
||||
dict.insert_untagged("recv", UntaggedValue::filesize(data.total_received()));
|
||||
|
||||
output.push(dict.into_value());
|
||||
}
|
||||
if !output.is_empty() {
|
||||
Some(UntaggedValue::Table(output))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
pub fn cpu(sys: &mut System, tag: Tag) -> Option<UntaggedValue> {
|
||||
sys.refresh_cpu();
|
||||
|
||||
let mut output = vec![];
|
||||
for cpu in sys.processors() {
|
||||
let mut dict = TaggedDictBuilder::new(&tag);
|
||||
dict.insert_untagged(
|
||||
"name",
|
||||
UntaggedValue::string(trim_cstyle_null(cpu.name().to_string())),
|
||||
);
|
||||
dict.insert_untagged(
|
||||
"brand",
|
||||
UntaggedValue::string(trim_cstyle_null(cpu.brand().to_string())),
|
||||
);
|
||||
dict.insert_untagged("freq", UntaggedValue::int(cpu.frequency() as i64));
|
||||
|
||||
output.push(dict.into_value());
|
||||
}
|
||||
if !output.is_empty() {
|
||||
Some(UntaggedValue::Table(output))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
pub fn mem(sys: &mut System, tag: Tag) -> Option<UntaggedValue> {
|
||||
sys.refresh_memory();
|
||||
|
||||
let mut dict = TaggedDictBuilder::new(tag);
|
||||
let total_mem = sys.total_memory();
|
||||
let free_mem = sys.free_memory();
|
||||
let total_swap = sys.total_swap();
|
||||
let free_swap = sys.free_swap();
|
||||
|
||||
dict.insert_untagged("total", UntaggedValue::filesize(total_mem * 1000));
|
||||
dict.insert_untagged("free", UntaggedValue::filesize(free_mem * 1000));
|
||||
dict.insert_untagged("swap total", UntaggedValue::filesize(total_swap * 1000));
|
||||
dict.insert_untagged("swap free", UntaggedValue::filesize(free_swap * 1000));
|
||||
|
||||
Some(dict.into_untagged_value())
|
||||
}
|
||||
|
||||
pub fn host(sys: &mut System, tag: Tag) -> Option<UntaggedValue> {
|
||||
sys.refresh_users_list();
|
||||
|
||||
let mut dict = TaggedDictBuilder::new(&tag);
|
||||
if let Some(name) = sys.name() {
|
||||
dict.insert_untagged("name", UntaggedValue::string(trim_cstyle_null(name)));
|
||||
}
|
||||
if let Some(version) = sys.os_version() {
|
||||
dict.insert_untagged(
|
||||
"os version",
|
||||
UntaggedValue::string(trim_cstyle_null(version)),
|
||||
);
|
||||
}
|
||||
if let Some(version) = sys.kernel_version() {
|
||||
dict.insert_untagged(
|
||||
"kernel version",
|
||||
UntaggedValue::string(trim_cstyle_null(version)),
|
||||
);
|
||||
}
|
||||
if let Some(hostname) = sys.host_name() {
|
||||
dict.insert_untagged(
|
||||
"hostname",
|
||||
UntaggedValue::string(trim_cstyle_null(hostname)),
|
||||
);
|
||||
}
|
||||
dict.insert_untagged(
|
||||
"uptime",
|
||||
UntaggedValue::duration(1000000000 * sys.uptime() as i64),
|
||||
);
|
||||
|
||||
let mut users = vec![];
|
||||
for user in sys.users() {
|
||||
let mut user_dict = TaggedDictBuilder::new(&tag);
|
||||
user_dict.insert_untagged(
|
||||
"name",
|
||||
UntaggedValue::string(trim_cstyle_null(user.name().to_string())),
|
||||
);
|
||||
|
||||
let mut groups = vec![];
|
||||
for group in user.groups() {
|
||||
groups
|
||||
.push(UntaggedValue::string(trim_cstyle_null(group.to_string())).into_value(&tag));
|
||||
}
|
||||
user_dict.insert_untagged("groups", UntaggedValue::Table(groups));
|
||||
|
||||
users.push(user_dict.into_value());
|
||||
}
|
||||
if !users.is_empty() {
|
||||
dict.insert_untagged("sessions", UntaggedValue::Table(users));
|
||||
}
|
||||
|
||||
Some(dict.into_untagged_value())
|
||||
}
|
||||
|
||||
pub fn temp(sys: &mut System, tag: Tag) -> Option<UntaggedValue> {
|
||||
sys.refresh_components();
|
||||
sys.refresh_components_list();
|
||||
|
||||
let mut output = vec![];
|
||||
|
||||
for component in sys.components() {
|
||||
let mut dict = TaggedDictBuilder::new(&tag);
|
||||
|
||||
dict.insert_untagged("unit", UntaggedValue::string(component.label()));
|
||||
dict.insert_untagged(
|
||||
"temp",
|
||||
UntaggedValue::decimal_from_float(component.temperature() as f64, tag.span),
|
||||
);
|
||||
dict.insert_untagged(
|
||||
"high",
|
||||
UntaggedValue::decimal_from_float(component.max() as f64, tag.span),
|
||||
);
|
||||
|
||||
if let Some(critical) = component.critical() {
|
||||
dict.insert_untagged(
|
||||
"critical",
|
||||
UntaggedValue::decimal_from_float(critical as f64, tag.span),
|
||||
);
|
||||
}
|
||||
output.push(dict.into_value());
|
||||
}
|
||||
if !output.is_empty() {
|
||||
Some(UntaggedValue::Table(output))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user