-
Notifications
You must be signed in to change notification settings - Fork 12
/
config.go
120 lines (101 loc) · 3.94 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
package config
import (
"encoding/json"
"log"
"strings"
"github.com/antham/envh"
"github.com/antham/chyle/chyle/decorators"
"github.com/antham/chyle/chyle/extractors"
"github.com/antham/chyle/chyle/matchers"
"github.com/antham/chyle/chyle/senders"
)
var chyleConfig CHYLE
// EnvValidationError is called when validating a
// configuration failed, it keeps a track of which
// environment variable is actually tested
type EnvValidationError struct {
message string
env string
}
// Env returns environment variable currently tested
func (v EnvValidationError) Env() string {
return v.env
}
// Error returns error as string
func (v EnvValidationError) Error() string {
return v.message
}
// configurater must be implemented to process custom config
type configurater interface {
process(config *CHYLE) (bool, error)
}
type ref struct {
ref *string
keyChain []string
}
// codebeat:disable[TOO_MANY_IVARS]
// CHYLE hold config extracted from environment variables
type CHYLE struct {
FEATURES struct {
MATCHERS matchers.Features
EXTRACTORS extractors.Features
DECORATORS decorators.Features
SENDERS senders.Features
} `json:"-"`
GIT struct {
REPOSITORY struct {
PATH string
}
REFERENCE struct {
FROM string
TO string
}
}
MATCHERS matchers.Config
EXTRACTORS extractors.Config
DECORATORS decorators.Config
SENDERS senders.Config
}
// codebeat:enable[TOO_MANY_IVARS]
// Walk traverses struct to populate or validate fields
func (c *CHYLE) Walk(fullconfig *envh.EnvTree, keyChain []string) (bool, error) {
if walker, ok := map[string]func(*envh.EnvTree, []string) (bool, error){
"CHYLE_FEATURES": func(*envh.EnvTree, []string) (bool, error) { return true, nil },
"CHYLE_GIT_REFERENCE": c.validateChyleGitReference,
"CHYLE_GIT_REPOSITORY": c.validateChyleGitRepository,
}[strings.Join(keyChain, "_")]; ok {
return walker(fullconfig, keyChain)
}
if processor, ok := map[string]func() configurater{
"CHYLE_DECORATORS_ENV": func() configurater { return &envDecoratorConfigurator{fullconfig} },
"CHYLE_DECORATORS_CUSTOMAPI": func() configurater { return newCustomAPIDecoratorConfigurator(fullconfig) },
"CHYLE_DECORATORS_GITHUBISSUE": func() configurater { return newGithubIssueDecoratorConfigurator(fullconfig) },
"CHYLE_DECORATORS_JIRAISSUE": func() configurater { return newJiraIssueDecoratorConfigurator(fullconfig) },
"CHYLE_DECORATORS_SHELL": func() configurater { return &shellDecoratorConfigurator{fullconfig} },
"CHYLE_EXTRACTORS": func() configurater { return &extractorsConfigurator{fullconfig} },
"CHYLE_MATCHERS": func() configurater { return &matchersConfigurator{fullconfig} },
"CHYLE_SENDERS_GITHUBRELEASE": func() configurater { return &githubReleaseSenderConfigurator{fullconfig} },
"CHYLE_SENDERS_CUSTOMAPI": func() configurater { return &customAPISenderConfigurator{fullconfig} },
"CHYLE_SENDERS_STDOUT": func() configurater { return &stdoutSenderConfigurator{fullconfig} },
}[strings.Join(keyChain, "_")]; ok {
return processor().process(c)
}
return false, nil
}
func (c *CHYLE) validateChyleGitRepository(fullconfig *envh.EnvTree, keyChain []string) (bool, error) {
return false, validateEnvironmentVariablesDefinition(fullconfig, [][]string{{"CHYLE", "GIT", "REPOSITORY", "PATH"}})
}
func (c *CHYLE) validateChyleGitReference(fullconfig *envh.EnvTree, keyChain []string) (bool, error) {
return false, validateEnvironmentVariablesDefinition(fullconfig, [][]string{{"CHYLE", "GIT", "REFERENCE", "FROM"}, {"CHYLE", "GIT", "REFERENCE", "TO"}})
}
// Create returns app config from an EnvTree object
func Create(envConfig *envh.EnvTree) (*CHYLE, error) {
chyleConfig = CHYLE{}
return &chyleConfig, envConfig.PopulateStruct(&chyleConfig)
}
// Debug dumps given CHYLE config as JSON structure
func Debug(config *CHYLE, logger *log.Logger) {
if d, err := json.MarshalIndent(config, "", " "); err == nil {
logger.Println(string(d))
}
}