-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.go
87 lines (73 loc) · 1.63 KB
/
model.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package main
import (
"encoding/gob"
"fmt"
"io"
"os"
"text/tabwriter"
"time"
)
const fileName = "wcc.dat"
type status string
var (
statusNoChange status = "no change"
statusError status = "error"
statusUpdated status = "updated"
)
type wccWebsite struct {
URL string
Selector string
Hash string
Status status
Error error
LastUpdated time.Time
LastChecked time.Time
}
type wccModel struct {
Websites []*wccWebsite
}
func open() (*wccModel, error) {
wcc := &wccModel{}
_, err := os.Stat(fileName)
if err == nil {
f, err := os.Open(fileName)
if err != nil {
return nil, err
}
defer f.Close()
err = gob.NewDecoder(f).Decode(wcc)
if err != nil {
return nil, err
}
}
return wcc, nil
}
func (wcc *wccModel) fprint(output io.Writer) {
tw := &tabwriter.Writer{}
tw.Init(output, 0, 4, 0, ' ', 0)
fmt.Fprint(tw, "No.\t | Status\t | URL\t | Selector\t | LastUpdated\t | LastChecked\n")
fmt.Fprint(tw, "---\t | ------\t | ---\t | --------\t | -----------\t | -----------\n")
for i, ws := range wcc.Websites {
var lastUpdated, lastChecked string
if !ws.LastUpdated.IsZero() {
lastUpdated = ws.LastUpdated.Format("2006/01/02 15:04:05")
}
if !ws.LastChecked.IsZero() {
lastChecked = ws.LastChecked.Format("2006/01/02 15:04:05")
}
fmt.Fprintf(tw, "%d\t | %s\t | %s\t | %s\t | %s\t | %s\n", i+1, ws.Status, ws.URL, ws.Selector, lastUpdated, lastChecked)
}
tw.Flush()
}
func (wcc *wccModel) save() error {
f, err := os.Create(fileName)
if err != nil {
return err
}
defer f.Close()
err = gob.NewEncoder(f).Encode(wcc)
if err != nil {
return err
}
return nil
}