-
Notifications
You must be signed in to change notification settings - Fork 0
/
configparser.go
87 lines (79 loc) · 2.23 KB
/
configparser.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
package main
import (
"fmt"
"io"
"os"
"strings"
"github.com/pelletier/go-toml/v2"
"github.com/robfig/cron/v3"
)
type DatabaseConfig struct {
ConnString string
PasswordVar string
JustUsePgPass bool
}
func DecodeDatabases(crontab io.Reader, usepgpass bool) (map[string]string, error) {
var configs map[string]DatabaseConfig
decoder := toml.NewDecoder(crontab)
err := decoder.Decode(&configs)
if err != nil {
return nil, err
}
databases := map[string]string{}
for key, config := range configs {
if config.ConnString == "" {
return nil, fmt.Errorf("Missing connstring in database %s", key)
}
if usepgpass || config.JustUsePgPass {
databases[key] = strings.Replace(config.ConnString, ":$password", "", 1)
} else if config.PasswordVar == "" {
databases[key] = config.ConnString
} else {
password := os.Getenv(config.PasswordVar)
if password == "" {
return nil, fmt.Errorf("Injected passwordvar %s is empty!", config.PasswordVar)
}
databases[key] = strings.Replace(config.ConnString, "$password", password, 1)
}
}
return databases, nil
}
type JobMiscOptions struct {
SkipValidation bool
AllowConcurrentJobs bool
Description string
}
type JobConfig struct {
CronSchedule string
Database string
Query string
JobMiscOptions
}
func DecodeJobs(crontab io.Reader) (jobconfigs map[string]JobConfig, err error) {
decoder := toml.NewDecoder(crontab)
err = decoder.Decode(&jobconfigs)
if err != nil {
return nil, err
}
return jobconfigs, nil
}
func CreateJobs(configs map[string]JobConfig, databases map[string]string, monitor Monitor) ([]Job, error) {
jobs := []Job{}
for name, config := range configs {
schedule, err := cron.ParseStandard(config.CronSchedule)
if err != nil {
return nil, fmt.Errorf("Cron schedule error: %w", err)
}
connstr, ok := databases[config.Database]
if !ok {
return nil, fmt.Errorf("Missing Db: The database %s specified by job %s does not seem to exist!", config.Database, name)
}
job, err := CreateJob(name, config.Database, schedule, connstr, config.Query, config.JobMiscOptions, monitor)
if err != nil {
return nil, err
}
jobs = append(jobs, job)
}
sortJobsLex(jobs) // since iterating over map keys is random.
return jobs, nil
}