From b9331d1b0816b3fddfb187028a4f147c835a423e Mon Sep 17 00:00:00 2001 From: Ian Manske Date: Tue, 7 May 2024 12:52:02 +0000 Subject: [PATCH] Add `sys users` command (#12787) # Description Add a new `sys users` command which returns a table of the users of the system. This is the same table that is currently present as `(sys).host.sessions`. The same table has been removed from the recently added `sys host` command. # User-Facing Changes Adds a new command. (The old `sys` command is left as is.) --- crates/nu-command/src/default_context.rs | 1 + crates/nu-command/src/system/sys/host.rs | 3 +- crates/nu-command/src/system/sys/mod.rs | 48 ++++++++++++----------- crates/nu-command/src/system/sys/sys_.rs | 5 ++- crates/nu-command/src/system/sys/users.rs | 38 ++++++++++++++++++ 5 files changed, 71 insertions(+), 24 deletions(-) create mode 100644 crates/nu-command/src/system/sys/users.rs diff --git a/crates/nu-command/src/default_context.rs b/crates/nu-command/src/default_context.rs index d8307be84d..ba55472e15 100644 --- a/crates/nu-command/src/default_context.rs +++ b/crates/nu-command/src/default_context.rs @@ -125,6 +125,7 @@ pub fn add_shell_command_context(mut engine_state: EngineState) -> EngineState { SysMem, SysNet, SysTemp, + SysUsers, UName, }; diff --git a/crates/nu-command/src/system/sys/host.rs b/crates/nu-command/src/system/sys/host.rs index 23508b5b6e..969f59ef99 100644 --- a/crates/nu-command/src/system/sys/host.rs +++ b/crates/nu-command/src/system/sys/host.rs @@ -26,7 +26,8 @@ impl Command for SysHost { call: &Call, _input: PipelineData, ) -> Result { - Ok(super::host(call.head).into_pipeline_data()) + let host = super::host(call.head); + Ok(Value::record(host, call.head).into_pipeline_data()) } fn examples(&self) -> Vec { diff --git a/crates/nu-command/src/system/sys/mod.rs b/crates/nu-command/src/system/sys/mod.rs index 0c61543e7d..5e0467c251 100644 --- a/crates/nu-command/src/system/sys/mod.rs +++ b/crates/nu-command/src/system/sys/mod.rs @@ -5,6 +5,7 @@ mod mem; mod net; mod sys_; mod temp; +mod users; pub use cpu::SysCpu; pub use disks::SysDisks; @@ -13,6 +14,7 @@ pub use mem::SysMem; pub use net::SysNet; pub use sys_::Sys; pub use temp::SysTemp; +pub use users::SysUsers; use chrono::{DateTime, Local}; use nu_protocol::{record, Record, Span, Value}; @@ -122,7 +124,29 @@ pub fn mem(span: Span) -> Value { Value::record(record, span) } -pub fn host(span: Span) -> Value { +pub fn users(span: Span) -> Value { + let users = Users::new_with_refreshed_list() + .iter() + .map(|user| { + let groups = user + .groups() + .iter() + .map(|group| Value::string(trim_cstyle_null(group.name()), span)) + .collect(); + + let record = record! { + "name" => Value::string(trim_cstyle_null(user.name()), span), + "groups" => Value::list(groups, span), + }; + + Value::record(record, span) + }) + .collect(); + + Value::list(users, span) +} + +pub fn host(span: Span) -> Record { let mut record = Record::new(); if let Some(name) = System::name() { @@ -160,27 +184,7 @@ pub fn host(span: Span) -> Value { let timestamp_str = datetime.with_timezone(datetime.offset()).to_rfc3339(); record.push("boot_time", Value::string(timestamp_str, span)); - let users = Users::new_with_refreshed_list() - .iter() - .map(|user| { - let groups = user - .groups() - .iter() - .map(|group| Value::string(trim_cstyle_null(group.name()), span)) - .collect(); - - let record = record! { - "name" => Value::string(trim_cstyle_null(user.name()), span), - "groups" => Value::list(groups, span), - }; - - Value::record(record, span) - }) - .collect(); - - record.push("sessions", Value::list(users, span)); - - Value::record(record, span) + record } pub fn temp(span: Span) -> Value { diff --git a/crates/nu-command/src/system/sys/sys_.rs b/crates/nu-command/src/system/sys/sys_.rs index 39dc2d419b..2886836be9 100644 --- a/crates/nu-command/src/system/sys/sys_.rs +++ b/crates/nu-command/src/system/sys/sys_.rs @@ -43,8 +43,11 @@ impl Command for Sys { ); let head = call.head; + + let mut host = super::host(head); + host.push("sessions", super::users(head)); let record = record! { - "host" => super::host(head), + "host" => Value::record(host, head), "cpu" => super::cpu(head), "disks" => super::disks(head), "mem" => super::mem(head), diff --git a/crates/nu-command/src/system/sys/users.rs b/crates/nu-command/src/system/sys/users.rs new file mode 100644 index 0000000000..9aab2b9b7b --- /dev/null +++ b/crates/nu-command/src/system/sys/users.rs @@ -0,0 +1,38 @@ +use nu_engine::command_prelude::*; + +#[derive(Clone)] +pub struct SysUsers; + +impl Command for SysUsers { + fn name(&self) -> &str { + "sys users" + } + + fn signature(&self) -> Signature { + Signature::build("sys users") + .category(Category::System) + .input_output_types(vec![(Type::Nothing, Type::record())]) + } + + fn usage(&self) -> &str { + "View information about the users on the system." + } + + fn run( + &self, + _engine_state: &EngineState, + _stack: &mut Stack, + call: &Call, + _input: PipelineData, + ) -> Result { + Ok(super::users(call.head).into_pipeline_data()) + } + + fn examples(&self) -> Vec { + vec![Example { + description: "Show info about the system users", + example: "sys users", + result: None, + }] + } +}