nu_command/system/sys/
users.rs

1use super::trim_cstyle_null;
2use nu_engine::command_prelude::*;
3use sysinfo::Users;
4
5#[derive(Clone)]
6pub struct SysUsers;
7
8impl Command for SysUsers {
9    fn name(&self) -> &str {
10        "sys users"
11    }
12
13    fn signature(&self) -> Signature {
14        Signature::build("sys users")
15            .category(Category::System)
16            .input_output_types(vec![(Type::Nothing, Type::table())])
17    }
18
19    fn description(&self) -> &str {
20        "View information about the users on the system."
21    }
22
23    fn run(
24        &self,
25        _engine_state: &EngineState,
26        _stack: &mut Stack,
27        call: &Call,
28        _input: PipelineData,
29    ) -> Result<PipelineData, ShellError> {
30        Ok(users(call.head).into_pipeline_data())
31    }
32
33    fn examples(&self) -> Vec<Example> {
34        vec![Example {
35            description: "Show info about the system users",
36            example: "sys users",
37            result: None,
38        }]
39    }
40}
41
42fn users(span: Span) -> Value {
43    let users = Users::new_with_refreshed_list()
44        .iter()
45        .map(|user| {
46            let groups = user
47                .groups()
48                .iter()
49                .map(|group| Value::string(trim_cstyle_null(group.name()), span))
50                .collect();
51
52            let record = record! {
53                "name" => Value::string(trim_cstyle_null(user.name()), span),
54                "groups" => Value::list(groups, span),
55            };
56
57            Value::record(record, span)
58        })
59        .collect();
60
61    Value::list(users, span)
62}