/
config.go
95 lines (77 loc) · 2.78 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
package flow
import (
"bytes"
"encoding/json"
"fmt"
"github.com/direktiv/direktiv/pkg/flow/ent/schema"
"github.com/jinzhu/copier"
)
type Config struct {
Broadcast *ConfigBroadcast `json:"broadcast"`
}
type ConfigBroadcast struct {
WorkflowCreate *bool `json:"workflow.create"`
WorkflowUpdate *bool `json:"workflow.update"`
WorkflowDelete *bool `json:"workflow.delete"`
DirectoryCreate *bool `json:"directory.create"`
DirectoryDelete *bool `json:"directory.delete"`
WorkflowVariableCreate *bool `json:"workflow.variable.create"`
WorkflowVariableUpdate *bool `json:"workflow.variable.update"`
WorkflowVariableDelete *bool `json:"workflow.variable.delete"`
NamespaceVariableCreate *bool `json:"namespace.variable.create"`
NamespaceVariableUpdate *bool `json:"namespace.variable.update"`
NamespaceVariableDelete *bool `json:"namespace.variable.delete"`
InstanceVariableCreate *bool `json:"instance.variable.create"`
InstanceVariableUpdate *bool `json:"instance.variable.update"`
InstanceVariableDelete *bool `json:"instance.variable.delete"`
InstanceStarted *bool `json:"instance.started"`
InstanceSuccess *bool `json:"instance.success"`
InstanceFailed *bool `json:"instance.failed"`
}
var defaultNamespaceConfig Config
func init() {
err := json.Unmarshal([]byte(schema.DefaultNamespaceConfig), &defaultNamespaceConfig)
if err != nil {
panic(fmt.Errorf("failed to unmarshal defaultNamespaceConfig: %w", err))
}
}
// mergeIntoNamespaceConfig : Unmarshal sourceCfg and merge it's content onto self
// merged contents are then marshaled and returned.
func (c *Config) mergeIntoNamespaceConfig(sourceCfg []byte) ([]byte, error) {
var sourceConfig Config
err := json.Unmarshal(sourceCfg, &sourceConfig)
if err != nil {
return nil, err
}
err = copier.CopyWithOption(&sourceConfig, c, copier.Option{IgnoreEmpty: true, DeepCopy: true})
if err != nil {
return nil, fmt.Errorf("failed to merge config: %w", err)
}
mergedCfg, err := json.Marshal(sourceConfig)
return mergedCfg, err
}
// loadNSConfig : loads config object from json cfgData
func loadNSConfig(cfgData []byte) (*Config, error) {
nsCFG := new(Config)
dec := json.NewDecoder(bytes.NewReader(cfgData))
dec.DisallowUnknownFields()
err := dec.Decode(&nsCFG)
return nsCFG, err
}
// broadcastEnabled : Checks if a broadcastTarget is available.
// If available return if it's value; mising target keys are returned as false
func (c *Config) broadcastEnabled(broadcastTarget string) bool {
var broadcastMap map[string]bool
bData, err := json.Marshal(c.Broadcast)
if err != nil {
return false
}
err = json.Unmarshal(bData, &broadcastMap)
if err != nil {
return false
}
if enabled, ok := broadcastMap[broadcastTarget]; ok {
return enabled
}
return false
}