-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimecard.go
58 lines (45 loc) · 949 Bytes
/
timecard.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
package main
import (
"path/filepath"
"io/ioutil"
"encoding/json"
"os"
)
const dbdir = "./db"
type TimeCard struct {
User, Password string
Time [52][7]int
}
func (t *TimeCard) punch(week, day, time int) {
t.Time[week][day] = time
}
func writeTC(t *TimeCard) {
dest := filepath.Join(dbdir, t.User + ".json")
data, err := json.Marshal(t)
check(err)
err = ioutil.WriteFile(dest, data, 0644)
check(err)
}
func readFile(file string) *TimeCard {
var out TimeCard
f, err := os.Open(file)
check(err)
defer f.Close()
data, err := ioutil.ReadAll(f)
check(err)
err = json.Unmarshal(data, &out)
check(err)
return &out
}
func readDB(user string) *TimeCard {
return readFile(filepath.Join(dbdir, user + ".json"))
}
func readAll() []*TimeCard{
var out []*TimeCard
files, err := ioutil.ReadDir(dbdir)
check(err)
for _, f := range files {
out = append(out, readFile(filepath.Join(dbdir, f.Name())))
}
return out
}