forked from roadrunner-server/velox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
107 lines (83 loc) · 2.37 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
package velox
import (
"fmt"
"github.com/pkg/errors"
)
const (
ref string = "ref"
defaultBranch string = "master"
gitlabBaseURL string = "https://gitlab.com"
)
type Config struct {
Velox map[string][]string `mapstructure:"velox"`
// Version
Roadrunner map[string]string `mapstructure:"roadrunner"`
// GitHub configuration
GitHub *CodeHosting `mapstructure:"github"`
// GitLab configuration
GitLab *CodeHosting `mapstructure:"gitlab"`
// Log contains log configuration
Log map[string]string `mapstructure:"log"`
}
type Token struct {
Token string `mapstructure:"token"`
}
type Endpoint struct {
BaseURL string `mapstructure:"endpoint"`
}
type CodeHosting struct {
BaseURL *Endpoint `mapstructure:"endpoint"`
Token *Token `mapstructure:"token"`
Plugins map[string]*PluginConfig `mapstructure:"plugins"`
}
type PluginConfig struct {
Ref string `mapstructure:"ref"`
Owner string `mapstructure:"owner"`
Repo string `mapstructure:"repository"`
Replace string `mapstructure:"replace"`
BuildFlags []string `mapstructure:"build-flags"`
}
func (c *Config) Validate() error { //nolint:gocognit,gocyclo
if _, ok := c.Roadrunner[ref]; !ok {
c.Roadrunner[ref] = defaultBranch
}
if (c.GitLab != nil && len(c.GitLab.Plugins) == 0) || (c.GitHub != nil && len(c.GitHub.Plugins) == 0) {
return errors.New("no plugins specified in the configuration")
}
if c.GitHub != nil {
for k, v := range c.GitHub.Plugins {
if v.Owner == "" {
return fmt.Errorf("no owner specified for the plugin: %s", k)
}
if v.Ref == "" {
return fmt.Errorf("no ref specified for the plugin: %s", k)
}
if v.Repo == "" {
return fmt.Errorf("no repository specified for the plugin: %s", k)
}
}
if c.GitHub.Token == nil {
c.GitHub.Token = &Token{Token: ""}
}
}
if c.GitLab != nil {
for k, v := range c.GitLab.Plugins {
if v.Owner == "" {
return fmt.Errorf("no owner specified for the plugin: %s", k)
}
if v.Ref == "" {
return fmt.Errorf("no ref specified for the plugin: %s", k)
}
if v.Repo == "" {
return fmt.Errorf("no repository specified for the plugin: %s", k)
}
}
if c.GitLab.BaseURL == nil {
c.GitLab.BaseURL = &Endpoint{BaseURL: gitlabBaseURL}
}
}
if len(c.Log) == 0 {
c.Log = map[string]string{"level": "debug", "mode": "development"}
}
return nil
}