-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
106 lines (85 loc) · 2.98 KB
/
main.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
// Copyright 2020 SAP SE
// SPDX-License-Identifier: Apache-2.0
package main
import (
"os"
"strings"
"gopkg.in/yaml.v3"
"github.com/sapcc/go-bits/logg"
"github.com/sapcc/go-bits/must"
"github.com/sapcc/go-makefile-maker/internal/core"
"github.com/sapcc/go-makefile-maker/internal/dockerfile"
"github.com/sapcc/go-makefile-maker/internal/ghworkflow"
"github.com/sapcc/go-makefile-maker/internal/golang"
"github.com/sapcc/go-makefile-maker/internal/golangcilint"
"github.com/sapcc/go-makefile-maker/internal/goreleaser"
"github.com/sapcc/go-makefile-maker/internal/makefile"
"github.com/sapcc/go-makefile-maker/internal/nix"
"github.com/sapcc/go-makefile-maker/internal/renovate"
)
func main() {
file := must.Return(os.Open("Makefile.maker.yaml"))
var cfg core.Configuration
dec := yaml.NewDecoder(file)
dec.KnownFields(true)
must.Succeed(dec.Decode(&cfg))
must.Succeed(file.Close())
cfg.Validate()
if cfg.GitHubWorkflow != nil && !strings.HasPrefix(cfg.Metadata.URL, "https://github.com") {
cfg.GitHubWorkflow.IsSelfHostedRunner = true
}
if cfg.Golang.SetGoModVersion {
golang.SetGoVersionInGoMod()
}
if fs, err := os.Stat("vendor/modules.txt"); err == nil && fs != nil {
cfg.Golang.EnableVendoring = true
}
// Scan go.mod file for additional context information.
sr := golang.Scan()
renderGoreleaserConfig := (cfg.GoReleaser.CreateConfig == nil && cfg.GitHubWorkflow != nil && cfg.GitHubWorkflow.Release.Enabled) || (cfg.GoReleaser.CreateConfig != nil && *cfg.GoReleaser.CreateConfig)
nix.RenderShell(cfg, sr, renderGoreleaserConfig)
// Render Makefile
if cfg.Makefile.Enabled == nil || *cfg.Makefile.Enabled {
for _, bin := range cfg.Binaries {
if !strings.HasPrefix(bin.FromPackage, ".") {
logg.Fatal("binaries[].fromPackage must begin with a dot, %q is not allowed!", bin.FromPackage)
}
}
makefile.Render(cfg, sr)
}
// Render Dockerfile
if cfg.Dockerfile.Enabled {
dockerfile.RenderConfig(cfg)
}
// Render golangci-lint config file
if cfg.GolangciLint.CreateConfig {
golangcilint.RenderConfig(cfg, sr)
}
// Render Goreleaser config file
if renderGoreleaserConfig {
goreleaser.RenderConfig(cfg)
}
// Render GitHub workflows
if cfg.GitHubWorkflow != nil {
// consider different fallbacks when no explicit go version is set
if cfg.GitHubWorkflow.Global.GoVersion == "" {
// default to the version in go.mod
goVersion := sr.GoVersion
// overwrite it, we want to use the latest go version
if cfg.Golang.SetGoModVersion {
goVersion = core.DefaultGoVersion
}
cfg.GitHubWorkflow.Global.GoVersion = goVersion
}
ghworkflow.Render(cfg, sr)
}
// Render Renovate config
if cfg.Renovate.Enabled {
if cfg.Renovate.GoVersion == "" {
cfg.Renovate.GoVersion = sr.GoVersionMajorMinor
}
// TODO: checking on GoVersion is only an aid until we can properly detect rust applications
isApplicationRepo := sr.GoVersion == "" || len(cfg.Binaries) > 0
renovate.RenderConfig(cfg.Renovate, sr, cfg.Metadata.URL, isApplicationRepo)
}
}