/
register.go
177 lines (162 loc) · 5.38 KB
/
register.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
177
// Copyright 2021 Silvio Böhler
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package register
import (
"bufio"
"fmt"
"log"
"os"
"runtime/pprof"
"github.com/sboehler/knut/cmd/flags"
"github.com/sboehler/knut/lib/common/date"
"github.com/sboehler/knut/lib/common/filter"
"github.com/sboehler/knut/lib/common/mapper"
"github.com/sboehler/knut/lib/common/table"
"github.com/sboehler/knut/lib/journal"
"github.com/sboehler/knut/lib/journal/register"
"github.com/spf13/cobra"
)
// CreateCmd creates the command.
func CreateCmd() *cobra.Command {
var r runner
// Cmd is the balance command.
c := &cobra.Command{
Use: "register",
Short: "create a register sheet",
Long: `Compute a register report.`,
Args: cobra.ExactValidArgs(1),
Run: r.run,
Hidden: true,
}
r.setupFlags(c)
return c
}
type runner struct {
// internal
cpuprofile string
// transformations
period flags.PeriodFlag
last int
interval flags.IntervalFlags
showCommodities bool
showSource bool
showDescriptions bool
mapping flags.MappingFlag
remap flags.RegexFlag
valuation flags.CommodityFlag
accounts, others, commodities flags.RegexFlag
// formatting
thousands, color bool
sortAlphabetically bool
digits int32
}
func (r *runner) run(cmd *cobra.Command, args []string) {
if r.cpuprofile != "" {
f, err := os.Create(r.cpuprofile)
if err != nil {
log.Fatal(err)
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
if err := r.execute(cmd, args); err != nil {
fmt.Fprintf(cmd.ErrOrStderr(), "%+v\n", err)
os.Exit(1)
}
}
func (r *runner) setupFlags(c *cobra.Command) {
c.Flags().StringVar(&r.cpuprofile, "cpuprofile", "", "file to write profile")
r.period.Setup(c, date.Period{End: date.Today()})
c.Flags().IntVar(&r.last, "last", 0, "last n periods")
r.interval.Setup(c, date.Daily)
c.Flags().BoolVarP(&r.sortAlphabetically, "sort", "s", false, "Sort accounts alphabetically")
c.Flags().BoolVarP(&r.showCommodities, "show-commodities", "c", false, "Show commodities")
c.Flags().BoolVarP(&r.showDescriptions, "show-descriptions", "d", false, "Show descriptions")
c.Flags().BoolVarP(&r.showSource, "show-source", "a", false, "Show the source accounts")
c.Flags().VarP(&r.valuation, "val", "v", "valuate in the given commodity")
c.Flags().VarP(&r.mapping, "map", "m", "<level>,<regex>")
c.Flags().VarP(&r.remap, "remap", "r", "<regex>")
c.Flags().Var(&r.accounts, "source", "filter source accounts with a regex")
c.Flags().Var(&r.others, "dest", "filter dest accounts with a regex")
c.Flags().Var(&r.commodities, "commodity", "filter commodities with a regex")
c.Flags().Int32Var(&r.digits, "digits", 0, "round to number of digits")
c.Flags().BoolVarP(&r.thousands, "thousands", "k", false, "show numbers in units of 1000")
c.Flags().BoolVar(&r.color, "color", true, "print output in color")
}
func (r runner) execute(cmd *cobra.Command, args []string) error {
var (
ctx = cmd.Context()
jctx = journal.NewContext()
valuation *journal.Commodity
err error
)
if valuation, err = r.valuation.Value(jctx); err != nil {
return err
}
r.showCommodities = r.showCommodities || valuation == nil
j, err := journal.FromPath(ctx, jctx, args[0])
if err != nil {
return err
}
var am mapper.Mapper[*journal.Account]
if r.showSource {
am = journal.RemapAccount(jctx, r.remap.Regex())
}
period := r.period.Value().Clip(j.Period())
var (
dates = period.Dates(r.interval.Value(), r.last)
f = filter.And(
journal.FilterDates(period.Contains),
journal.FilterAccount(r.accounts.Regex()),
journal.FilterOther(r.others.Regex()),
journal.FilterCommodity(r.commodities.Regex()),
)
m = journal.KeyMapper{
Date: date.Align(dates),
Account: am,
Other: mapper.Combine(
journal.RemapAccount(jctx, r.remap.Regex()),
journal.ShortenAccount(jctx, r.mapping.Value()),
),
Commodity: journal.MapCommodity(r.showCommodities),
Valuation: journal.MapCommodity(valuation != nil),
Description: mapper.If[string](r.showDescriptions),
}.Build()
rep = register.NewReport(jctx)
processors = []journal.DayFn{
journal.ComputePrices(valuation),
journal.Balance(jctx, valuation),
journal.Query(f, m, valuation, rep),
}
)
if _, err := j.Process(processors...); err != nil {
return err
}
var (
reportRenderer = register.Renderer{
ShowCommodities: r.showCommodities,
ShowDescriptions: r.showDescriptions,
ShowSource: r.showSource,
SortAlphabetically: r.sortAlphabetically,
}
tableRenderer = table.TextRenderer{
Color: r.color,
Thousands: r.thousands,
Round: r.digits,
}
out = bufio.NewWriter(cmd.OutOrStdout())
)
defer out.Flush()
return tableRenderer.Render(reportRenderer.Render(rep), out)
}