-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.go
253 lines (219 loc) · 9.3 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/******************************************************************************
*
* Copyright 2020 SAP SE
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/
package core
import (
"os/exec"
"strings"
"github.com/sapcc/go-bits/logg"
)
var AutogeneratedHeader = strings.TrimSpace(`
################################################################################
# This file is AUTOGENERATED with <https://github.com/sapcc/go-makefile-maker> #
# Edit Makefile.maker.yaml instead. #
################################################################################
`)
///////////////////////////////////////////////////////////////////////////////
// Core configuration
// Configuration is the data structure that we read from the input file.
type Configuration struct {
Verbatim string `yaml:"verbatim"`
VariableValues map[string]string `yaml:"variables"`
Binaries []BinaryConfiguration `yaml:"binaries"`
Test TestConfiguration `yaml:"testPackages"`
Coverage CoverageConfiguration `yaml:"coverageTest"`
Golang GolangConfiguration `yaml:"golang"`
GolangciLint GolangciLintConfiguration `yaml:"golangciLint"`
GoReleaser GoReleaserConfiguration `yaml:"goReleaser"`
SpellCheck SpellCheckConfiguration `yaml:"spellCheck"`
GitHubWorkflow *GithubWorkflowConfiguration `yaml:"githubWorkflow"`
Makefile MakefileConfig `yaml:"makefile"`
Renovate RenovateConfig `yaml:"renovate"`
Dockerfile DockerfileConfig `yaml:"dockerfile"`
Metadata Metadata `yaml:"metadata"`
}
// Variable returns the value of this variable if it's overridden in the config,
// or the default value otherwise.
func (c Configuration) Variable(name, defaultValue string) string {
value, exists := c.VariableValues[name]
if exists {
return " " + value
}
if defaultValue == "" {
return ""
}
return " " + defaultValue
}
// BinaryConfiguration appears in type Configuration.
type BinaryConfiguration struct {
Name string `yaml:"name"`
FromPackage string `yaml:"fromPackage"`
InstallTo string `yaml:"installTo"`
}
// TestConfiguration appears in type Configuration.
type TestConfiguration struct {
Only string `yaml:"only"`
Except string `yaml:"except"`
}
// CoverageConfiguration appears in type Configuration.
type CoverageConfiguration struct {
Only string `yaml:"only"`
Except string `yaml:"except"`
}
// GolangConfiguration appears in type Configuration.
type GolangConfiguration struct {
EnableVendoring bool `yaml:"enableVendoring"`
LdFlags map[string]string `yaml:"ldflags"`
SetGoModVersion bool `yaml:"setGoModVersion"`
}
// GolangciLintConfiguration appears in type Configuration.
type GolangciLintConfiguration struct {
CreateConfig bool `yaml:"createConfig"`
ErrcheckExcludes []string `yaml:"errcheckExcludes"`
SkipDirs []string `yaml:"skipDirs"`
}
type GoReleaserConfiguration struct {
CreateConfig bool `yaml:"createConfig"`
BinaryName string `yaml:"binaryName"`
Files *[]string `yaml:"files"`
Format string `yaml:"format"`
Ldflags map[string]string `yaml:"ldflags"`
NameTemplate string `yaml:"nameTemplate"`
}
// SpellCheckConfiguration appears in type Configuration.
type SpellCheckConfiguration struct {
IgnoreWords []string `yaml:"ignoreWords"`
}
///////////////////////////////////////////////////////////////////////////////
// GitHub workflow configuration
// GithubWorkflowConfiguration appears in type Configuration.
type GithubWorkflowConfiguration struct {
// These global-level settings are applicable for all workflows. They are
// superseded by their workflow-level counterpart(s).
Global struct {
DefaultBranch string `yaml:"defaultBranch"`
GoVersion string `yaml:"goVersion"`
} `yaml:"global"`
CI CIWorkflowConfig `yaml:"ci"`
IsSelfHostedRunner bool `yaml:"omit"`
License LicenseWorkflowConfig `yaml:"license"`
PushContainerToGhcr PushContainerToGhcrConfig `yaml:"pushContainerToGhcr"`
Release ReleaseWorkflowConfig `yaml:"release"`
SecurityChecks SecurityChecksWorkflowConfig `yaml:"securityChecks"`
}
// CIWorkflowConfig appears in type Configuration.
type CIWorkflowConfig struct {
Enabled bool `yaml:"enabled"`
IgnorePaths []string `yaml:"ignorePaths"`
RunnerType []string `yaml:"runOn"`
Coveralls bool `yaml:"coveralls"`
Postgres bool `yaml:"postgres"`
}
// LicenseWorkflowConfig appears in type Configuration.
type LicenseWorkflowConfig struct {
Enabled *bool `yaml:"enabled"`
IgnorePatterns []string `yaml:"ignorePatterns"`
}
type PushContainerToGhcrConfig struct {
Enabled bool `yaml:"enabled"`
}
type ReleaseWorkflowConfig struct {
Enabled bool `yaml:"enabled"`
}
// SecurityChecksWorkflowConfig appears in type Configuration.
type SecurityChecksWorkflowConfig struct {
Enabled *bool `yaml:"enabled"`
}
type PackageRule struct {
ExcludePackagePatterns []string `yaml:"excludePackagePatterns" json:"excludePackagePatterns,omitempty"`
MatchPackageNames []string `yaml:"matchPackageNames" json:"matchPackageNames,omitempty"`
MatchPackagePatterns []string `yaml:"matchPackagePatterns" json:"matchPackagePatterns,omitempty"`
MatchPackagePrefixes []string `yaml:"matchPackagePrefixes" json:"matchPackagePrefixes,omitempty"`
MatchUpdateTypes []string `yaml:"matchUpdateTypes" json:"matchUpdateTypes,omitempty"`
MatchDepTypes []string `yaml:"matchDepTypes" json:"matchDepTypes,omitempty"`
MatchFiles []string `yaml:"matchFiles" json:"matchFiles,omitempty"`
AllowedVersions string `yaml:"allowedVersions" json:"allowedVersions,omitempty"`
AutoMerge bool `yaml:"automerge" json:"automerge,omitempty"`
EnableRenovate *bool `yaml:"enabled" json:"enabled,omitempty"`
GroupName string `yaml:"groupName" json:"groupName,omitempty"`
}
// RenovateConfig appears in type Configuration.
type RenovateConfig struct {
Enabled bool `yaml:"enabled"`
Assignees []string `yaml:"assignees"`
GoVersion string `yaml:"goVersion"`
PackageRules []PackageRule `yaml:"packageRules"`
}
// DockerfileConfig appears in type Configuration.
type DockerfileConfig struct {
Enabled bool `yaml:"enabled"`
Entrypoint []string `yaml:"entrypoint"`
ExtraDirectives []string `yaml:"extraDirectives"`
ExtraIgnores []string `yaml:"extraIgnores"`
ExtraPackages []string `yaml:"extraPackages"`
RunAsRoot bool `yaml:"runAsRoot"`
WithLinkerdAwait bool `yaml:"withLinkerdAwait"`
}
type MakefileConfig struct {
Enabled *bool `yaml:"enabled"` // this is a pointer to bool to treat an absence as true for backwards compatibility
}
type Metadata struct {
URL string `yaml:"url"`
}
///////////////////////////////////////////////////////////////////////////////
// Helper functions
func (c *Configuration) Validate() {
if c.Dockerfile.Enabled {
if c.Metadata.URL == "" {
logg.Fatal("metadata.url must be set when docker.enabled is true")
}
}
// Validate GolangciLintConfiguration.
if len(c.GolangciLint.ErrcheckExcludes) > 0 && !c.GolangciLint.CreateConfig {
logg.Fatal("golangciLint.createConfig must be set to 'true' if golangciLint.errcheckExcludes is defined")
}
// Validate GithubWorkflowConfiguration.
ghwCfg := c.GitHubWorkflow
if ghwCfg != nil {
if c.Metadata.URL == "" {
logg.Fatal("metadata.url must be set when any github workflow is configured otherwise it cannot be determined which github runner type should be used")
}
// Validate global options.
if ghwCfg.Global.DefaultBranch == "" {
errMsg := "could not find default branch using git, you can define manually be setting 'githubWorkflow.global.defaultBranch' in config"
b, err := exec.Command("git", "symbolic-ref", "refs/remotes/origin/HEAD").CombinedOutput()
if err != nil {
logg.Fatal("%s: %s", errMsg, err.Error())
}
branch := strings.TrimPrefix(string(b), "refs/remotes/origin/")
if branch == string(b) {
logg.Fatal(errMsg)
} else {
c.GitHubWorkflow.Global.DefaultBranch = strings.TrimSpace(branch)
}
}
// Validate CI workflow configuration.
if ghwCfg.CI.Postgres {
if !ghwCfg.CI.Enabled {
logg.Fatal("githubWorkflow.ci.enabled must be set to 'true' when githubWorkflow.ci.postgres is enabled")
}
if len(ghwCfg.CI.RunnerType) > 1 && !strings.HasPrefix(ghwCfg.CI.RunnerType[0], "ubuntu") {
logg.Fatal("githubWorkflow.ci.runOn must only define a single Ubuntu based runner when githubWorkflow.ci.postgres is enabled")
}
}
}
}