forked from MediaMath/keryxlib
/
config.go
77 lines (66 loc) · 2.17 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
package keryxlib
import (
"encoding/json"
"io/ioutil"
"os"
"github.com/MediaMath/keryxlib/message"
)
//Config contains necessary information to start a keryx stream
type Config struct {
DataDir string `json:"data_dir"`
PGConnStrings []string `json:"pg_conn_strings"`
BufferMax int `json:"buffer_max"`
ExcludeRelations map[string][]string `json:"exclude,omitempty"`
IncludeRelations map[string][]string `json:"include,omitempty"`
BufferDirectory string `json:"buffer_directory"`
MaxMessagePerTxn uint `json:"max_message_per_txn"`
}
func (config *Config) IncludedTables() []message.Table {
var tables []message.Table
if len(config.IncludeRelations) > 0 {
for tableName, _ := range config.IncludeRelations {
table := message.TableFromFullName(tableName)
if table != nil {
tables = append(tables, *table)
}
}
}
return tables
}
func (config *Config) ExcludedTables() []message.Table {
var tables []message.Table
for tableName, columns := range config.ExcludeRelations {
table := message.TableFromFullName(tableName)
if table != nil && (len(columns) != 0 || columns[0] != "*") {
tables = append(tables, *table)
}
}
return tables
}
//BufferDirectoryDefaultBase is the root directory to attempt to create buffers files in if
//nothing else is specified
const BufferDirectoryDefaultBase = "/var/tmp/keryx"
//GetBufferDirectoryOrTemp gets the buffer directory out of the config file. If it isnt defined it creates it in BufferDirectoryDefaultBase
func (c *Config) GetBufferDirectoryOrTemp() (bufferWorkingDirectory string, err error) {
bufferWorkingDirectory = c.BufferDirectory
if bufferWorkingDirectory == "" {
err = os.MkdirAll(BufferDirectoryDefaultBase, 0700)
if err == nil {
bufferWorkingDirectory, err = ioutil.TempDir(BufferDirectoryDefaultBase, "buffer")
}
}
return
}
//ConfigFromFile loads a config object from a json file
func ConfigFromFile(path string) (*Config, error) {
file, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
config := new(Config)
err = json.Unmarshal(file, config)
if err != nil {
return nil, err
}
return config, nil
}