-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
88 lines (72 loc) · 2.04 KB
/
Copy pathmain.go
File metadata and controls
88 lines (72 loc) · 2.04 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
package main
import (
"fmt"
"log"
"os"
"strconv"
"github.com/1deyce/currency-converter/converter"
"github.com/1deyce/currency-converter/rates"
"github.com/charmbracelet/huh"
"github.com/joho/godotenv"
)
func main() {
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
appID := os.Getenv("OPENEXCHANGE_APP_ID")
if appID == "" {
fmt.Println("OPENEXCHANGE_APP_ID not set, please set it in your environment variables")
return
}
apiURL := fmt.Sprintf("https://openexchangerates.org/api/latest.json?app_id=%s&symbols=USD,GBP,EUR,JPY,ZAR", appID)
rates, err := rates.FetchRates(apiURL)
if err != nil {
fmt.Printf("Error fetching exchange rates: %v\n", err)
return
}
var from, to, amountStr string
form := huh.NewForm(
huh.NewGroup(
huh.NewSelect[string]().
Title("Please select the currency you wish to convert FROM").
Options(
huh.NewOption("United States Dollar", "USD"),
huh.NewOption("Great Britain Pound", "GBP"),
huh.NewOption("Euro", "EUR"),
huh.NewOption("Japanese Yen", "JPY"),
huh.NewOption("South African Rand", "ZAR"),
).
Value(&from),
huh.NewInput().
Title("Please enter the amount you wish to convert").
Prompt("? ").
Value(&amountStr),
huh.NewSelect[string]().
Title("Please select the currency you wish to convert TO").
Options(
huh.NewOption("United States Dollar", "USD"),
huh.NewOption("Great Britain Pound", "GBP"),
huh.NewOption("Euro", "EUR"),
huh.NewOption("Japanese Yen", "JPY"),
huh.NewOption("South African Rand", "ZAR"),
).
Value(&to),
),
)
err = form.Run()
if err != nil {
log.Fatal(err)
}
amount, err := strconv.ParseFloat(amountStr, 64)
if err != nil {
fmt.Printf("Error parsing amount: %v\n", err)
return
}
converted, err := converter.Convert(from, to, amount, rates)
if err != nil {
fmt.Printf("Error converting currency: %v\n", err)
return
}
fmt.Printf("Converted amount: %.2f %s\n", converted, to)
}