...

Source file src/os/user/lookup.go

     1	// Copyright 2011 The Go Authors. All rights reserved.
     2	// Use of this source code is governed by a BSD-style
     3	// license that can be found in the LICENSE file.
     4	
     5	package user
     6	
     7	import "sync"
     8	
     9	// Current returns the current user.
    10	//
    11	// The first call will cache the current user information.
    12	// Subsequent calls will return the cached value and will not reflect
    13	// changes to the current user.
    14	func Current() (*User, error) {
    15		cache.Do(func() { cache.u, cache.err = current() })
    16		if cache.err != nil {
    17			return nil, cache.err
    18		}
    19		u := *cache.u // copy
    20		return &u, nil
    21	}
    22	
    23	// cache of the current user
    24	var cache struct {
    25		sync.Once
    26		u   *User
    27		err error
    28	}
    29	
    30	// Lookup looks up a user by username. If the user cannot be found, the
    31	// returned error is of type UnknownUserError.
    32	func Lookup(username string) (*User, error) {
    33		if u, err := Current(); err == nil && u.Username == username {
    34			return u, err
    35		}
    36		return lookupUser(username)
    37	}
    38	
    39	// LookupId looks up a user by userid. If the user cannot be found, the
    40	// returned error is of type UnknownUserIdError.
    41	func LookupId(uid string) (*User, error) {
    42		if u, err := Current(); err == nil && u.Uid == uid {
    43			return u, err
    44		}
    45		return lookupUserId(uid)
    46	}
    47	
    48	// LookupGroup looks up a group by name. If the group cannot be found, the
    49	// returned error is of type UnknownGroupError.
    50	func LookupGroup(name string) (*Group, error) {
    51		return lookupGroup(name)
    52	}
    53	
    54	// LookupGroupId looks up a group by groupid. If the group cannot be found, the
    55	// returned error is of type UnknownGroupIdError.
    56	func LookupGroupId(gid string) (*Group, error) {
    57		return lookupGroupId(gid)
    58	}
    59	
    60	// GroupIds returns the list of group IDs that the user is a member of.
    61	func (u *User) GroupIds() ([]string, error) {
    62		return listGroups(u)
    63	}
    64	

View as plain text