-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.go
72 lines (57 loc) · 1.4 KB
/
types.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
71
72
package main
import (
"bufio"
"fmt"
"io"
"log"
"strconv"
"strings"
)
type PasswdLine struct {
Name string `json:"name"`
Password string `json:"password"`
UID int64 `json:"uid"`
GID int64 `json:"gid"`
Fullname string `json:"fullname"`
Home string `json:"home"`
Shell string `json:"shell"`
}
func (p PasswdLine) Marshal() ([]byte, error) {
return []byte(fmt.Sprintf(
"%s:%s:%d:%d:%s:%s:%s",
p.Name, p.Password, p.UID, p.GID,
p.Fullname, p.Home, p.Shell,
)), nil
}
func (p *PasswdLine) UnmarshalText(text []byte) (err error) {
items := strings.Split(string(text), ":")
if len(items) < 7 {
return fmt.Errorf("invalid passwd entry string, expected 7 values colon delimited, got %d", len(items))
}
p.Name, p.Password, p.Fullname, p.Home, p.Shell = items[0], items[1], items[4], items[5], items[6]
if p.UID, err = strconv.ParseInt(items[2], 10, 32); err != nil {
return err
}
if p.GID, err = strconv.ParseInt(items[2], 10, 32); err != nil {
return err
}
return nil
}
func ParsePasswd(r io.Reader) map[string]PasswdLine {
rd := bufio.NewReader(r)
passwd := make(map[string]PasswdLine)
for {
line, err := rd.ReadBytes('\n')
line = []byte(strings.TrimRight(string(line), "\n"))
if err != nil {
break
}
p := PasswdLine{}
if err := p.UnmarshalText(line); err != nil {
log.Println(err.Error())
return passwd
}
passwd[p.Name] = p
}
return passwd
}