This repository has been archived by the owner on Feb 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
115 lines (99 loc) · 2.77 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
package main
import (
"context"
"net/http"
"time"
"github.com/Seklfreak/nordigen-lunchmoney-sync/lunchmoney"
"github.com/Seklfreak/nordigen-lunchmoney-sync/nordigen"
"github.com/kelseyhightower/envconfig"
"github.com/pkg/errors"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
func main() {
// parse config
var config struct {
Nordigen *nordigen.Config `envconfig:"NORDIGEN" required:"true"`
NordigenRequisitionIDs []string `envconfig:"NORDIGEN_REQUISITION_IDS"`
LunchmoneyAccessToken string `envconfig:"LUNCHMONEY_ACCESS_TOKEN" required:"true"`
TransactionsMap map[string]int `envconfig:"TRANSACTIONS_MAP"` // map[nordigenAccountID]lunchmoneyAssetID
BalancesMap map[string]int `envconfig:"BALANCES_MAP"` // map[nordigenAccountID]lunchmoneyAssetID
Debug bool `envconfig:"DEBUG"`
}
err := envconfig.Process("", &config)
if err != nil {
panic(errors.Wrap(err, "failed to process config"))
}
// init logger
logOpts := make([]zap.Option, 0)
if !config.Debug {
logOpts = append(logOpts, zap.IncreaseLevel(zapcore.InfoLevel))
}
log, err := zap.NewDevelopment(logOpts...)
if err != nil {
panic(errors.Wrap(err, "failed to create logger"))
}
defer log.Sync()
zap.ReplaceGlobals(log)
// create Nordigen client
nordigenClient, err := nordigen.NewClient(
config.Nordigen,
&http.Client{
Timeout: 60 * time.Second,
},
)
if err != nil {
log.Fatal("failed to create nordigen client", zap.Error(err))
}
// create Lunchmoney client
lunchmoneyClient := lunchmoney.NewClient(
config.LunchmoneyAccessToken,
&http.Client{
Timeout: 60 * time.Second,
},
)
ctx := context.Background()
// print accounts if there is no mapping
if len(config.TransactionsMap) == 0 && len(config.BalancesMap) == 0 {
log.Info("no mapping found, printing accounts")
err = printAccounts(ctx, config.NordigenRequisitionIDs, nordigenClient, lunchmoneyClient, log)
if err != nil {
log.Fatal("failed to print accounts", zap.Error(err))
}
return
}
for nordigenAccountID, lunchmoneyAssetID := range config.TransactionsMap {
err = syncAccount(
ctx,
nordigenAccountID,
lunchmoneyAssetID,
nordigenClient,
lunchmoneyClient,
log,
)
if err != nil {
log.Fatal("failure syncing transactions",
zap.String("nordigen_account_id", nordigenAccountID),
zap.Int("lunchmoney_asset_id", lunchmoneyAssetID),
zap.Error(err),
)
}
}
for nordigenAccountID, lunchmoneyAssetID := range config.BalancesMap {
err = syncBalance(
ctx,
nordigenAccountID,
lunchmoneyAssetID,
nordigenClient,
lunchmoneyClient,
log,
)
if err != nil {
log.Fatal("failure syncing balance",
zap.String("nordigen_account_id", nordigenAccountID),
zap.Int("lunchmoney_asset_id", lunchmoneyAssetID),
zap.Error(err),
)
}
}
}