Skip to main content

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            // On Windows, Uid wraps a SID (Security Identifier) which is a string like "S-1-5-18"
53            // On Unix-like systems (macOS, Linux, BSD), Uid wraps a numeric u32 user ID
54            // We need conditional compilation to handle these platform differences
55            #[cfg(windows)]
56            let id_value = Value::string(user.id().to_string(), span);
57            #[cfg(not(windows))]
58            let id_value = {
59                let id_ref: &u32 = user.id();
60                Value::int((*id_ref) as i64, span)
61            };
62
63            let record = record! {
64                "id" => id_value,
65                "name" => Value::string(trim_cstyle_null(user.name()), span),
66                "groups" => Value::list(groups, span),
67            };
68
69            Value::record(record, span)
70        })
71        .collect();
72
73    Value::list(users, span)
74}