-
Notifications
You must be signed in to change notification settings - Fork 2
/
user.go
64 lines (57 loc) · 1.03 KB
/
user.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
package main
import (
"bufio"
"encoding/csv"
"io"
"os"
"strings"
)
type accessType string
const (
readonly accessType = "RO"
writeonly accessType = "WO"
readwrite accessType = "RW"
)
type userData struct {
name string
password string
access accessType
}
func loadUserDataFromFile(filename string) map[string]userData {
fp, _ := os.Open(filename)
return loadCSV(bufio.NewReader(fp))
}
func loadCSV(reader io.Reader) map[string]userData {
var data = map[string]userData{}
var accT accessType
csvReader := csv.NewReader(reader)
csvReader.Comma = ','
for {
record, err := csvReader.Read()
if err == io.EOF {
break
}
if len(record) > 1 {
if len(record) > 2 {
switch strings.ToUpper(record[2]) {
case "RO":
accT = readonly
case "WO":
accT = writeonly
case "RW":
accT = readwrite
default:
accT = readonly
}
} else {
accT = readonly
}
data[record[0]] = userData{
name: record[0],
password: record[1],
access: accT,
}
}
}
return data
}