-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcurrency.go
More file actions
259 lines (228 loc) · 6.54 KB
/
Copy pathcurrency.go
File metadata and controls
259 lines (228 loc) · 6.54 KB
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
package main
import (
"encoding/json"
"fmt"
"math"
"net/http"
"os"
"path/filepath"
"time"
)
// activeCurrency holds the resolved currency code, symbol, and exchange rate.
// When Rate is 0, costs are displayed in USD.
var activeCurrency struct {
Code string
Symbol string
Rate float64
Suffix bool
}
// CurrencyConfig represents the persistent config in ~/.goccc.json.
type CurrencyConfig struct {
Currency string `json:"currency"`
CachedRate float64 `json:"cached_rate,omitempty"`
RateUpdated string `json:"rate_updated,omitempty"`
WarnThreshold float64 `json:"warn_threshold,omitempty"`
AlertThreshold float64 `json:"alert_threshold,omitempty"`
Statusline *StatuslineConfig `json:"statusline,omitempty"`
}
type currencyInfo struct {
Symbol string
Suffix bool
}
var currencySymbols = map[string]currencyInfo{
"USD": {"$", false},
"EUR": {"€", false},
"GBP": {"£", false},
"JPY": {"¥", false},
"CNY": {"¥", false},
"KRW": {"₩", false},
"INR": {"₹", false},
"RUB": {"₽", false},
"BRL": {"R$", false},
"ZAR": {"R", false},
"CAD": {"CA$", false},
"AUD": {"A$", false},
"CHF": {"CHF", false},
"SEK": {"kr", true},
"NOK": {"kr", true},
"DKK": {"kr", true},
"PLN": {"zł", true},
"TRY": {"₺", false},
"MXN": {"MX$", false},
"NZD": {"NZ$", false},
"HKD": {"HK$", false},
"SGD": {"S$", false},
"TWD": {"NT$", false},
"THB": {"฿", false},
"IDR": {"Rp", false},
"MYR": {"RM", false},
"PHP": {"₱", false},
"VND": {"₫", false},
"CZK": {"Kč", true},
"HUF": {"Ft", true},
"RON": {"lei", true},
"BGN": {"лв", true},
"ISK": {"kr", true},
"UAH": {"₴", false},
"ILS": {"₪", false},
"AED": {"د.إ", true},
"SAR": {"﷼", true},
"EGP": {"E£", false},
"NGN": {"₦", false},
"KES": {"KSh", false},
"ARS": {"AR$", false},
"CLP": {"CL$", false},
"COP": {"CO$", false},
"PEN": {"S/.", false},
"PKR": {"₨", false},
"BDT": {"৳", false},
"LKR": {"Rs", false},
}
func symbolForCurrency(code string) (string, bool) {
if info, ok := currencySymbols[code]; ok {
return info.Symbol, info.Suffix
}
return code, true
}
var configPath = func() string {
home, err := os.UserHomeDir()
if err != nil {
return ""
}
return filepath.Join(home, ".goccc.json")
}
func loadCurrencyConfig(path string) CurrencyConfig {
if path == "" {
return CurrencyConfig{}
}
data, err := os.ReadFile(path)
if err != nil {
return CurrencyConfig{}
}
var cfg CurrencyConfig
if err := json.Unmarshal(data, &cfg); err != nil {
return CurrencyConfig{}
}
return cfg
}
func saveCurrencyConfig(path string, cfg CurrencyConfig) {
if path == "" {
return
}
data, err := json.MarshalIndent(cfg, "", " ")
if err != nil {
return
}
if err := os.WriteFile(path, append(data, '\n'), 0644); err != nil {
fmt.Fprintf(os.Stderr, "goccc: warning: failed to save currency config: %v\n", err)
}
}
type erAPIResponse struct {
Result string `json:"result"`
Rates map[string]float64 `json:"rates"`
}
var httpClient = &http.Client{Timeout: 2 * time.Second}
func fetchExchangeRate(currency string) (float64, error) {
resp, err := httpClient.Get("https://open.er-api.com/v6/latest/USD")
if err != nil {
return 0, fmt.Errorf("fetching exchange rate: %w", err)
}
defer func() { _ = resp.Body.Close() }()
if resp.StatusCode != http.StatusOK {
return 0, fmt.Errorf("exchange rate API returned status %d", resp.StatusCode)
}
var apiResp erAPIResponse
if err := json.NewDecoder(resp.Body).Decode(&apiResp); err != nil {
return 0, fmt.Errorf("decoding exchange rate response: %w", err)
}
rate, ok := apiResp.Rates[currency]
if !ok {
return 0, fmt.Errorf("currency %q not found in exchange rate data", currency)
}
return rate, nil
}
// initConfig loads config from ~/.goccc.json: thresholds and currency.
func initConfig(symbolFlag string, rateFlag float64) error {
cfgPath := configPath()
cfg := loadCurrencyConfig(cfgPath)
customWarn := cfg.WarnThreshold > 0
customAlert := cfg.AlertThreshold > 0
if customWarn {
costThresholdYellow = cfg.WarnThreshold
}
if customAlert {
costThresholdRed = cfg.AlertThreshold
}
if costThresholdYellow > costThresholdRed {
costThresholdYellow, costThresholdRed = costThresholdRed, costThresholdYellow
customWarn, customAlert = customAlert, customWarn
}
if err := initCurrency(symbolFlag, rateFlag, cfgPath, &cfg); err != nil {
return err
}
if activeCurrency.Rate > 0 {
if !customWarn {
costThresholdYellow *= activeCurrency.Rate
}
if !customAlert {
costThresholdRed *= activeCurrency.Rate
}
if costThresholdYellow > costThresholdRed {
costThresholdYellow, costThresholdRed = costThresholdRed, costThresholdYellow
}
}
return nil
}
// initCurrency resolves currency from CLI flags or config file and sets activeCurrency.
func initCurrency(symbolFlag string, rateFlag float64, cfgPath string, cfg *CurrencyConfig) error {
if (symbolFlag != "") != (rateFlag != 0) {
return fmt.Errorf("-currency-symbol and -currency-rate must be used together")
}
if symbolFlag != "" && rateFlag != 0 {
if math.IsNaN(rateFlag) || math.IsInf(rateFlag, 0) || rateFlag <= 0 {
return fmt.Errorf("-currency-rate must be a positive number")
}
activeCurrency.Symbol = symbolFlag
activeCurrency.Rate = rateFlag
return nil
}
if cfg.Currency != "" {
sym, suffix, rate := resolveCurrencyRate(cfg, cfgPath)
activeCurrency.Code = cfg.Currency
activeCurrency.Symbol = sym
activeCurrency.Suffix = suffix
activeCurrency.Rate = rate
}
return nil
}
const rateStaleDuration = 24 * time.Hour
func resolveCurrencyRate(cfg *CurrencyConfig, cfgPath string) (symbol string, suffix bool, rate float64) {
if cfg.Currency == "" || cfg.Currency == "USD" {
return "$", false, 0
}
symbol, suffix = symbolForCurrency(cfg.Currency)
// Check if cached rate is fresh enough
if cfg.CachedRate > 0 && cfg.RateUpdated != "" {
if updated, err := time.Parse(time.RFC3339, cfg.RateUpdated); err == nil {
if time.Since(updated) < rateStaleDuration {
return symbol, suffix, cfg.CachedRate
}
}
}
// Fetch fresh rate
newRate, err := fetchExchangeRate(cfg.Currency)
if err != nil {
fmt.Fprintf(os.Stderr, "goccc: warning: %v\n", err)
// Fall back to cached rate if available
if cfg.CachedRate > 0 {
return symbol, suffix, cfg.CachedRate
}
fmt.Fprintf(os.Stderr, "goccc: warning: no cached rate available, displaying in USD\n")
return "$", false, 0
}
// Save updated rate
cfg.CachedRate = newRate
cfg.RateUpdated = time.Now().UTC().Format(time.RFC3339)
saveCurrencyConfig(cfgPath, *cfg)
return symbol, suffix, newRate
}