Repository navigation
Expand file tree
/
Copy pathmain.go
More file actions
143 lines (125 loc) · 2.92 KB
/
Copy pathmain.go
File metadata and controls
143 lines (125 loc) · 2.92 KB
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
package main
import (
"errors"
"fmt"
"os"
"runtime"
"sync"
)
// version is injected at build time via -ldflags "-X main.version=...".
var version = "dev"
// dataDir holds the downloaded source CSV and the generated artifacts.
const dataDir = "data"
func usage() {
fmt.Fprintf(os.Stderr, `ipinfo-lite %s — lightweight IPinfo CSV database pipeline
Usage:
ipinfo-lite <command>
Commands:
update Download and verify the official IPinfo Lite CSV and MMDB (requires IPINFO_TOKEN)
process Reduce the raw CSV to the lite schema
release Compress the output to .gz, .xz and .zst
mmdb Validate and publish the official IPinfo Lite MMDB unchanged
xdb Convert the output to ip2region xdb format (.ipv4.xdb / .ipv6.xdb)
checksum Write checksums.txt with SHA-256 of all release artifacts
generate Build all artifacts from existing source data (no download)
all Run all of the above in sequence
`, version)
}
func main() {
if err := loadDotenv(".env"); err != nil {
fmt.Fprintf(os.Stderr, "warning: could not load .env: %v\n", err)
}
if len(os.Args) < 2 {
usage()
os.Exit(2)
}
var err error
switch os.Args[1] {
case "update":
err = runUpdate()
case "process":
err = runProcess()
case "release":
err = runRelease()
case "mmdb":
err = runMmdb()
case "xdb":
err = runXdb()
case "checksum":
err = runChecksum()
case "generate":
err = runGenerate()
case "all":
err = runAll()
case "-h", "--help", "help":
usage()
return
case "-v", "--version", "version":
fmt.Println(version)
return
default:
fmt.Fprintf(os.Stderr, "unknown command: %s\n", os.Args[1])
usage()
os.Exit(2)
}
if err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
}
func runAll() error {
if err := runUpdate(); err != nil {
return err
}
return runGenerate()
}
func runGenerate() error {
if err := runArtifactPipeline(); err != nil {
return err
}
return runChecksum()
}
// runArtifactPipeline shares process's ordered output batches with the XDB
// builder. The independent official MMDB publish runs concurrently, and
// compression starts as soon as the final CSV is closed.
func runArtifactPipeline() error {
bufferSize := max(2, runtime.NumCPU())
xdbRows := make(chan [][]string, bufferSize)
errs := make(chan error, 3)
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
if err := runMmdb(); err != nil {
errs <- err
}
}()
wg.Add(1)
go func() {
defer wg.Done()
if err := writeXdbBatches(xdbRows); err != nil {
errs <- err
}
}()
processErr := runProcessWithConsumer(func(rows [][]string) error {
xdbRows <- rows
return nil
})
close(xdbRows)
if processErr == nil {
wg.Add(1)
go func() {
defer wg.Done()
if err := runRelease(); err != nil {
errs <- err
}
}()
}
wg.Wait()
close(errs)
allErrors := []error{processErr}
for err := range errs {
allErrors = append(allErrors, err)
}
return errors.Join(allErrors...)
}