-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add System.username #12605
base: master
Are you sure you want to change the base?
Add System.username #12605
Conversation
The Windows equivalent is You might notice that Personally I prefer it being a class method of just |
For this part, my thinking was to have the stdlib do the harder thing of making system calls, and leave the choice of checking env vars up to user code, if the situation should or shouldn't respect env vars being different than the result of getuid or GetUserNameExW. |
I pushed two fixup commits to do this, one to just do it, and another to refactor out the duplicate code. I left the windows part out for myself, since I don't have a way to test that at the moment, and while I could make a VM or something, it'll realistically be several weeks before I could find time to get that all working, so maybe someone else could pick that part up? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd suggest to call this method System.username
. It's more concise and follows the same scheme as System.hostname
.
Can you have multiple hostnames configured in a system, and a current one being used? Can you have multiple usernames in a system and a current one being used? |
On POSIX there are the differences between the real user, the effective user, and the login user ( |
I made this change in the last fixup
|
String.new(pwd.pw_name) | ||
else | ||
raise RuntimeError.from_errno("Could not get current user name") | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd suggest using an early-return.
String.new(pwd.pw_name) | |
else | |
raise RuntimeError.from_errno("Could not get current user name") | |
end | |
return String.new(pwd.pw_name) | |
end | |
raise RuntimeError.from_errno("Could not get current user name") |
In discussion of #12604 we determined that a useful shortcut would be to grab the current username directly, which avoids allocating several strings out of the lib passwd struct and the System::User class if the only thing one cares about is the current user name.
Reference #7738