forked from cert-lv/graphoscope
-
Notifications
You must be signed in to change notification settings - Fork 0
/
loaders.go
85 lines (70 loc) · 1.5 KB
/
loaders.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"regexp"
yaml "gopkg.in/yaml.v3"
)
var (
// Query formatting rules,
// which help to format comma/space separated indicators to a valid SQL query
formats string
)
/*
* Return content of the requested file by its path
*/
func loadFileIntoString(path string) (string, error) {
file, err := ioutil.ReadFile(path)
if err != nil {
return "", err
}
return string(file), nil
}
/*
* Load query formatting rules
*/
func loadFormats() error {
buffer, err := loadFileIntoString(config.Formats)
if err != nil {
return fmt.Errorf("Failed to read rules file '%s': %s", config.Formats, err.Error())
}
var f map[string][]string
err = yaml.Unmarshal([]byte(buffer), &f)
if err != nil {
return fmt.Errorf("Failed unmarshalling rules yaml: %s", err.Error())
}
// Validate regexps
for group, res := range f {
for _, re := range res {
_, err = regexp.Compile(re)
if err != nil {
return fmt.Errorf("Invalid %s's regular expression '%s' : %s", group, re, err.Error())
}
}
}
b, err := json.Marshal(f)
if err != nil {
return fmt.Errorf("Failed to marshal rules struct: %s", err.Error())
}
formats = string(b)
return nil
}
/*
* Load service's version
*/
func loadVersion() error {
path := "VERSION"
var err error
// Try to get from the environment variable first
if os.Getenv(path) != "" {
version = os.Getenv(path)
return nil
}
version, err = loadFileIntoString(path)
if err != nil {
return err
}
return nil
}