-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgocurrency.go
147 lines (119 loc) · 2.94 KB
/
gocurrency.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
// http://play.golang.org/p/ebVIqodGPd
package main
import (
"encoding/json"
"flag"
"fmt"
"io"
"net/http"
"net/url"
"os"
"sort"
)
const (
API = "http://api.fixer.io/"
)
var (
val string
base string
convert float64
date string
)
func help() {
fmt.Println("GoCurrency is used to quickly check the conversion rates")
flag.PrintDefaults()
}
func exitHelp(code int) {
help()
os.Exit(code)
}
func init() {
flag.Usage = help
flag.StringVar(&base, "base", "USD", "The base currency to quote a currency against (default USD)")
flag.Float64Var(&convert, "convert", 1, "Amount of currency to convert (default 1)")
flag.StringVar(&date, "date", "", "Historical date to show (default today)")
flag.Parse()
if len(base) == 0 {
fmt.Println("Base must be set")
exitHelp(3)
}
if convert < 1 {
fmt.Println("Convert must be 1 or greater")
exitHelp(3)
}
val = flag.Arg(0)
}
func sendRequest() {
v := url.Values{}
if len(base) > 0 {
v.Add("base", base)
}
if len(val) > 0 {
v.Add("symbols", val)
}
params := v.Encode()
if len(date) == 0 {
date = "latest"
}
resp, err := http.Get(API + date + "?" + params)
if err != nil {
fmt.Println("Failed to get data", err)
os.Exit(3)
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
fmt.Println("Not 200 found")
os.Exit(3)
}
handleResponse(resp.Body)
}
func sortDataKeys(m map[string]float64) []string {
var keys []string
for k := range m {
keys = append(keys, k)
}
sort.Strings(keys)
return keys
}
func convertCurrency(r float64, c float64) float64 {
return r * c
}
type DataType struct {
Base string
Date string
Rates map[string]float64
}
func handleResponse(r io.ReadCloser) {
var data DataType
err := json.NewDecoder(r).Decode(&data)
if err != nil {
fmt.Println("Error reading data", err)
os.Exit(3)
}
fmt.Printf("\nBase currency: %s, Date: %s\n\n", data.Base, data.Date)
if convert > 1 {
fmt.Printf("| %-12s|-%-12s|-%-12s |\n", "------------", "------------", "------------")
fmt.Printf("| %-12s| %-12s| %-12s |\n", "Symbol", "Rate", "Converted")
fmt.Printf("| %-12s|-%-12s|-%-12s |\n", "------------", "------------", "------------")
} else {
fmt.Printf("| %-12s|-%-12s |\n", "------------", "------------")
fmt.Printf("| %-12s| %-12s |\n", "Symbol", "Rate")
fmt.Printf("| %-12s|-%-12s |\n", "------------", "------------")
}
keys := sortDataKeys(data.Rates)
for _, k := range keys {
if convert > 1 {
fmt.Printf("| %-12s| %-12.3f| %-12.3f |\n", k, data.Rates[k], convertCurrency(data.Rates[k], convert))
} else {
fmt.Printf("| %-12s| %-12.3f |\n", k, data.Rates[k])
}
}
if convert > 1 {
fmt.Printf("| %-12s|-%-12s|-%-12s |\n", "------------", "------------", "------------")
} else {
fmt.Printf("| %-12s|-%-12s |\n", "------------", "------------")
}
}
func main() {
sendRequest()
}