-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
100 lines (85 loc) · 1.98 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
package main
import (
"database/sql"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"github.com/urfave/cli/v2"
)
const configTemplate = `{
"tables": [
{
"name": "x",
"partition_schema": "yearweek",
"retention": 5,
"max_future_partitions": 5
},
{
"name": "y",
"partition_schema": "yearweek",
"retention": 2,
"max_future_partitions": 1
}
]
}
`
type Config struct {
Database string
DatabaseDSN string
Tables []Table `json:"tables"`
db *sql.DB
}
type Table struct {
Name string `json:"name"`
PartitionSchema string `json:"partition_schema"`
Retention int `json:"retention"`
MaxFuturePartitions int `json:"max_future_partitions"`
}
func template(_ *cli.Context) error {
fmt.Fprintln(os.Stdout, configTemplate)
return nil
}
func config(ctx *cli.Context) (err error) {
_, err = loadAndValidateConfig(ctx)
if err == nil {
// don't leave the user hanging
fmt.Fprintln(os.Stdout, "Configuration is valid!")
}
return
}
func loadAndValidateConfig(ctx *cli.Context) (*Config, error) {
c := Config{
Database: ctx.String("database"),
DatabaseDSN: ctx.String("database-dsn"),
}
configBytes, err := ioutil.ReadFile(ctx.String("config"))
if err != nil {
err = errors.New("Unable to load config file: " + err.Error())
return nil, err
}
err = json.Unmarshal(configBytes, &c)
if err != nil {
err = errors.New("Unable to parse config file: " + err.Error())
return nil, err
}
c.db, err = connectDB(c.DatabaseDSN)
if err != nil {
err = errors.New("Failed to establish connection with SQL database: " + err.Error())
return nil, err
}
err = c.db.Ping()
if err != nil {
err = errors.New("Failed to ping SQL database: " + err.Error())
return nil, err
}
for _, table := range c.Tables {
err = verifyTable(c.db, c.Database, table.Name, table.PartitionSchema)
if err != nil {
err = errors.New("Failed to verify table " + table.Name + ": " + err.Error())
return nil, err
}
}
return &c, nil
}