Implement getUserInfo function to read environment variables #24
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
Implemented the
getUserInfo()
function in the CLI package to read environment variables and populate theUserInfo
struct with appropriate user information for both sudo and non-sudo scenarios. This centralizes environment variable access and provides the data needed for proper user context handling throughout the application.Implementation Details
Core Functions Added
getUserInfo()
- Main function that detects sudo usage and returns appropriate UserInfogetCurrentUserInfo()
- Helper function for non-sudo scenariosgetConfigDir(homeDir)
- Helper function for config directory logic with XDG supportEnvironment Variables Handled
SUDO_USER
- Detects if running under sudo and gets original usernameSUDO_UID
- Original user ID (parsed to integer)SUDO_GID
- Original group ID (parsed to integer)XDG_CONFIG_HOME
- XDG config directory path for proper config locationUserInfo Struct Population
The function properly populates all UserInfo fields:
Key Features
Sudo Detection & Handling
SUDO_USER
environment variableSUDO_UID
andSUDO_GID
Configuration Directory Logic
XDG_CONFIG_HOME
when available~/.config/coder_jail
Error Handling
Code Cleanup
Removed all commented environment variable code that was no longer needed:
namespace/linux.go
- Removed 36+ lines of commented sudo handling codenamespace/macos.go
- Removed 26+ lines of commented sudo handling codetls/tls.go
- Removed entire commentedgetConfigDir()
function (37+ lines)Testing
Benefits
Usage
The
getUserInfo()
function is called once during CLI initialization and provides the user context needed by: