-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
95 lines (80 loc) · 2.15 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
package main
import (
"flag"
"fmt"
"github.com/codyja/alkatronic/api"
"log"
"os"
"sync"
)
type DbClient struct {
Host string
Port string
Name string
Username string
Password string
}
func NewDbClient(host, port, name, username, password string) *DbClient {
return &DbClient{
Host: host,
Port: port,
Name: name,
Username: username,
Password: password,
}
}
const (
usage = `usage: %s
Alkatronic CLI Logger
This application can login to the Focustronic website with your credentials and pull down the specified days worth of
records. This can be useful to initially populate a Postgres database. It can also run in daemon mode to poll the
Focustronic site every 30 minutes for new data and log to a Postgres DB.
Environment Variables:
ALKATRONIC_USERNAME="user_here"
ALKATRONIC_PASSWORD="pass_here"
DB_CONNECTION_STRING="postgresql://db_user:db_pass@server:5432/pg_db"
Options:
`
)
func main() {
// read flags
flagDaemon := flag.Bool("d", false, "Run in Daemon mode to keep polling for new Alkatronic data")
flagDays := flag.Int("days", 7, "Number of days worth of records to retrieve. Valid days: 7,30, or 90")
flag.Usage = func() {
fmt.Fprintf(flag.CommandLine.Output(), usage, os.Args[0])
flag.PrintDefaults()
}
flag.Parse()
username, ok := os.LookupEnv("ALKATRONIC_USERNAME")
if !ok {
log.Fatalf("ALKATRONIC_USERNAME not set")
}
password, ok := os.LookupEnv("ALKATRONIC_PASSWORD")
if !ok {
log.Fatalf("ALKATRONIC_PASSWORD not set")
}
dbConn, ok := os.LookupEnv("DB_CONNECTION_STRING")
if !ok {
log.Fatalf("DB_CONNECTION_STRING not set")
}
// Initialize new Alkatronic client
c, err := api.NewAlkatronicClient()
if err != nil {
log.Fatalf("error initializing new Alkatronic Client: %s", err)
}
// Initialize new Postgresql client
pg, err := NewPostgresAlkatronic(dbConn)
if err != nil {
log.Fatalf("error initializing new Postgresql Client: %s", err )
}
// Create wait group so that alkatronic authentication completes before requesting data
var wg sync.WaitGroup
wg.Add(1)
go alkAuth(c, username, password, &wg)
wg.Wait()
if *flagDaemon {
alkLoop(c, pg)
} else {
GetAllAlkData(c, pg, *flagDays)
}
}