This repository has been archived by the owner on Apr 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathconfig.go
116 lines (102 loc) · 2.53 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
package main
import (
"fmt"
"io/ioutil"
"log"
"os"
"regexp"
"strings"
"github.com/pelletier/go-toml"
"github.com/pkg/errors"
)
// Storage holds the configuration for [storage] section of the toml config.
type Storage struct {
LogDirectory string
}
// GCP holds the configuration for [gcp] section of the toml config.
type GCP struct {
ProjectID string
CredentialsFile string
UploadBucket string
Dataset string
TemplateTable string
LogPrefix string
LogBucket string
}
// Exclude holds the configuration for the [[apps.excludes]] subsection
// of the toml config.
type Exclude struct {
Group int
Contains string
}
// App holds the configuration for a single entry in the [[apps]]
// section of the toml config.
type App struct {
Name string
Regex string
CompiledRegex *regexp.Regexp
TimeGroup int
TimeFormat string
Excludes []Exclude
}
func (app *App) isExcluded(r []string) bool {
exclude := false
for _, e := range app.Excludes {
if e.Group >= len(r) {
log.Printf("skipping exclusion: %v, Group not found in result", e)
continue
}
if strings.Contains(r[e.Group], e.Contains) {
exclude = true
break
}
}
return exclude
}
// Configuration holds the full configuration loaded from the toml config file.
type Configuration struct {
Storage Storage
GCP GCP
Apps []App
}
func (cfg *Configuration) getApp(c string) (App, error) {
for _, app := range cfg.Apps {
if c == app.Name {
return app, nil
}
}
return App{}, errors.New("App not found")
}
func (cfg *Configuration) extractAppNames() (set map[string]struct{}) {
set = make(map[string]struct{}, len(cfg.Apps))
for _, app := range cfg.Apps {
set[app.Name] = struct{}{}
}
return set
}
func (cfg *Configuration) compileRegex() {
for i, c := range cfg.Apps {
cmp := regexp.MustCompile(c.Regex)
cfg.Apps[i].CompiledRegex = cmp
}
}
func (cfg *Configuration) setupDirectory() error {
err := os.MkdirAll(cfg.Storage.LogDirectory, os.ModePerm)
if err != nil {
err = errors.Wrap(err, fmt.Sprintf("Unable to create dir %s", cfg.Storage.LogDirectory))
}
return err
}
// NewConfiguration takes a path to a toml file and returns a new Configuration
func NewConfiguration(path string) (*Configuration, error) {
cfg := &Configuration{}
data, err := ioutil.ReadFile(path)
if err != nil {
return nil, errors.Wrap(err, fmt.Sprintf("Unable to open config (%s)", path))
}
err = toml.Unmarshal(data, cfg)
if err != nil {
return nil, errors.Wrap(err, "Error loading config")
}
return cfg, nil
}