-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
119 lines (101 loc) · 2.54 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
package main
import (
"flag"
"fmt"
htmltemplate "html/template"
"io"
"log"
"math"
"os"
"path/filepath"
"strings"
"text/template"
"time"
"github.com/Masterminds/sprig/v3"
"github.com/jordic/goics"
"github.com/pkg/errors"
)
func printDebug(out io.Writer, evs Events) error {
for _, ev := range evs {
fmt.Fprintf(out, "%+v\n", ev)
}
return nil
}
func debug(format string, a ...interface{}) {
if opts.debug {
fmt.Fprintf(os.Stderr, format, a...)
}
}
var opts struct {
debug bool
year int
month int
chcat string
}
func main() {
var err error
flag.BoolVar(&opts.debug, "d", false, "print debug info")
flag.IntVar(&opts.year, "y", math.MinInt64, "limit output to year")
flag.IntVar(&opts.month, "m", math.MinInt64, "limit output to month")
flag.StringVar(&opts.chcat, "c", "", "append (+), remove (-) or limit (=) categories \"+cat1,-cat2,=cat3...\"")
flag.Parse()
d := goics.NewDecoder(os.Stdin)
var evs Events
err = d.Decode(&evs)
if err != nil {
log.Fatalf("%#v\n", err)
}
if opts.year != math.MinInt64 {
debug("Filtering year %d\n", opts.year)
start := time.Date(opts.year, time.January, 1, 0, 0, 0, 0, time.Local)
evs = evs.ExpandBetween(start, start.AddDate(1, 0, 0))
}
if opts.month != math.MinInt64 {
debug("Filtering month %d\n", opts.month)
evs = evs.FilterTime(func(t time.Time) int { return int(time.Time.Month(t)) }, opts.month)
}
evs = evs.Sort()
var limitCategories []string
for i, catopt := range splitText(opts.chcat) {
if len(catopt) <= 1 {
debug("Ignoring empty category change #%d\n", i)
continue
}
opt, cat := catopt[0], catopt[1:]
var optf strSliceBoolOp
switch opt {
case '+':
optf = strUnion
case '-':
optf = strSub
case '=':
limitCategories = append(limitCategories, cat)
continue
default:
debug("Unknown operation %q\n", opt)
}
evs.OpCategories(optf, cat)
}
evs.OpCategories(strCut, limitCategories...)
if opts.debug {
PrinterFunc(printDebug).Print(os.Stderr, evs)
}
for _, tmpl := range flag.Args() {
var t interface {
Execute(io.Writer, any) error
}
var err error
if strings.HasPrefix(strings.ToLower(filepath.Ext(tmpl)), ".html") {
t, err = htmltemplate.New(filepath.Base(tmpl)).Funcs(sprig.FuncMap()).ParseFiles(tmpl)
} else {
t, err = template.New(filepath.Base(tmpl)).Funcs(sprig.FuncMap()).ParseFiles(tmpl)
}
if err != nil {
log.Fatalf("%+v", errors.Wrapf(err, "Error parsing %q", tmpl))
}
err = t.Execute(os.Stdout, evs)
if err != nil {
log.Fatalf("%+v", errors.Wrapf(err, "Error executing %q", tmpl))
}
}
}