cross-platformaccountvlang

How to get a list of system users in vlang?


I am trying to list all the system users, I know that on Linux I could use awk -F: '{ print $1 }' /etc/passwd and on Windows I could use wmic useraccount get name but I was wondering if there was function in V that would be better suited for it and didn't require different commands for Linux and Windows.


Solution

  • EDIT: You can now use fn user_names() ?[]string from the os module in order to get the names of users on the system.

    There currently is now way to get a list of users on the system. However you can you compile time if statements to execute the commands you referred to above.

    import os
    
    fn main() {
        $if windows {
            result := os.execute('wmic useraccount get name')?
        } $else $if linux {
            result := os.execute('awk -F: "{ print $1 }" /etc/passwd')?
        } $else {
            println('unsupported os')
        }
        // do something with result
    }