-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig.go
175 lines (144 loc) · 4.24 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package fluentbitconfig
import (
"fmt"
"strings"
"golang.org/x/exp/slices"
"github.com/calyptia/go-fluentbit-config/v2/property"
)
type Config struct {
Env property.Properties `json:"env,omitempty" yaml:"env,omitempty"`
Includes []string `json:"includes,omitempty" yaml:"includes,omitempty"`
Service property.Properties `json:"service,omitempty" yaml:"service,omitempty"`
Customs Plugins `json:"customs,omitempty" yaml:"customs,omitempty"`
Pipeline Pipeline `json:"pipeline,omitempty" yaml:"pipeline,omitempty"`
}
type Pipeline struct {
Inputs Plugins `json:"inputs,omitempty" yaml:"inputs,omitempty"`
Parsers Plugins `json:"parsers,omitempty" yaml:"parsers,omitempty"`
Filters Plugins `json:"filters,omitempty" yaml:"filters,omitempty"`
Outputs Plugins `json:"outputs,omitempty" yaml:"outputs,omitempty"`
}
func (c *Config) SetEnv(key string, value any) {
if c.Env == nil {
c.Env = property.Properties{}
}
c.Env.Set(key, value)
}
func (c *Config) Include(path string) {
c.Includes = append(c.Includes, path)
}
func (c *Config) AddSection(kind SectionKind, props property.Properties) {
if kind == SectionKindService {
if c.Service == nil {
c.Service = property.Properties{}
}
for _, p := range props {
c.Service.Set(p.Key, p.Value)
}
return
}
name := Name(props)
if name == "" {
return
}
makePlugin := func(i int) Plugin {
return Plugin{
ID: fmt.Sprintf("%s.%d", name, i),
Name: name,
Properties: props,
}
}
switch kind {
case SectionKindCustom:
c.Customs = append(c.Customs, makePlugin(len(c.Customs)))
case SectionKindInput:
c.Pipeline.Inputs = append(c.Pipeline.Inputs, makePlugin(len(c.Pipeline.Inputs)))
case SectionKindParser:
c.Pipeline.Parsers = append(c.Pipeline.Parsers, makePlugin(len(c.Pipeline.Parsers)))
case SectionKindFilter:
c.Pipeline.Filters = append(c.Pipeline.Filters, makePlugin(len(c.Pipeline.Filters)))
case SectionKindOutput:
c.Pipeline.Outputs = append(c.Pipeline.Outputs, makePlugin(len(c.Pipeline.Outputs)))
}
}
func (c Config) Equal(target Config) bool {
if !c.Env.Equal(target.Env) {
return false
}
if !slices.Equal(c.Includes, target.Includes) {
return false
}
if !c.Service.Equal(target.Service) {
return false
}
if !c.Customs.Equal(target.Customs) {
return false
}
if !c.Pipeline.Inputs.Equal(target.Pipeline.Inputs) {
return false
}
if !c.Pipeline.Parsers.Equal(target.Pipeline.Parsers) {
return false
}
if !c.Pipeline.Filters.Equal(target.Pipeline.Filters) {
return false
}
if !c.Pipeline.Outputs.Equal(target.Pipeline.Outputs) {
return false
}
return true
}
// IDs namespaced with the section kind and name.
// For example: input:tail:tail.0
func (c Config) IDs(namespaced bool) []string {
var ids []string
set := func(kind SectionKind, plugins Plugins) {
for _, plugin := range plugins {
if namespaced {
ids = append(ids, fmt.Sprintf("%s:%s:%s", kind, plugin.Name, plugin.ID))
} else {
ids = append(ids, plugin.ID)
}
}
}
set(SectionKindCustom, c.Customs)
set(SectionKindInput, c.Pipeline.Inputs)
set(SectionKindParser, c.Pipeline.Parsers)
set(SectionKindFilter, c.Pipeline.Filters)
set(SectionKindOutput, c.Pipeline.Outputs)
return ids
}
// FindByID were the id should be namespaced with the section kind and name.
// For example: input:tail:tail.0
func (c Config) FindByID(id string) (Plugin, bool) {
find := func(kind SectionKind, plugins Plugins, id string) (Plugin, bool) {
for _, plugin := range plugins {
if fmt.Sprintf("%s:%s:%s", kind, plugin.Name, plugin.ID) == id {
return plugin, true
}
}
return Plugin{}, false
}
if plugin, ok := find(SectionKindCustom, c.Customs, id); ok {
return plugin, true
}
if plugin, ok := find(SectionKindInput, c.Pipeline.Inputs, id); ok {
return plugin, true
}
if plugin, ok := find(SectionKindParser, c.Pipeline.Parsers, id); ok {
return plugin, true
}
if plugin, ok := find(SectionKindFilter, c.Pipeline.Filters, id); ok {
return plugin, true
}
return Plugin{}, false
}
// Name from properties.
func Name(props property.Properties) string {
nameVal, ok := props.Get("name")
if !ok {
return ""
}
name := stringFromAny(nameVal)
return strings.ToLower(strings.TrimSpace(strings.ToValidUTF8(name, "")))
}