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.)
This commit is contained in:
Ian Manske 2024-05-07 12:52:02 +00:00 committed by GitHub
parent c54d223ea0
commit b9331d1b08
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 71 additions and 24 deletions

View file

@ -125,6 +125,7 @@ pub fn add_shell_command_context(mut engine_state: EngineState) -> EngineState {
SysMem,
SysNet,
SysTemp,
SysUsers,
UName,
};

View file

@ -26,7 +26,8 @@ impl Command for SysHost {
call: &Call,
_input: PipelineData,
) -> Result<PipelineData, ShellError> {
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<Example> {

View file

@ -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 {

View file

@ -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),

View file

@ -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<PipelineData, ShellError> {
Ok(super::users(call.head).into_pipeline_data())
}
fn examples(&self) -> Vec<Example> {
vec![Example {
description: "Show info about the system users",
example: "sys users",
result: None,
}]
}
}