func groupLookup(gid string) (string, error) { g, err := user.LookupGroupId(gid) if err != nil { return "", err } return g.Name, nil }
// SetRuntimeGroup sets group on what to run as func SetRuntimeGroup(groupname string) (err error) { log.Printf("Setting gid to %s", groupname) gstruct, err := user.LookupGroupId(groupname) if err != nil { return } egid, _ := strconv.Atoi(gstruct.Gid) syscall.Setgid(egid) return }
func currentUser() *User { u := &User{ User: currentStdUser(), } ids, err := u.GroupIds() if err != nil { return u } for _, id := range ids { if g, err := user.LookupGroupId(id); err == nil { u.Groups = append(u.Groups, g) } } return u }
func init() { formatQualifier = func(q string, tag Tag) string { switch tag { case TagUser: usr, err := user.LookupId(q) if err != nil { return q } return usr.Username case TagGroup: grp, err := user.LookupGroupId(q) if err != nil { return q } return grp.Name default: return q } } }
func LookupGroup(groupname string) (*user.Group, error) { var gr *user.Group var err error r := regexp.MustCompile(`^\d+$`) if r.MatchString(groupname) { gr, err = user.LookupGroupId(groupname) if err != nil { return nil, err } } else { gr, err = user.LookupGroup(groupname) if err != nil { return nil, err } } return gr, err }
func valueFromUser(name string) (string, error) { currentUser, err := user.Current() if err != nil { return "", err } switch name { case "name": return currentUser.Username, nil case "uid": return currentUser.Uid, nil case "gid": return currentUser.Gid, nil case "home": return currentUser.HomeDir, nil case "group": group, err := user.LookupGroupId(currentUser.Gid) return group.Name, err default: return "", fmt.Errorf("unknown variable \"user.%s\"", name) } }
// Owner retrieves the owner and group for the file func (fu *FileUtil) Owner() (*FileOwner, error) { fi, err := os.Stat(fu.Path) if err != nil { return &FileOwner{}, err } uid := fi.Sys().(*syscall.Stat_t).Uid gid := fi.Sys().(*syscall.Stat_t).Gid u, err := user.LookupId(strconv.FormatInt(int64(uid), 10)) if err != nil { return &FileOwner{}, err } g, err := user.LookupGroupId(strconv.FormatInt(int64(gid), 10)) if err != nil { return &FileOwner{}, err } owner := &FileOwner{u, g} return owner, nil }
// NewFile creates a resource for managing files and directories func NewFile(name string) (Resource, error) { // Defaults for owner and group currentUser, err := user.Current() if err != nil { return nil, err } currentGroup, err := user.LookupGroupId(currentUser.Gid) if err != nil { return nil, err } // Resource defaults f := &File{ Base: Base{ Name: name, Type: "file", State: "present", Before: make([]string, 0), After: make([]string, 0), PresentStates: []string{"present"}, AbsentStates: []string{"absent"}, }, Path: name, Mode: 0644, Owner: currentUser.Username, Group: currentGroup.Name, FileType: fileTypeRegular, Recursive: false, Purge: false, outdated: make([]*outdatedFile, 0), extra: make(map[string]struct{}), } return f, nil }