-
Notifications
You must be signed in to change notification settings - Fork 0
/
users.go
70 lines (51 loc) · 1.08 KB
/
users.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
package main
import (
"bufio"
"io"
"os"
"strconv"
"strings"
)
// https://www.socketloop.com/tutorials/golang-get-all-local-users-and-print-out-their-home-directory-description-and-group-id
type User struct {
Uid uint32
Gid uint32
}
var users = []User{}
func getUsers() ([]User, error) {
result := []User{}
file, err := os.Open("/etc/passwd")
if err != nil {
return result, err
}
defer file.Close()
reader := bufio.NewReader(file)
for {
line, err := reader.ReadString('\n')
if equal := strings.Index(line, "#"); equal < 0 {
lineSlice := strings.FieldsFunc(line, func(divide rune) bool {
return divide == ':'
})
if len(lineSlice) >= 3 {
uid, err := strconv.Atoi(lineSlice[2])
if err != nil {
return result, err
}
if uid >= 1000 && uid != 65534 {
gid, err := strconv.Atoi(lineSlice[3])
if err != nil {
return result, err
}
result = append(result, User{Uid: uint32(uid), Gid: uint32(gid)})
}
}
}
if err == io.EOF {
break
}
if err != nil {
return result, err
}
}
return result, nil
}