/
main.go
151 lines (126 loc) · 5.11 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
package main
import (
"context"
"errors"
"io"
"os"
"strings"
kingpin "github.com/alecthomas/kingpin"
"github.com/josler/wdid/config"
"github.com/josler/wdid/core"
"github.com/josler/wdid/fileedit"
)
const (
VERSION = "1.1.0"
)
var (
app = kingpin.New("wdid", "A tool to track what you did.")
v = app.Flag("verbose", "Enable verbose logging.").Short('v').Bool()
format = app.Flag("format", "format to print in ('human', 'text', or 'json).").Default("human").Enum("human", "text", "json")
bump = app.Command("bump", "Bump an item to a new time, skipping the existing and creating a new one.")
bumpID = bump.Arg("id", "ID of item to bump.").Required().String()
bumpTime = bump.Flag("time", "Time to bump item to the item at.").Short('t').PlaceHolder("TIME").Default("now").String()
add = app.Command("add", "Add a new item to track.")
addTime = add.Flag("time", "Time to add the item at.").Short('t').PlaceHolder("TIME").Default("now").String()
addDone = add.Flag("done", "Mark item as done already").Short('d').Bool()
newThing = add.Arg("new-item", "Description of new item.").String()
do = app.Command("do", "Mark an item as done.")
doID = do.Arg("id", "ID of item to mark done.").Required().String()
edit = app.Command("edit", "Edit an item's time or description.")
editTime = edit.Flag("time", "Time to add the item at.").Short('t').PlaceHolder("TIME").String()
editID = edit.Arg("id", "ID of item to edit.").Required().String()
editDescription = edit.Arg("description", "Description of new item.").String()
group = app.Command("group", "create a group.")
groupName = group.Flag("name", "name of the group").Short('n').Required().String()
groupFilters = group.Flag("filters", "filters for the group").Short('f').Required().String()
groupRm = app.Command("group-rm", "delete a group.")
groupRmName = groupRm.Flag("name", "name of the group").Short('n').Required().String()
groupList = app.Command("group-ls", "List groups.")
importCmd = app.Command("import", "Import items from a file or stdin.")
importFilename = importCmd.Arg("in", "Filename to import from, if omitted, stdin used").String()
list = app.Command("ls", "List the items you're tracking.").Alias("list").Default()
listFilter = list.Flag("filter", "Filter the results").Short('f').String()
listGroup = list.Flag("group", "List items in a group").Short('g').String()
listArg = list.Arg("filters", "Filter your items.").Default("0").String()
rm = app.Command("rm", "Remove (permanently!) a single item.").Alias("delete")
rmID = rm.Arg("id", "ID of item to remove.").Required().String()
skip = app.Command("skip", "Mark an item as skipped.")
skipID = skip.Arg("id", "ID of item to mark skipped.").Required().String()
show = app.Command("show", "Show a single item.")
showID = show.Arg("id", "ID of item to show.").Required().String()
tagList = app.Command("tag-ls", "List tags.")
)
func main() {
conf, err := config.Load()
app.FatalIfError(err, "")
kingpin.CommandLine.HelpFlag.Short('h')
kingpin.EnableFileExpansion = false
app.Version(VERSION)
app.HelpFlag.Short('h') // allow -h for --help
app.UsageTemplate(kingpin.CompactUsageTemplate)
app.Interspersed(true)
commandName := kingpin.MustParse(app.Parse(os.Args[1:]))
store, err := createStore(conf)
app.FatalIfError(err, "")
ctx := context.WithValue(context.Background(), "store", store)
ctx = context.WithValue(ctx, "verbose", *v)
ctx = context.WithValue(ctx, "format", *format)
ctx = context.WithValue(ctx, "config", conf)
switch commandName {
case add.FullCommand():
var description io.Reader
if *newThing != "" {
description = strings.NewReader(*newThing)
} else {
description, err = fileedit.NewWithNoContent()
if err != nil {
break
}
}
if *addDone {
err = core.AddDone(ctx, description, *addTime)
} else {
err = core.Add(ctx, description, *addTime)
}
case bump.FullCommand():
err = core.Bump(ctx, *bumpID, *bumpTime)
case do.FullCommand():
err = core.Do(ctx, *doID)
case edit.FullCommand():
if *editDescription == "" && *editTime == "" {
err = core.EditDataFromFile(ctx, *editID)
} else {
err = core.Edit(ctx, *editID, strings.NewReader(*editDescription), *editTime)
}
case importCmd.FullCommand():
err = core.Import(ctx, *importFilename)
case list.FullCommand():
if *listFilter != "" {
*listArg = *listFilter // temporary override
}
err = core.List(ctx, *listArg, *listGroup)
case rm.FullCommand():
err = core.Rm(ctx, *rmID)
case skip.FullCommand():
err = core.Skip(ctx, *skipID)
case show.FullCommand():
err = core.Show(ctx, *showID)
case tagList.FullCommand():
err = core.ListTag(ctx)
case group.FullCommand():
err = core.CreateGroup(ctx, *groupName, *groupFilters)
case groupRm.FullCommand():
err = core.DeleteGroup(ctx, *groupRmName)
case groupList.FullCommand():
err = core.ListGroup(ctx)
}
app.FatalIfError(err, "")
}
func createStore(conf *config.Config) (core.Store, error) {
switch conf.Store.Type {
case "bolt":
return core.NewBoltStore(conf.Store.Filepath())
default:
return nil, errors.New("store not specified correctly")
}
}