/
main.go
115 lines (103 loc) · 2.28 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
package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"sync"
"time"
"github.com/BurntSushi/toml"
"github.com/groob/vquery/axiom"
)
var (
Version = "unreleased"
fVersion = flag.Bool("version", false, "display the version")
fConfig = flag.String("config", "", "configuration file to load")
conf config
client *axiom.Client
wg sync.WaitGroup
)
type report struct {
Name string `toml:"name"`
ID int `toml:"id"`
}
type config struct {
Interval time.Duration `toml:"interval"`
VeracrossUsername string `toml:"veracross_username"`
VeracrossPassword string `toml:"veracross_password"`
VeracrossSchool string `toml:"veracross_school"`
Reports []report `toml:"reports"`
ReportsPath string `toml:"reports_path"`
}
func init() {
flag.Parse()
if *fVersion {
fmt.Printf("axiom-query - version %s\n", Version)
os.Exit(0)
}
if _, err := toml.DecodeFile(*fConfig, &conf); err != nil {
log.Fatal(err)
}
c, err := axiom.NewClient(conf.VeracrossUsername, conf.VeracrossPassword,
conf.VeracrossSchool,
)
if err != nil {
log.Fatal(err)
}
client = c
}
func saveReport(jsonData []byte, name string) error {
jsonFile, err := os.Create(conf.ReportsPath + "/" + name + ".json")
if err != nil {
return err
}
defer jsonFile.Close()
_, err = jsonFile.Write(jsonData)
if err != nil {
return err
}
return nil
}
func runReport(reportID int, name string) {
// runs a report from Veracross and saves the JSON file localy
defer wg.Done()
request := fmt.Sprintf("https://axiom.veracross.com/whitby/query/%v/result_data.json", reportID)
req, err := http.NewRequest("POST", request, nil)
req.Header.Set("x-csrf-token", client.Token)
resp, err := client.Do(req)
if err != nil {
log.Println(err)
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Println(err)
return
}
//save report
err = saveReport(body, name)
if err != nil {
log.Println(err)
return
}
}
func run(done chan bool) {
for _, report := range conf.Reports {
wg.Add(1)
go runReport(report.ID, report.Name)
}
wg.Wait()
done <- true
}
func main() {
done := make(chan bool)
ticker := time.NewTicker(time.Minute * conf.Interval).C
for {
go run(done)
<-done
<-ticker
}
}