/
main.go
176 lines (144 loc) · 4.1 KB
/
main.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package main
import (
"encoding/json"
"io/ioutil"
"os"
"path/filepath"
"strconv"
"time"
"github.com/olekukonko/tablewriter"
"github.com/urfave/cli"
"github.com/mauromorales/wad/config"
fm "github.com/mauromorales/wad/filemanager"
pt "github.com/mauromorales/wad/progresstracker"
)
func main() {
var cfg config.Config
app := cli.NewApp()
app.Name = "wad"
app.Usage = "A CLI tool to help you write"
app.Version = "0.1.0"
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "config-dir",
Usage: "path to config directory",
},
}
app.Before = func(c *cli.Context) error {
cfg = config.New(c.String("config-dir"), 500)
return nil
}
app.Commands = []cli.Command{
{
Name: "init",
Usage: "initializes WAD",
Action: func(c *cli.Context) error {
configFile := filepath.Join(cfg.Dir, "config.json")
filesFile := filepath.Join(cfg.Dir, "files.json")
progressFile := filepath.Join(cfg.Dir, "progress.json")
if _, err := os.Stat(configFile); err == nil {
return cli.NewExitError("WAD has already been initialized under `"+cfg.Dir+"'", 1)
} else {
config := map[string]int{"goal": 500}
json, _ := json.Marshal(config)
ioutil.WriteFile(configFile, json, 0644)
ioutil.WriteFile(filesFile, []byte("{}"), 0644)
ioutil.WriteFile(progressFile, []byte("{}"), 0644)
}
return nil
},
},
{
Name: "files",
Aliases: []string{"f"},
Usage: "track a given file",
Action: func(c *cli.Context) error {
filesFile := filepath.Join(cfg.Dir, "files.json")
fileManager := fm.NewFileManager(filesFile)
data := fileManager.GetFiles(true)
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"File", "Words", "Last tracked on"})
for _, v := range data {
table.Append(v)
}
table.Render()
return nil
},
},
{
Name: "track",
Aliases: []string{"t"},
Usage: "track a given file",
Action: func(c *cli.Context) error {
filesFile := filepath.Join(cfg.Dir, "files.json")
fileManager := fm.NewFileManager(filesFile)
current_time := time.Now().Local()
file, _ := filepath.Abs(c.Args().First())
words, err := fileManager.Track(file, current_time.Format("2006-01-02"))
if err != nil {
return cli.NewExitError(err, 1)
}
jsonContent, _ := json.Marshal(fileManager.Files)
ioutil.WriteFile(filesFile, jsonContent, 0644)
progressFile := filepath.Join(cfg.Dir, "progress.json")
progressTracker, _ := pt.NewProgressTracker(progressFile)
progressTracker.TrackFile(fileManager, current_time.Format("2006-01-02"), file, words, cfg.Goal)
jsonContent, _ = json.Marshal(progressTracker.Dates)
ioutil.WriteFile(progressFile, jsonContent, 0644)
return nil
},
},
{
Name: "progress",
Aliases: []string{"p"},
Usage: "display the current progress",
Flags: []cli.Flag{
cli.StringFlag{
Name: "from",
},
},
Action: func(c *cli.Context) error {
progressFile := filepath.Join(cfg.Dir, "progress.json")
progressTracker, _ := pt.NewProgressTracker(progressFile)
data := [][]string{}
lastGoal := -1
now := time.Now().Local()
from := now.AddDate(0, 0, -7)
for !from.After(now) {
var total int
current_date := from.Format("2006-01-02")
if day, ok := progressTracker.Dates[current_date]; ok {
for _, words := range day.Files {
total += words
}
var status string
if total >= day.Goal {
status = "+"
} else {
status = "-"
}
lastGoal = day.Goal
data = append(data, []string{current_date, strconv.Itoa(total), strconv.Itoa(day.Goal), status})
} else {
var goal string
if lastGoal < 0 {
goal = "n/a"
} else {
goal = strconv.Itoa(lastGoal)
}
data = append(data, []string{current_date, "0", goal, "n/a"})
}
from = from.AddDate(0, 0, 1)
}
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Day", "Words", "Goal", "Status"})
for _, v := range data {
table.Append(v)
}
table.Render()
return nil
},
},
}
app.Run(os.Args)
}