-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
93 lines (74 loc) · 1.88 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
package main
import (
"database/sql"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
_ "github.com/go-sql-driver/mysql"
"github.com/zerosixtytwo/owidfetch/internal/owid"
"gopkg.in/yaml.v3"
)
const appVersion = 1.25
var config = new(struct {
DBDSN string `yaml:"DB_DSN"`
OWIDDataUrl string `yaml:"OWID_DATA_URL"`
})
func main() {
currentDirectory, err := os.Getwd()
if err != nil {
log.Fatalln(err)
}
var configFilePath = fmt.Sprintf("%s/owidf.conf.yaml", currentDirectory)
var printVersion bool
flag.StringVar(&configFilePath, "c", configFilePath, "Configuration File path.")
flag.BoolVar(&printVersion, "v", false, "Print version and exit.")
flag.Parse()
if printVersion {
fmt.Printf("%.2f\n", appVersion)
os.Exit(0)
}
rawConf, err := ioutil.ReadFile(configFilePath)
if err != nil {
log.Fatalln(err)
}
err = yaml.Unmarshal(rawConf, config)
if err != nil {
log.Fatalln(err)
}
if len(config.DBDSN) == 0 {
log.Fatalln("database dsn must not be null in the configuration file")
}
if len(config.OWIDDataUrl) == 0 {
log.Fatalln("the owid repository must not be null in the configuration file")
}
log.Println("Configured data source:", config.OWIDDataUrl)
log.Print("Configuration is Ok, fetching data from the repository ... ")
results, err := owid.Fetch(config.OWIDDataUrl)
if err != nil {
log.Fatalln(err)
}
log.Printf("Fetched data for %d countries.", len(*results)-9)
log.Println("Connecting to database ... ")
db, err := sql.Open("mysql", config.DBDSN)
if err != nil {
log.Fatalln(err)
}
defer db.Close()
err = db.Ping()
if err != nil {
log.Fatalln(err)
}
log.Println("Database connection succeeded.")
log.Println("Updating tables ... ")
err = updateContinentTables(db, results)
if err != nil {
log.Fatalln(err)
}
log.Println("Inserting fetched results ... ")
err = insertCountryReports(results, db)
if err != nil {
log.Fatalln(err)
}
}