-
Notifications
You must be signed in to change notification settings - Fork 0
/
storage.go
63 lines (58 loc) · 1.29 KB
/
storage.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
package ezpwd
import (
"bufio"
"fmt"
"io"
"regexp"
"strings"
)
var splitter = regexp.MustCompile("\\s/\\s")
type Password struct {
Service string
Login string
Password string
Comment string
}
func ReadPasswords(src io.Reader) ([]Password, error) {
passwords := make([]Password, 0, 50)
rdr := bufio.NewScanner(src)
for rdr.Scan() {
line := rdr.Text()
parts := splitter.Split(line, 4)
pLen := len(parts)
if pLen < 3 || pLen > 4 {
continue
}
p := Password{
Service: parts[0],
Login: parts[1],
Password: parts[2],
}
if pLen == 4 {
p.Comment = parts[3]
}
passwords = append(passwords, p)
}
return passwords, nil
}
func WritePasswords(passwords []Password, writer io.Writer) error {
wrtr := bufio.NewWriter(writer)
for _, pwd := range passwords {
if pwd.Comment != "" {
if _, err := wrtr.WriteString(fmt.Sprintf("%s / %s / %s / %s\n", strings.TrimSpace(pwd.Service),
strings.TrimSpace(pwd.Login), strings.TrimSpace(pwd.Password), strings.TrimSpace(pwd.Comment)),
); err != nil {
return err
}
} else {
if _, err := wrtr.WriteString(fmt.Sprintf("%s / %s / %s\n",
strings.TrimSpace(pwd.Service),
strings.TrimSpace(pwd.Login),
strings.TrimSpace(pwd.Password)),
); err != nil {
return err
}
}
}
return wrtr.Flush()
}