-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
80 lines (67 loc) · 1.6 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
package main
import (
"context"
"flag"
"fmt"
"os"
"os/signal"
"path/filepath"
"time"
"github.com/mxmCherry/taxes/v2/internal/bankgovua"
"github.com/mxmCherry/taxes/v2/internal/formatter"
"github.com/mxmCherry/taxes/v2/internal/httpclient"
"github.com/mxmCherry/taxes/v2/internal/tax"
"gopkg.in/yaml.v3"
)
func main() {
flag.Parse()
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, os.Kill)
defer cancel()
out := formatter.Table(os.Stdout)
defer out.Close()
rates := &bankgovua.Client{
HTTP: httpclient.New(150*time.Millisecond, 3),
}
filenames := flag.Args()
if len(filenames) == 0 {
filenames = []string{
filepath.Join(os.Getenv("HOME"), ".taxes/data.yaml"),
}
}
for _, filename := range filenames {
if err := process(ctx, out, rates, filename); err != nil {
fmt.Fprintf(os.Stderr, "ERROR: %q: %s\n", filename, err)
return
}
select {
case <-ctx.Done():
return
default:
}
}
}
func process(ctx context.Context, out formatter.Formatter, rates tax.CurrencyRates, filename string) error {
f, err := os.Open(filename)
if err != nil {
return fmt.Errorf("open %q: %w", filename, err)
}
defer f.Close()
var data struct {
Business *tax.Business `yaml:"business"`
Transactions tax.TransactionSlice `yaml:"transactions"`
}
if err := yaml.NewDecoder(f).Decode(&data); err != nil {
return fmt.Errorf("decode yaml: %w", err)
}
calc, err := tax.NewCalc(
data.Business,
data.Transactions,
rates,
)
if err != nil {
return err
}
return calc.Each(ctx, func(q *tax.Quarter) error {
return out.Format(data.Business, q)
})
}