/
main.go
120 lines (101 loc) · 2.43 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// this file is used to retrieve the version of the ISO country codes from
// the datasource named id rawFile.
// the iso.go file is then generated.
package main
import (
"bytes"
"encoding/json"
"go/format"
"io/ioutil"
"log"
"net/http"
"os"
"text/template"
"time"
)
const rawFile = "https://datahub.io/core/country-codes/r/country-codes.json"
const textFile = "latest_iso.txt"
type ISOSource []struct {
Name string `json:"CLDR display name"`
CodeISO3166Alpha2 string `json:"ISO3166-1-Alpha-2"`
CodeISO3166Alpha3 string `json:"ISO3166-1-Alpha-3"`
}
var client = &http.Client{
Timeout: 5 * time.Second,
}
var tpl = `
// Code generated by "make build"; DO NOT EDIT.
package iso3166
type ISO31661Alpha2 string
func (i ISO31661Alpha2) String() string {
return string(i)
}
const (
{{range $k, $v := . -}}
// {{$v.Name }}
{{ $v.CodeISO3166Alpha2 }} ISO31661Alpha2 = "{{$v.CodeISO3166Alpha2 }}"
{{end -}}
)
var ValidAlpha2Codes = []ISO31661Alpha2{
{{range $k, $v := .}}
{{- $v.CodeISO3166Alpha2 -}},
{{end -}}
}
type ISO31661Alpha3 string
func (i ISO31661Alpha3) String() string {
return string(i)
}
const (
{{range $k, $v := . -}}
// {{$v.Name }}
{{ $v.CodeISO3166Alpha3 }} ISO31661Alpha3 = "{{$v.CodeISO3166Alpha3 }}"
{{end -}}
)
var ValidAlpha3Codes = []ISO31661Alpha3{
{{range $k, $v := .}}
{{- $v.CodeISO3166Alpha3 -}},
{{end -}}
}
`
func main() {
// request the raw file
resp, err := client.Get(rawFile)
if err != nil {
log.Printf("error connecting to datahub: %v", err)
}
// read it
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Printf("error reading response body: %v", err)
}
defer resp.Body.Close()
var source ISOSource
if err := json.Unmarshal(b, &source); err != nil {
log.Fatalf("cannot unmarshal json: %v", err)
}
for i, s := range source {
if s.Name == "" {
source = append(source[:i], source[i+1:]...)
}
}
// execute the template into a buffer
var buf bytes.Buffer
t := template.Must(template.New("iso").Parse(tpl))
if err := t.Execute(&buf, source); err != nil {
log.Fatal(err)
}
// format the new source code
byt, err := format.Source(buf.Bytes())
if err != nil {
log.Printf("error formatting source: %v", err)
}
// create the outfile
outfile, err := os.Create("../iso.go")
if err != nil {
log.Printf("error opening outfile: %v", err)
}
// write it to disk
if _, err := outfile.Write(byt); err != nil {
log.Printf("error writing to disk: %v", err)
}
}