10 // The current operating system does not provide the required data for user lookups.
11 ErrUnsupported = errors.New("user lookup: operating system does not provide passwd-formatted data")
14 func lookupUser(filter func(u User) bool) (User, error) {
15 // Get operating system-specific passwd reader-closer.
16 passwd, err := GetPasswd()
23 users, err := ParsePasswdFilter(passwd, filter)
28 // No user entries found.
30 return User{}, fmt.Errorf("no matching entries in passwd file")
33 // Assume the first entry is the "correct" one.
37 // CurrentUser looks up the current user by their user id in /etc/passwd. If the
38 // user cannot be found (or there is no /etc/passwd file on the filesystem),
39 // then CurrentUser returns an error.
40 func CurrentUser() (User, error) {
41 return LookupUid(syscall.Getuid())
44 // LookupUser looks up a user by their username in /etc/passwd. If the user
45 // cannot be found (or there is no /etc/passwd file on the filesystem), then
46 // LookupUser returns an error.
47 func LookupUser(username string) (User, error) {
48 return lookupUser(func(u User) bool {
49 return u.Name == username
53 // LookupUid looks up a user by their user id in /etc/passwd. If the user cannot
54 // be found (or there is no /etc/passwd file on the filesystem), then LookupId
56 func LookupUid(uid int) (User, error) {
57 return lookupUser(func(u User) bool {
62 func lookupGroup(filter func(g Group) bool) (Group, error) {
63 // Get operating system-specific group reader-closer.
64 group, err := GetGroup()
71 groups, err := ParseGroupFilter(group, filter)
76 // No user entries found.
78 return Group{}, fmt.Errorf("no matching entries in group file")
81 // Assume the first entry is the "correct" one.
85 // CurrentGroup looks up the current user's group by their primary group id's
86 // entry in /etc/passwd. If the group cannot be found (or there is no
87 // /etc/group file on the filesystem), then CurrentGroup returns an error.
88 func CurrentGroup() (Group, error) {
89 return LookupGid(syscall.Getgid())
92 // LookupGroup looks up a group by its name in /etc/group. If the group cannot
93 // be found (or there is no /etc/group file on the filesystem), then LookupGroup
95 func LookupGroup(groupname string) (Group, error) {
96 return lookupGroup(func(g Group) bool {
97 return g.Name == groupname
101 // LookupGid looks up a group by its group id in /etc/group. If the group cannot
102 // be found (or there is no /etc/group file on the filesystem), then LookupGid
104 func LookupGid(gid int) (Group, error) {
105 return lookupGroup(func(g Group) bool {