-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
105 lines (79 loc) · 2.33 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
package main
import (
"flag"
"fmt"
"log"
"os"
"github.com/blakeyc/oxr-cli/jobs"
"github.com/blakeyc/oxr-cli/utils"
)
// VERSION of application
const VERSION = "1.2.0"
var (
showVersion = flag.Bool("version", false, "print application version")
output = flag.String("output", "", "destination of the output file")
job = flag.String("job", "", "task type to perform (latest|historical)")
appID = flag.String("app_id", "", "your open exchange rates app_id")
baseCurrency = flag.String("base", "USD", "which currency to use as the base, defaults to USD")
dates = flag.String("dates", "", "the dates to get historical rates for (YYYY-MM-DD)")
start = flag.String("start", "", "start date for time series request")
end = flag.String("end", "", "end date for time series request")
fields = flag.String("fields", "base,currency,rate,timestamp,date", "pick which fields to include in output (base,currency,rate,timestamp,date)")
)
func main() {
var err error
flag.Usage = usage
flag.Parse()
if *showVersion {
version()
return
}
// Check the required params have been supplied
if *appID == "" {
log.Fatal("error: missing --app_id")
}
if *job == "" {
log.Fatal("error: missing --job")
}
if *output == "" {
log.Fatal("error: missing --output")
}
// Compile array of fields to be used for output
fields, errFields := utils.ExtractFields(*fields)
if errFields != nil {
log.Fatal(errFields)
}
// Execute the correct job from the supplied param
switch *job {
case "latest":
err = jobs.GetLatest(*appID, *baseCurrency, *output, fields)
case "historical":
if *dates == "" {
log.Fatal("error: missing --dates")
}
dates, errDates := utils.ExtractDates(*dates)
if errDates != nil {
log.Fatal(errDates)
}
err = jobs.GetHistorical(*appID, *baseCurrency, *output, fields, dates)
case "timeseries":
if *start == "" || *end == "" {
log.Fatal("error: missing --start || --end")
}
err = jobs.GetTimeSeries(*appID, *baseCurrency, *output, fields, *start, *end)
default:
log.Fatal("error: unrecognized job type, should be on of (latest|historical)")
}
if err != nil {
log.Fatal(err)
}
}
// print usage
func usage() {
fmt.Fprint(os.Stderr, "usage: oxr [flags]\n")
flag.PrintDefaults()
}
// print version
func version() {
fmt.Printf("v%s\n", VERSION)
}