jx/internal/resource/os.go

45 lines
809 B
Go
Raw Permalink Normal View History

2024-03-20 19:23:31 +00:00
// Copyright 2024 Matthew Rich <matthewrich.conf@gmail.com>. All rights reserved.
2024-03-22 17:39:06 +00:00
2024-03-20 16:15:27 +00:00
package resource
import (
2024-03-25 20:31:06 +00:00
"os/user"
"strconv"
2024-03-20 16:15:27 +00:00
)
2024-03-20 19:23:31 +00:00
func LookupUIDString(userName string) string {
2024-03-25 20:31:06 +00:00
user, userLookupErr := user.Lookup(userName)
if userLookupErr != nil {
return ""
}
return user.Uid
2024-03-20 19:23:31 +00:00
}
2024-03-25 20:31:06 +00:00
func LookupUID(userName string) (int, error) {
user, userLookupErr := user.Lookup(userName)
if userLookupErr != nil {
return -1, userLookupErr
}
2024-03-20 16:15:27 +00:00
2024-03-25 20:31:06 +00:00
uid, uidErr := strconv.Atoi(user.Uid)
if uidErr != nil {
return -1, uidErr
}
2024-03-20 16:15:27 +00:00
2024-03-25 20:31:06 +00:00
return uid, nil
2024-03-20 16:15:27 +00:00
}
2024-03-25 20:31:06 +00:00
func LookupGID(groupName string) (int, error) {
group, groupLookupErr := user.LookupGroup(groupName)
if groupLookupErr != nil {
return -1, groupLookupErr
}
gid, gidErr := strconv.Atoi(group.Gid)
if gidErr != nil {
return -1, gidErr
}
return gid, nil
2024-03-20 16:15:27 +00:00
}