-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.go
265 lines (208 loc) · 4.61 KB
/
config.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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
package main
import (
"errors"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
"time"
"golang.org/x/sys/unix"
yaml "gopkg.in/yaml.v2"
)
var (
// The default number of workers (parallel queries).
DefaultWorkers = 5
DefaultFormat = "csv"
DefaultCache = "/tmp"
)
func formatMimetype(f string) string {
switch strings.ToLower(f) {
case "csv":
return "text/csv"
case "json":
return "application/json"
case "ldjson":
return "application/x-ldjson"
}
return ""
}
type Connection struct {
Driver string `json:"driver"`
Info map[string]interface{} `json:"info"`
}
type QueryConfig struct {
// The connection to use for these queries.
Connection string
// Directory containing one of more files with queries.
Dir string
// Single file containing a query.
File string
SQL string
// Name of the query. This only applies if SQL or File is set.
Name string
}
type Query struct {
Connection *Connection
// Name of the query. By default, the basename of SQL file.
Name string
// File containing the query.
File string
// The SQL string to be executed.
SQL string
// Timestamps for information purposes.
ScheduleTime time.Time
ExecuteTime time.Time
CompleteTime time.Time
}
type Config struct {
// Number of workers, i.e. parallel queries.
Workers int
GZip bool
Format string
Connections map[string]*Connection
// Local cache directory.
Cache struct {
Path string
Purge bool
}
SQLAgent struct {
Addr string
}
HTTP struct {
Addr string
}
NATS struct {
URL string
Topic string
}
// Set of query configurations.
Queries []*QueryConfig
// Configuration for S3 storage.
S3 *S3Storage
Schedule struct {
Cron string
}
}
func ReadConfig(file string) (*Config, error) {
b, err := ioutil.ReadFile(file)
if err != nil {
return nil, fmt.Errorf("error reading config file: %s", err)
}
var c Config
if err := yaml.Unmarshal(b, &c); err != nil {
return nil, fmt.Errorf("error decoding config file: %s", err)
}
// Ensure output directory is available.
if c.Cache.Path == "" {
c.Cache.Path = DefaultCache
}
if c.Format == "" {
c.Format = DefaultFormat
} else if formatMimetype(c.Format) == "" {
return nil, fmt.Errorf("unsupported format: %s", c.Format)
}
info, err := os.Stat(c.Cache.Path)
if err != nil {
return nil, err
}
if !info.IsDir() {
return nil, errors.New("output directory is not a directory.")
}
// Check if directory is writable.
if unix.Access(c.Cache.Path, unix.W_OK) != nil {
return nil, errors.New("output directory is not writable.")
}
if c.S3 != nil {
if err := c.S3.Auth(); err != nil {
return nil, fmt.Errorf("error with S3 auth: %s", err)
}
}
return &c, nil
}
func (c *Config) ReadQueries() ([]*Query, error) {
scheduleTime := time.Now()
var queries []*Query
for _, qc := range c.Queries {
// Check if connection exists.
conn, ok := c.Connections[qc.Connection]
if !ok {
return nil, fmt.Errorf("No connection named %s", qc.Connection)
}
// Read queries from a directory.
if qc.Dir != "" {
x, err := ReadQueryDir(qc.Dir)
if err != nil {
return nil, err
}
// Ensure a repo is
for _, q := range x {
q.Connection = conn
q.ScheduleTime = scheduleTime
queries = append(queries, q)
}
} else if qc.File != "" {
q, err := ReadQueryFile(qc.File)
if err != nil {
return nil, err
}
if qc.Name != "" {
q.Name = qc.Name
}
q.Connection = conn
q.ScheduleTime = scheduleTime
queries = append(queries, q)
} else if qc.SQL != "" {
if qc.Name == "" {
return nil, errors.New("name required for inline SQL")
}
q := &Query{
Name: qc.Name,
SQL: qc.SQL,
Connection: conn,
ScheduleTime: scheduleTime,
}
queries = append(queries, q)
} else {
log.Printf("warn: Query config does not have `dir` or `file` set")
}
}
return queries, nil
}
func ReadQueryFile(path string) (*Query, error) {
b, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
// Trim off .sql extension.
baseName := filepath.Base(path[:len(path)-4])
return &Query{
File: path,
Name: baseName,
SQL: string(b),
}, nil
}
func ReadQueryDir(dir string) ([]*Query, error) {
var queries []*Query
// Read all the queries.
err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if info.IsDir() {
return nil
}
fileName := info.Name()
if !strings.HasSuffix(fileName, ".sql") {
return nil
}
q, err := ReadQueryFile(path)
if err != nil {
return err
}
queries = append(queries, q)
return nil
})
if err != nil {
return nil, err
}
return queries, nil
}