-
Notifications
You must be signed in to change notification settings - Fork 65
/
config_rb_reader.go
64 lines (58 loc) · 1.42 KB
/
config_rb_reader.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
package chef
import (
"errors"
"os"
"path/filepath"
"strings"
)
type ConfigRb struct {
ClientKey string
ChefServerUrl string
NodeName string
}
type clientFunc func(s []string, path string, m *ConfigRb) error
var clientRegistry map[string]clientFunc
func init() {
clientRegistry = make(map[string]clientFunc, 2)
clientRegistry["client_key"] = configKeyParser
clientRegistry["chef_server_url"] = configServerParser
clientRegistry["node_name"] = configNodeNameParser
}
func NewClientRb(data, path string) (c ConfigRb, err error) {
linesData := strings.Split(data, "\n")
if len(linesData) < 3 {
return c, errors.New("not much info")
}
for _, i := range linesData {
key, value := getKeyValue(strings.TrimSpace(i))
if fn, ok := clientRegistry[key]; ok {
err = fn(value, path, &c)
if err != nil {
return
}
}
}
return c, err
}
func configKeyParser(s []string, path string, c *ConfigRb) error {
str := StringParserForMeta(s)
data := strings.Split(str, "/")
size := len(data)
if size > 0 {
keyPath := filepath.Join(path, data[size-1])
keyData, err := os.ReadFile(keyPath)
if err != nil {
return err
}
c.ClientKey = string(keyData)
}
return nil
}
func configServerParser(s []string, path string, c *ConfigRb) error {
c.ChefServerUrl = StringParserForMeta(s)
return nil
}
func configNodeNameParser(s []string, path string, c *ConfigRb) error {
c.NodeName = StringParserForMeta(s)
return nil
}