-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
config.go
137 lines (110 loc) · 3.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
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
package velox
import (
"fmt"
"os"
"github.com/pkg/errors"
)
const (
ref string = "ref"
defaultBranch string = "master"
V2024 string = "v2024"
V2023 string = "v2023"
V2 string = "v2"
gitlabBaseURL string = "https://gitlab.com"
)
type Config struct {
// Version
Roadrunner map[string]string `mapstructure:"roadrunner"`
// Debug configuration
Debug *Debug `mapstructure:"debug"`
// GitHub configuration
GitHub *CodeHosting `mapstructure:"github"`
// GitLab configuration
GitLab *CodeHosting `mapstructure:"gitlab"`
// Log contains log configuration
Log map[string]string `mapstructure:"log"`
}
type Debug struct {
Enabled bool `mapstructure:"enabled"`
}
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"`
Folder string `mapstructure:"folder"`
Replace string `mapstructure:"replace"`
}
func (c *Config) Validate() error { //nolint:gocognit,gocyclo
if _, ok := c.Roadrunner[ref]; !ok {
if c.Roadrunner == nil {
c.Roadrunner = make(map[string]string)
}
c.Roadrunner[ref] = defaultBranch
}
if c.Debug == nil {
c.Debug = &Debug{
Enabled: false,
}
}
if c.GitHub == nil || c.GitHub.Token == nil || c.GitHub.Token.Token == "" {
return errors.New("github section should contain a token to download RoadRunner")
}
// section exists, but no plugin specified
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")
}
// we have a GitHub section
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 == "" {
return errors.New("github.token should not be empty, create a token with any permissions: https://github.com/settings/tokens")
}
c.GitHub.Token.Token = os.ExpandEnv(c.GitHub.Token.Token)
}
// we have a GitLab section
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 c.GitLab.Token == nil || c.GitLab.Token.Token == "" {
return errors.New("gitlab.token should not be empty, create a token with at least [api, read_api] permissions: https://gitlab.com/-/profile/personal_access_tokens")
}
c.GitLab.Token.Token = os.ExpandEnv(c.GitLab.Token.Token)
}
if len(c.Log) == 0 {
c.Log = map[string]string{"level": "debug", "mode": "development"}
}
return nil
}