-
Notifications
You must be signed in to change notification settings - Fork 55
/
housework.go
147 lines (119 loc) · 2.4 KB
/
housework.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package main
import (
"flag"
"fmt"
"log"
"os"
"path/filepath"
"strconv"
"strings"
"github.com/awoodbeck/gnp/ch12/housework"
storage "github.com/awoodbeck/gnp/ch12/json"
// storage "github.com/awoodbeck/gnp/ch12/gob"
// storage "github.com/awoodbeck/gnp/ch12/protobuf"
)
var dataFile string
func init() {
flag.StringVar(&dataFile, "file", "housework.db", "data file")
flag.Usage = func() {
fmt.Fprintf(flag.CommandLine.Output(),
`Usage: %s [flags] [add chore, ...|complete #]
add add comma-separated chores
complete complete designated chore
Flags:
`, filepath.Base(os.Args[0]))
flag.PrintDefaults()
}
}
func load() ([]*housework.Chore, error) {
if _, err := os.Stat(dataFile); os.IsNotExist(err) {
return make([]*housework.Chore, 0), nil
}
df, err := os.Open(dataFile)
if err != nil {
return nil, err
}
defer func() {
if err := df.Close(); err != nil {
fmt.Printf("closing data file: %v", err)
}
}()
return storage.Load(df)
}
func flush(chores []*housework.Chore) error {
df, err := os.Create(dataFile)
if err != nil {
return err
}
defer func() {
if err := df.Close(); err != nil {
fmt.Printf("closing data file: %v", err)
}
}()
return storage.Flush(df, chores)
}
func list() error {
chores, err := load()
if err != nil {
return err
}
if len(chores) == 0 {
fmt.Println("You're all caught up!")
return nil
}
fmt.Println("#\t[X]\tDescription")
for i, chore := range chores {
c := " "
if chore.Complete {
c = "X"
}
fmt.Printf("%d\t[%s]\t%s\n", i+1, c, chore.Description)
}
return nil
}
func add(s string) error {
chores, err := load()
if err != nil {
return err
}
for _, chore := range strings.Split(s, ",") {
if desc := strings.TrimSpace(chore); desc != "" {
chores = append(chores, &housework.Chore{
Description: desc,
})
}
}
return flush(chores)
}
func complete(s string) error {
i, err := strconv.Atoi(s)
if err != nil {
return err
}
chores, err := load()
if err != nil {
return err
}
if i < 1 || i > len(chores) {
return fmt.Errorf("chore %d not found", i)
}
chores[i-1].Complete = true
return flush(chores)
}
func main() {
flag.Parse()
var err error
switch strings.ToLower(flag.Arg(0)) {
case "add":
err = add(strings.Join(flag.Args()[1:], " "))
case "complete":
err = complete(flag.Arg(1))
}
if err != nil {
log.Fatal(err)
}
err = list()
if err != nil {
log.Fatal(err)
}
}