-
Notifications
You must be signed in to change notification settings - Fork 4
/
group.go
70 lines (62 loc) · 1.16 KB
/
group.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Copyright © 2015-2016 Platina Systems, Inc. All rights reserved.
// Use of this source code is governed by the GPL-2 license described in the
// LICENSE file.
// +build linux
// Package group provides an /etc/group parser.
package group
import (
"bufio"
"os"
"strconv"
"strings"
)
type Entry struct {
pw string
gid int
members []string
}
func Parse() map[string]*Entry {
f, err := os.Open("/etc/group")
if err != nil {
return map[string]*Entry{}
}
defer f.Close()
m := make(map[string]*Entry)
scanner := bufio.NewScanner(f)
for scanner.Scan() {
fields := strings.Split(scanner.Text(), ":")
if len(fields) < 3 {
continue
}
entry := new(Entry)
entry.pw = fields[1]
gid, err := strconv.ParseInt(fields[2], 0, 0)
if err != nil {
continue
}
entry.gid = int(gid)
if len(fields) == 4 {
entry.members = strings.Split(fields[3], ",")
}
m[fields[0]] = entry
}
return m
}
func (p *Entry) Gid() (gid int) {
if p != nil {
gid = p.gid
}
return
}
func (p *Entry) Members() (members []string) {
if p != nil {
members = p.members
}
return
}
func (p *Entry) Passwd() (pw string) {
if p != nil {
pw = p.pw
}
return
}