mirror of
https://github.com/qdm12/gluetun.git
synced 2026-05-06 20:10:11 +02:00
4a78989d9d
- main reason being it's a burden to always define sentinel errors at global scope, wrap them with `%w` instead of using a string directly - only use sentinel errors when it has to be checked using `errors.Is` - replace all usage of these sentinel errors in `fmt.Errorf` with direct strings that were in the sentinel error - exclude the sentinel error definition requirement from .golangci.yml - update unit tests to use ContainersError instead of ErrorIs so it stays as a "not a change detector test" without requiring a sentinel error
52 lines
1.1 KiB
Go
52 lines
1.1 KiB
Go
package alpine
|
|
|
|
import (
|
|
"fmt"
|
|
"io/fs"
|
|
"os"
|
|
"os/user"
|
|
"strconv"
|
|
)
|
|
|
|
// CreateUser creates a user in Alpine with the given UID.
|
|
func (a *Alpine) CreateUser(username string, uid int) (createdUsername string, err error) {
|
|
UIDStr := strconv.Itoa(uid)
|
|
u, err := a.lookupID(UIDStr)
|
|
_, unknownUID := err.(user.UnknownUserIdError)
|
|
if err != nil && !unknownUID {
|
|
return "", err
|
|
}
|
|
|
|
if u != nil {
|
|
if u.Username == username {
|
|
return "", nil
|
|
}
|
|
return u.Username, nil
|
|
}
|
|
|
|
u, err = a.lookup(username)
|
|
_, unknownUsername := err.(user.UnknownUserError)
|
|
if err != nil && !unknownUsername {
|
|
return "", err
|
|
}
|
|
|
|
if u != nil {
|
|
return "", fmt.Errorf("user already exists: with name %s for ID %s instead of %d",
|
|
username, u.Uid, uid)
|
|
}
|
|
|
|
const permission = fs.FileMode(0o644)
|
|
file, err := os.OpenFile(a.passwdPath, os.O_APPEND|os.O_WRONLY, permission)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
s := fmt.Sprintf("%s:x:%d:::/dev/null:/sbin/nologin\n", username, uid)
|
|
_, err = file.WriteString(s)
|
|
if err != nil {
|
|
_ = file.Close()
|
|
return "", err
|
|
}
|
|
|
|
return username, file.Close()
|
|
}
|