add system pkg
This commit is contained in:
parent
6925598bf2
commit
d88b67ea2f
126
internal/system/os.go
Normal file
126
internal/system/os.go
Normal file
@ -0,0 +1,126 @@
|
||||
// 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
|
||||
}
|
38
internal/system/os_test.go
Normal file
38
internal/system/os_test.go
Normal file
@ -0,0 +1,38 @@
|
||||
// Copyright 2024 Matthew Rich <matthewrich.conf@gmail.com>. All rights reserved.
|
||||
|
||||
|
||||
package system
|
||||
|
||||
import (
|
||||
_ "context"
|
||||
_ "encoding/json"
|
||||
"github.com/stretchr/testify/assert"
|
||||
_ "io"
|
||||
_ "net/http"
|
||||
_ "net/http/httptest"
|
||||
_ "net/url"
|
||||
_ "strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestLookupUID(t *testing.T) {
|
||||
uid, e := LookupUID("nobody")
|
||||
|
||||
assert.Nil(t, e)
|
||||
assert.Equal(t, 65534, uid)
|
||||
|
||||
nuid, ne := LookupUID("10101")
|
||||
assert.Error(t, ne, "user: unknonwn userid ", ne)
|
||||
assert.Equal(t, 10101, nuid)
|
||||
}
|
||||
|
||||
func TestLookupGID(t *testing.T) {
|
||||
gid, e := LookupGID("adm")
|
||||
|
||||
assert.Nil(t, e)
|
||||
assert.Equal(t, 4, gid)
|
||||
|
||||
ngid, ne := LookupGID("1001")
|
||||
assert.Nil(t, ne)
|
||||
assert.Equal(t, 1001, ngid)
|
||||
}
|
Loading…
Reference in New Issue
Block a user