os: add `fn user_names()` (#14424)

Adam Oates 2022-05-18 10:37:34 +00:00 committed by Jef Roosens
parent b37130e664
commit 88c4e8db29
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
1 changed files with 22 additions and 0 deletions

View File

@ -343,6 +343,28 @@ pub fn user_os() string {
return 'unknown' 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. // home_dir returns path to the user's home directory.
pub fn home_dir() string { pub fn home_dir() string {
$if windows { $if windows {