-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
94 lines (74 loc) · 1.95 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
package main
import (
"bufio"
"encoding/json"
"fmt"
"humn/coordinate_mapping"
"humn/workerpool"
"log"
"os"
"strconv"
)
//var Writer *bufio.Writer
var allTask []*workerpool.Task
func main() {
accessToken, poolSize := GetCommandLineArgs()
outputChannel := make(chan coordinate_mapping.CoordinatePostcodeOutput)
go func() {
for o := range outputChannel {
writeOutput(o)
}
}()
pool := workerpool.NewPool(allTask, poolSize)
go func() {
coordinateMapper := coordinate_mapping.NewCoordinateMapper(accessToken, outputChannel)
scanInput(coordinateMapper, pool)
}()
pool.RunBackground()
}
func scanInput(coordinateMapper coordinate_mapping.CoordinateMapper, pool *workerpool.Pool) {
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
var coordinate coordinate_mapping.Coordinate
err := json.Unmarshal(scanner.Bytes(), &coordinate)
if err != nil {
log.Fatal(err)
}
task := workerpool.NewTask(func(data interface{}) error {
err = coordinateMapper.GetPostcodeDataForCoordinatesAndWriteToOutput(coordinate)
return err
}, nil)
pool.AddTask(task)
}
pool.Stop()
}
func writeOutput(output coordinate_mapping.CoordinatePostcodeOutput) {
outputJsonBytes, err := json.Marshal(output)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(string(outputJsonBytes))
}
func GetCommandLineArgs() (string, int) {
args := os.Args
if len(args) < 2 {
log.Fatal("Error: Please pass in 2 command line args. token and pool count")
}
accessToken := os.Args[1]
if accessToken == "" {
log.Fatal("Error: Please pass in command line arg 1 with your map token")
}
poolSizeString := os.Args[2]
poolSize, err := strconv.Atoi(poolSizeString)
if err != nil {
poolSize = 5
log.Println("Error parsing command line arg 2 'pool size'", err)
log.Println("Setting pool size to default value", poolSize)
}
if poolSize == 0 {
poolSize = 5
log.Println("Setting pool size to default value", poolSize)
}
return accessToken, poolSize
}