127 lines
2.4 KiB
Go
127 lines
2.4 KiB
Go
|
// Copyright 2024 Matthew Rich <matthewrich.conf@gmail.com>. All rights reserved.
|
||
|
|
||
|
package system
|
||
|
|
||
|
import (
|
||
|
"os/user"
|
||
|
"strconv"
|
||
|
"regexp"
|
||
|
"log/slog"
|
||
|
)
|
||
|
|
||
|
var MatchId *regexp.Regexp = regexp.MustCompile(`^[0-9]+$`)
|
||
|
|
||
|
func LookupUIDString(userName string) string {
|
||
|
user, userLookupErr := user.Lookup(userName)
|
||
|
if userLookupErr != nil {
|
||
|
return ""
|
||
|
}
|
||
|
return user.Uid
|
||
|
}
|
||
|
|
||
|
func LookupUID(userName string) (int, error) {
|
||
|
var userLookupErr error
|
||
|
var UID string
|
||
|
if MatchId.MatchString(userName) {
|
||
|
user, err := user.LookupId(userName)
|
||
|
slog.Info("LookupUID() numeric", "user", user, "userLookupErr", err)
|
||
|
if err != nil {
|
||
|
userLookupErr = err
|
||
|
UID = userName
|
||
|
} else {
|
||
|
UID = user.Uid
|
||
|
}
|
||
|
} else {
|
||
|
if user, err := user.Lookup(userName); err != nil {
|
||
|
return -1, err
|
||
|
} else {
|
||
|
UID = user.Uid
|
||
|
}
|
||
|
}
|
||
|
|
||
|
uid, uidErr := strconv.Atoi(UID)
|
||
|
slog.Info("LookupUID()", "uid", uid, "uidErr", uidErr)
|
||
|
if uidErr != nil {
|
||
|
if userLookupErr != nil {
|
||
|
return -1, userLookupErr
|
||
|
} else {
|
||
|
return -1, uidErr
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return uid, userLookupErr
|
||
|
}
|
||
|
|
||
|
func LookupGID(groupName string) (int, error) {
|
||
|
var GID string
|
||
|
if MatchId.MatchString(groupName) {
|
||
|
group, groupLookupErr := user.LookupGroupId(groupName)
|
||
|
if groupLookupErr != nil {
|
||
|
//return -1, groupLookupErr
|
||
|
GID = groupName
|
||
|
} else {
|
||
|
GID = group.Gid
|
||
|
}
|
||
|
} else {
|
||
|
group, groupLookupErr := user.LookupGroup(groupName)
|
||
|
if groupLookupErr != nil {
|
||
|
return -1, groupLookupErr
|
||
|
}
|
||
|
GID = group.Gid
|
||
|
}
|
||
|
|
||
|
gid, gidErr := strconv.Atoi(GID)
|
||
|
if gidErr != nil {
|
||
|
return -1, gidErr
|
||
|
}
|
||
|
|
||
|
return gid, nil
|
||
|
}
|
||
|
|
||
|
func LookupGIDString(groupName string) string {
|
||
|
group, groupLookupErr := user.LookupGroup(groupName)
|
||
|
if groupLookupErr != nil {
|
||
|
return ""
|
||
|
}
|
||
|
return group.Gid
|
||
|
}
|
||
|
|
||
|
func ProcessUser() *user.User {
|
||
|
processUser, userErr := user.Current()
|
||
|
if userErr != nil {
|
||
|
panic(userErr)
|
||
|
}
|
||
|
return processUser
|
||
|
}
|
||
|
|
||
|
func ProcessGroup(u *user.User) *user.Group {
|
||
|
if u == nil {
|
||
|
u = ProcessUser()
|
||
|
}
|
||
|
processGroup, groupErr := user.LookupGroupId(u.Gid)
|
||
|
if groupErr != nil {
|
||
|
panic(groupErr)
|
||
|
}
|
||
|
return processGroup
|
||
|
}
|
||
|
|
||
|
func ProcessUserName() string {
|
||
|
processUser, userErr := user.Current()
|
||
|
if userErr != nil {
|
||
|
panic(userErr)
|
||
|
}
|
||
|
return processUser.Username
|
||
|
}
|
||
|
|
||
|
func ProcessGroupName() string {
|
||
|
processUser, userErr := user.Current()
|
||
|
if userErr != nil {
|
||
|
panic(userErr)
|
||
|
}
|
||
|
processGroup, groupErr := user.LookupGroupId(processUser.Gid)
|
||
|
if groupErr != nil {
|
||
|
panic(groupErr)
|
||
|
}
|
||
|
return processGroup.Name
|
||
|
}
|