Skip to content

Commit 5c4e356

Browse files
Copilotabdurriq
andcommitted
Add documentation for _common helper scripts
Co-authored-by: abdurriq <[email protected]>
1 parent 79767c0 commit 5c4e356

1 file changed

Lines changed: 122 additions & 0 deletions

File tree

src/_common/README.md

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
# Common Helper Scripts
2+
3+
This directory contains common helper scripts that can be shared across multiple features to avoid code duplication.
4+
5+
## common-setup.sh
6+
7+
A helper script that provides common setup functions used across multiple features.
8+
9+
### Functions
10+
11+
#### `determine_user_from_input`
12+
13+
Determines the appropriate non-root user based on the input username.
14+
15+
**Usage:**
16+
```bash
17+
# Source the helper script
18+
source "${SCRIPT_DIR}/../_common/common-setup.sh"
19+
20+
# Determine the user
21+
USERNAME=$(determine_user_from_input "${USERNAME}" "root")
22+
```
23+
24+
**Parameters:**
25+
- `$1` (required): Input username from feature configuration (e.g., "automatic", "auto", "none", or a specific username)
26+
- `$2` (optional): Fallback username when no user is found in automatic mode (defaults to "root")
27+
28+
**Behavior:**
29+
- **"auto" or "automatic"**:
30+
- First checks if `_REMOTE_USER` environment variable is set and is not "root"
31+
- If `_REMOTE_USER` is root or not set, searches for an existing user from the priority list:
32+
1. `devcontainer`
33+
2. `vscode`
34+
3. `node`
35+
4. `codespace`
36+
5. User with UID 1000 (from `/etc/passwd`)
37+
- If no user is found, returns the fallback user (default: "root")
38+
39+
- **"none"**: Always returns "root"
40+
41+
- **Specific username**:
42+
- Validates the user exists using `id -u`
43+
- If the user exists, returns that username
44+
- If the user doesn't exist, returns "root"
45+
46+
**Examples:**
47+
48+
```bash
49+
# Basic usage with default fallback (root)
50+
USERNAME=$(determine_user_from_input "automatic")
51+
52+
# With custom fallback
53+
USERNAME=$(determine_user_from_input "automatic" "vscode")
54+
55+
# Explicit user
56+
USERNAME=$(determine_user_from_input "myuser")
57+
58+
# None (always returns root)
59+
USERNAME=$(determine_user_from_input "none")
60+
```
61+
62+
**Return Value:**
63+
Prints the resolved username to stdout, which can be captured using command substitution.
64+
65+
## Testing
66+
67+
Tests for the helper scripts are located in `/test/_common/`. Run the tests with:
68+
69+
```bash
70+
bash test/_common/test-common-setup.sh
71+
```
72+
73+
## Edge Cases
74+
75+
The helper handles several edge cases:
76+
77+
1. **Missing awk**: Some systems (like Mariner) don't have awk by default. Features should install it before sourcing the helper if needed.
78+
79+
2. **UID 1000 lookup**: The user with UID 1000 is included in the search as it's commonly the first non-system user created.
80+
81+
3. **_REMOTE_USER behavior**: When `_REMOTE_USER` is set to a non-root user, it takes priority over all other detection methods in automatic mode.
82+
83+
4. **Empty user list entries**: The helper safely handles empty entries in the user detection loop.
84+
85+
## Migration Guide
86+
87+
To migrate an existing feature to use the common helper:
88+
89+
### Before:
90+
```bash
91+
# Determine the appropriate non-root user
92+
if [ "${USERNAME}" = "auto" ] || [ "${USERNAME}" = "automatic" ]; then
93+
USERNAME=""
94+
POSSIBLE_USERS=("vscode" "node" "codespace" "$(awk -v val=1000 -F ":" '$3==val{print $1}' /etc/passwd)")
95+
for CURRENT_USER in "${POSSIBLE_USERS[@]}"; do
96+
if id -u ${CURRENT_USER} > /dev/null 2>&1; then
97+
USERNAME=${CURRENT_USER}
98+
break
99+
fi
100+
done
101+
if [ "${USERNAME}" = "" ]; then
102+
USERNAME=root
103+
fi
104+
elif [ "${USERNAME}" = "none" ] || ! id -u ${USERNAME} > /dev/null 2>&1; then
105+
USERNAME=root
106+
fi
107+
```
108+
109+
### After:
110+
```bash
111+
# Source common helper functions
112+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
113+
source "${SCRIPT_DIR}/../_common/common-setup.sh"
114+
115+
# Determine the appropriate non-root user
116+
USERNAME=$(determine_user_from_input "${USERNAME}" "root")
117+
```
118+
119+
**Note:** For features like `common-utils` that create users and need a different fallback, use:
120+
```bash
121+
USERNAME=$(determine_user_from_input "${USERNAME}" "vscode")
122+
```

0 commit comments

Comments
 (0)