-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
60 lines (47 loc) · 1.51 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
package main
import (
"fmt"
"os"
"strconv"
"strings"
"github.com/Rayrsn/currencyConverter/pkg/currency"
"github.com/Rayrsn/currencyConverter/pkg/utils"
)
var Version = "1.1.0"
func main() {
if err := run(); err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
}
func run() error {
if len(os.Args) == 1 {
return fmt.Errorf("no arguments passed")
}
if len(os.Args) == 2 && (os.Args[1] == "-v" || os.Args[1] == "--version") {
fmt.Println("Version:", Version)
return nil
}
converter := currency.NewConverter()
firstCurrency := os.Args[1]
secondCurrency := os.Args[2]
switch {
case len(os.Args) == 3 && utils.IsCurrency(strings.ToUpper(firstCurrency)) && utils.IsCurrency(strings.ToUpper(secondCurrency)):
rate, err := converter.Convert(1.0, firstCurrency, secondCurrency)
if err != nil {
return err
}
fmt.Printf("1 %s = %f %s\n", strings.ToUpper(firstCurrency), rate, strings.ToUpper(secondCurrency))
case len(os.Args) == 4 && utils.IsNumber(firstCurrency):
thirdCurrency := os.Args[3]
amount, _ := strconv.ParseFloat(firstCurrency, 64)
rate, err := converter.Convert(amount, secondCurrency, thirdCurrency)
if err != nil {
return err
}
fmt.Printf("%g %s = %f %s\n", amount, secondCurrency, rate, strings.ToUpper(thirdCurrency))
default:
return fmt.Errorf("invalid arguments")
}
return nil
}