-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
83 lines (74 loc) · 1.46 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
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"math"
"sync"
"time"
pf "github.com/ipopov/pricefetch/lib"
)
type Config struct {
V pf.VanguardFetcher
P pf.PolygonFetcher
}
func longestName(xs []pf.Security) int {
ret := 0
for _, x := range xs {
if len(x.Name) > ret {
ret = len(x.Name)
}
}
return ret
}
func longestPrice(xs []pf.Security) int {
ret := 0
for _, x := range xs {
// 4 is for the dollar sign, dot, cents.
digits := 4 + 1 + int(math.Log10(x.Price))
if digits > ret {
ret = digits
}
}
return ret
}
func main() {
var configFlag = flag.String("config", "", "")
flag.Parse()
var config Config
config_serialized, err := ioutil.ReadFile(*configFlag)
if err != nil {
log.Panic(err)
}
err = json.Unmarshal(config_serialized, &config)
if err != nil {
log.Panic(err)
}
results := make([][]pf.Security, 2)
var wg sync.WaitGroup
wg.Add(2)
get :=
func(out_index int, s pf.SecurityFetcher) {
prices, err := s.Run()
if err != nil {
log.Panic(err)
}
results[out_index] = prices
wg.Done()
}
go get(0, config.V)
go get(1, config.P)
wg.Wait()
out := []pf.Security{}
for _, r := range results {
out = append(out, r...)
}
namePadWidth := longestName(out)
pricePadWidth := longestPrice(out)
for _, x := range out {
currencyFmt := fmt.Sprintf("$%.02f", x.Price)
fmt.Printf("P %s %-*s %*s\n", time.Now().Format("2006/01/02"), namePadWidth, x.Name, pricePadWidth, currencyFmt)
}
}