-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
205 lines (180 loc) · 4.94 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
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package main
import (
"fmt"
"os"
"strings"
"sync"
"time"
resty "github.com/go-resty/resty/v2"
"gitlab.com/rbrt-weiler/coinspy/core"
"gitlab.com/rbrt-weiler/coinspy/databases"
"gitlab.com/rbrt-weiler/coinspy/providers"
"gitlab.com/rbrt-weiler/coinspy/types"
)
// listProviders prints all possible providers to stdout and exits the application.
func listProviders() {
cons := &core.Cons
prov, err := core.ProviderList()
if err != nil {
cons.Fprintf(os.Stderr, "Error: %s\n", err)
os.Exit(core.ErrGeneric)
}
for p, mar := range prov {
for _, m := range mar {
if m != "default" {
cons.Printf("%s/%s\n", p, m)
} else {
cons.Printf("%s\n", p)
}
}
}
os.Exit(core.ErrSuccess)
}
// initializeProvider initializes and returns a provider object based on the name of the provider.
func initializeProvider(providerName string, httpClient *resty.Client) (provider providers.Provider, err error) {
switch strings.ToLower(providerName) {
case "bitpanda":
provider = providers.Bitpanda(httpClient)
case "coingate":
provider = providers.CoinGate(httpClient)
case "coingecko":
provider = providers.Coingecko(httpClient)
case "livecoinwatch":
provider = providers.LiveCoinWatch(httpClient)
default:
err = fmt.Errorf("provider %s is unknown", providerName)
}
return
}
// fetchRates fetches all requested exchange rates from all given providers, as per CLI arguments.
func fetchRates() (rates types.ExchangeRates) {
var providerName string
var markets []string
var provider providers.Provider
var err error
var wg sync.WaitGroup
config := &core.Config
cons := &core.Cons
coins := strings.Split(config.Coins, ",")
fiats := strings.Split(config.Fiats, ",")
client := resty.New()
client.SetHeader("User-Agent", core.ToolID)
for _, singleProvider := range strings.Split(config.Providers, ",") {
if strings.Contains(singleProvider, "/") {
parts := strings.Split(singleProvider, "/")
providerName = parts[0]
markets = parts[1:]
} else {
providerName = singleProvider
markets = []string{"default"}
}
provider, err = initializeProvider(providerName, client)
if err != nil {
cons.Fprintf(os.Stderr, "Error: %s\n", err)
os.Exit(core.ErrGeneric)
}
for _, market := range markets {
err = provider.SetMarket(market)
if err != nil {
cons.Fprintf(os.Stderr, "Error: %s\n", err)
os.Exit(core.ErrGeneric)
}
for _, coin := range coins {
for _, fiat := range fiats {
wg.Add(1)
go provider.FetchRateSynced(coin, fiat, &rates, &wg)
}
}
}
}
wg.Wait()
return
}
// ratesToStrings turns the exchange rates into a list of strings, with compactness as defined by CLI arguments.
func ratesToStrings(rates *types.ExchangeRates) (resultSet []string) {
var portfolioValue map[string]float64
config := &core.Config
portfolioValue = make(map[string]float64)
rates.Sort()
for _, rate := range rates.Rates {
if rate.Owned > 0 {
portfolioValue[strings.ToUpper(rate.Fiat)] += rate.Owned * rate.Rate
}
}
if config.PortfolioValueTop {
for fiat, value := range portfolioValue {
resultSet = append(resultSet, fmt.Sprintf("Total portfolio value: %.2f %s", value, fiat))
}
}
for _, rate := range rates.Rates {
if rate.Error == nil {
if config.VeryCompactOutput {
resultSet = append(resultSet, rate.StringVeryCompact())
} else if config.CompactOutput {
resultSet = append(resultSet, rate.StringCompact())
} else {
resultSet = append(resultSet, rate.String())
}
}
}
if config.PortfolioValueBottom {
for fiat, value := range portfolioValue {
resultSet = append(resultSet, fmt.Sprintf("Total portfolio value: %.2f %s", value, fiat))
}
}
return
}
// init is used to initialize the application.
func init() {
core.SetupFlags()
}
// main ties everything together.
func main() {
var rates types.ExchangeRates
var resultSet []string
config := &core.Config
cons := &core.Cons
if config.List.Providers {
listProviders()
}
core.CheckArguments()
rates = fetchRates()
resultSet = ratesToStrings(&rates)
if !config.Quiet {
for _, line := range resultSet {
cons.Println(line)
}
}
if config.DuckDB.Enabled {
db := databases.DuckDB()
dbErr := db.StoreExchangeRates(&rates)
if dbErr != nil {
cons.Fprintf(os.Stderr, "Error: %s\n", dbErr)
os.Exit(core.ErrGeneric)
}
}
if config.QuestDB.Enabled {
db := databases.QuestDB()
dbErr := db.StoreExchangeRates(&rates)
if dbErr != nil {
cons.Fprintf(os.Stderr, "Error: %s\n", dbErr)
os.Exit(core.ErrGeneric)
}
}
if config.SQLite3.Enabled {
db := databases.SQLite3()
dbErr := db.StoreExchangeRates(&rates)
if dbErr != nil {
cons.Fprintf(os.Stderr, "Error: %s\n", dbErr)
os.Exit(core.ErrGeneric)
}
}
if config.Pushover.Enabled {
poErr := core.SendPushoverMessage(config.Pushover.Token, config.Pushover.User, strings.Join(resultSet, core.LineBreak), time.Now())
if poErr != nil {
cons.Fprintf(os.Stderr, "Error: %s\n", poErr)
os.Exit(core.ErrGeneric)
}
}
os.Exit(core.ErrSuccess)
}