mirror of
https://github.com/nushell/nushell
synced 2025-01-14 06:04:09 +00:00
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:
parent
c54d223ea0
commit
b9331d1b08
5 changed files with 71 additions and 24 deletions
|
@ -125,6 +125,7 @@ pub fn add_shell_command_context(mut engine_state: EngineState) -> EngineState {
|
||||||
SysMem,
|
SysMem,
|
||||||
SysNet,
|
SysNet,
|
||||||
SysTemp,
|
SysTemp,
|
||||||
|
SysUsers,
|
||||||
UName,
|
UName,
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
|
@ -26,7 +26,8 @@ impl Command for SysHost {
|
||||||
call: &Call,
|
call: &Call,
|
||||||
_input: PipelineData,
|
_input: PipelineData,
|
||||||
) -> Result<PipelineData, ShellError> {
|
) -> 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> {
|
fn examples(&self) -> Vec<Example> {
|
||||||
|
|
|
@ -5,6 +5,7 @@ mod mem;
|
||||||
mod net;
|
mod net;
|
||||||
mod sys_;
|
mod sys_;
|
||||||
mod temp;
|
mod temp;
|
||||||
|
mod users;
|
||||||
|
|
||||||
pub use cpu::SysCpu;
|
pub use cpu::SysCpu;
|
||||||
pub use disks::SysDisks;
|
pub use disks::SysDisks;
|
||||||
|
@ -13,6 +14,7 @@ pub use mem::SysMem;
|
||||||
pub use net::SysNet;
|
pub use net::SysNet;
|
||||||
pub use sys_::Sys;
|
pub use sys_::Sys;
|
||||||
pub use temp::SysTemp;
|
pub use temp::SysTemp;
|
||||||
|
pub use users::SysUsers;
|
||||||
|
|
||||||
use chrono::{DateTime, Local};
|
use chrono::{DateTime, Local};
|
||||||
use nu_protocol::{record, Record, Span, Value};
|
use nu_protocol::{record, Record, Span, Value};
|
||||||
|
@ -122,7 +124,29 @@ pub fn mem(span: Span) -> Value {
|
||||||
Value::record(record, span)
|
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();
|
let mut record = Record::new();
|
||||||
|
|
||||||
if let Some(name) = System::name() {
|
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();
|
let timestamp_str = datetime.with_timezone(datetime.offset()).to_rfc3339();
|
||||||
record.push("boot_time", Value::string(timestamp_str, span));
|
record.push("boot_time", Value::string(timestamp_str, span));
|
||||||
|
|
||||||
let users = Users::new_with_refreshed_list()
|
record
|
||||||
.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)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn temp(span: Span) -> Value {
|
pub fn temp(span: Span) -> Value {
|
||||||
|
|
|
@ -43,8 +43,11 @@ impl Command for Sys {
|
||||||
);
|
);
|
||||||
|
|
||||||
let head = call.head;
|
let head = call.head;
|
||||||
|
|
||||||
|
let mut host = super::host(head);
|
||||||
|
host.push("sessions", super::users(head));
|
||||||
let record = record! {
|
let record = record! {
|
||||||
"host" => super::host(head),
|
"host" => Value::record(host, head),
|
||||||
"cpu" => super::cpu(head),
|
"cpu" => super::cpu(head),
|
||||||
"disks" => super::disks(head),
|
"disks" => super::disks(head),
|
||||||
"mem" => super::mem(head),
|
"mem" => super::mem(head),
|
||||||
|
|
38
crates/nu-command/src/system/sys/users.rs
Normal file
38
crates/nu-command/src/system/sys/users.rs
Normal 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,
|
||||||
|
}]
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue