From a786c58d0ab3af1b3ae439870ebf6ecaf25e1c02 Mon Sep 17 00:00:00 2001 From: Adam Oates <31167933+islonely@users.noreply.github.com> Date: Wed, 18 May 2022 10:37:34 +0000 Subject: [PATCH] os: add `fn user_names()` (#14424) --- vlib/os/os.v | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/vlib/os/os.v b/vlib/os/os.v index e37829e1a3..b7574fc646 100644 --- a/vlib/os/os.v +++ b/vlib/os/os.v @@ -343,6 +343,28 @@ pub fn user_os() string { return 'unknown' } +// user_names returns an array of the name of every user on the system. +pub fn user_names() ?[]string { + $if windows { + result := execute('wmic useraccount get name') + if result.exit_code != 0 { + return error('Failed to get user names. Exited with code $result.exit_code: $result.output') + } + mut users := result.output.split_into_lines() + // windows command prints an empty line at the end of output + users.delete(users.len - 1) + return users + } $else { + lines := read_lines('/etc/passwd')? + mut users := []string{cap: lines.len} + for line in lines { + end_name := line.index(':') or { line.len } + users << line[0..end_name] + } + return users + } +} + // home_dir returns path to the user's home directory. pub fn home_dir() string { $if windows {