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
99 lines (85 loc) · 2.29 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
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"
)
func main() {
// init logger
log, err := zap.NewDevelopment()
if err != nil {
panic(errors.Wrap(err, "failed to create logger"))
}
defer log.Sync()
zap.ReplaceGlobals(log)
// parse config
var config struct {
Nordigen *nordigen.Config `envconfig:"NORDIGEN" required:"true"`
LunchmoneyAccessToken string `envconfig:"LUNCHMONEY_ACCESS_TOKEN" required:"true"`
Mapping map[string]int `envconfig:"MAPPING"` // map[nordigenAccountID]lunchmoneyAssetID
}
err = envconfig.Process("", &config)
if err != nil {
log.Fatal("failed to process config", zap.Error(err))
}
// 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.Mapping) == 0 {
log.Info("no mapping found, printing accounts")
accounts, err := lunchmoneyClient.GetAssets(ctx)
if err != nil {
log.Fatal("failed to fetch accounts from lunchmoney", zap.Error(err))
}
for _, account := range accounts {
log.Info("lunchmoney account",
zap.Int("id", account.ID),
zap.String("name", account.Name),
zap.String("institution_name", account.InstitutionName),
zap.String("type", account.TypeName),
zap.String("subtype", account.SubtypeName),
zap.Float64("balance", float64(account.Balance)),
zap.String("currency", account.Currency),
)
}
return
}
for nordigenAccountID, lunchmoneyAssetID := range config.Mapping {
err = syncAccount(
ctx,
nordigenAccountID,
lunchmoneyAssetID,
nordigenClient,
lunchmoneyClient,
log,
)
if err != nil {
log.Fatal("failure syncing account",
zap.String("nordigen_account_id", nordigenAccountID),
zap.Int("lunchmoney_asset_id", lunchmoneyAssetID),
zap.Error(err),
)
}
}
}