forked from goreleaser/goreleaser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
217 lines (200 loc) · 6.32 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
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
package main
import (
"fmt"
"io/ioutil"
"os"
"strings"
"time"
"github.com/alecthomas/kingpin"
"github.com/apex/log"
"github.com/apex/log/handlers/cli"
"github.com/caarlos0/ctrlc"
"github.com/fatih/color"
"github.com/goreleaser/goreleaser/internal/pipe"
"github.com/goreleaser/goreleaser/internal/pipeline"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/pkg/context"
)
var (
version = "dev"
commit = "none"
date = "unknown"
)
type releaseOptions struct {
Config string
ReleaseNotes string
Snapshot bool
SkipPublish bool
SkipSign bool
SkipValidate bool
RmDist bool
Debug bool
Parallelism int
Timeout time.Duration
}
func init() {
log.SetHandler(cli.Default)
}
func main() {
fmt.Println()
defer fmt.Println()
var app = kingpin.New("goreleaser", "Deliver Go binaries as fast and easily as possible")
var initCmd = app.Command("init", "Generates a .goreleaser.yml file").Alias("i")
var releaseCmd = app.Command("release", "Releases the current project").Alias("r").Default()
var config = releaseCmd.Flag("config", "Load configuration from file").Short('c').Short('f').PlaceHolder(".goreleaser.yml").String()
var releaseNotes = releaseCmd.Flag("release-notes", "Load custom release notes from a markdown file").PlaceHolder("notes.md").String()
var snapshot = releaseCmd.Flag("snapshot", "Generate an unversioned snapshot release, skipping all validations and without publishing any artifacts").Bool()
var skipPublish = releaseCmd.Flag("skip-publish", "Generates all artifacts but does not publish them anywhere").Bool()
var skipSign = releaseCmd.Flag("skip-sign", "Skips signing the artifacts").Bool()
var skipValidate = releaseCmd.Flag("skip-validate", "Skips all git sanity checks").Bool()
var rmDist = releaseCmd.Flag("rm-dist", "Remove the dist folder before building").Bool()
var parallelism = releaseCmd.Flag("parallelism", "Amount of slow tasks to do in concurrently").Short('p').Default("4").Int() // TODO: use runtime.NumCPU here?
var debug = releaseCmd.Flag("debug", "Enable debug mode").Bool()
var timeout = releaseCmd.Flag("timeout", "Timeout to the entire release process").Default("30m").Duration()
app.Version(fmt.Sprintf("%v, commit %v, built at %v", version, commit, date))
app.VersionFlag.Short('v')
app.HelpFlag.Short('h')
switch kingpin.MustParse(app.Parse(os.Args[1:])) {
case initCmd.FullCommand():
var filename = ".goreleaser.yml"
if err := initProject(filename); err != nil {
log.WithError(err).Error("failed to init project")
terminate(1)
return
}
log.WithField("file", filename).Info("config created; please edit accordingly to your needs")
case releaseCmd.FullCommand():
start := time.Now()
log.Infof(color.New(color.Bold).Sprintf("releasing using goreleaser %s...", version))
var options = releaseOptions{
Config: *config,
ReleaseNotes: *releaseNotes,
Snapshot: *snapshot,
SkipPublish: *skipPublish,
SkipValidate: *skipValidate,
SkipSign: *skipSign,
RmDist: *rmDist,
Parallelism: *parallelism,
Debug: *debug,
Timeout: *timeout,
}
if err := releaseProject(options); err != nil {
log.WithError(err).Errorf(color.New(color.Bold).Sprintf("release failed after %0.2fs", time.Since(start).Seconds()))
terminate(1)
return
}
log.Infof(color.New(color.Bold).Sprintf("release succeeded after %0.2fs", time.Since(start).Seconds()))
}
}
func terminate(status int) {
os.Exit(status)
}
func releaseProject(options releaseOptions) error {
if options.Debug {
log.SetLevel(log.DebugLevel)
}
cfg, err := loadConfig(options.Config)
if err != nil {
return err
}
ctx, cancel := context.NewWithTimeout(cfg, options.Timeout)
defer cancel()
ctx.Parallelism = options.Parallelism
ctx.Debug = options.Debug
log.Debugf("parallelism: %v", ctx.Parallelism)
if options.ReleaseNotes != "" {
bts, err := ioutil.ReadFile(options.ReleaseNotes)
if err != nil {
return err
}
log.WithField("file", options.ReleaseNotes).Info("loaded custom release notes")
log.WithField("file", options.ReleaseNotes).Debugf("custom release notes: \n%s", string(bts))
ctx.ReleaseNotes = string(bts)
}
ctx.Snapshot = options.Snapshot
ctx.SkipPublish = ctx.Snapshot || options.SkipPublish
ctx.SkipValidate = ctx.Snapshot || options.SkipValidate
ctx.SkipSign = options.SkipSign
ctx.RmDist = options.RmDist
return doRelease(ctx)
}
func doRelease(ctx *context.Context) error {
defer func() { cli.Default.Padding = 3 }()
var release = func() error {
for _, pipe := range pipeline.Pipeline {
cli.Default.Padding = 3
log.Infof(color.New(color.Bold).Sprint(strings.ToUpper(pipe.String())))
cli.Default.Padding = 6
if err := handle(pipe.Run(ctx)); err != nil {
return err
}
}
return nil
}
return ctrlc.Default.Run(ctx, release)
}
func handle(err error) error {
if err == nil {
return nil
}
if pipe.IsSkip(err) {
log.WithField("reason", err.Error()).Warn("skipped")
return nil
}
return err
}
// InitProject creates an example goreleaser.yml in the current directory
func initProject(filename string) error {
if _, err := os.Stat(filename); !os.IsNotExist(err) {
if err != nil {
return err
}
return fmt.Errorf("%s already exists", filename)
}
log.Infof(color.New(color.Bold).Sprintf("Generating %s file", filename))
return ioutil.WriteFile(filename, []byte(exampleConfig), 0644)
}
func loadConfig(path string) (config.Project, error) {
if path != "" {
return config.Load(path)
}
for _, f := range [4]string{
".goreleaser.yml",
".goreleaser.yaml",
"goreleaser.yml",
"goreleaser.yaml",
} {
proj, err := config.Load(f)
if err != nil && os.IsNotExist(err) {
continue
}
return proj, err
}
// the user didn't specified a config file and the known files
// doest not exist, so, return an empty config and a nil err.
log.Warn("could not load config, using defaults")
return config.Project{}, nil
}
var exampleConfig = `# This is an example goreleaser.yaml file with some sane defaults.
# Make sure to check the documentation at http://goreleaser.com
builds:
- env:
- CGO_ENABLED=0
archive:
replacements:
darwin: Darwin
linux: Linux
windows: Windows
386: i386
amd64: x86_64
checksum:
name_template: 'checksums.txt'
snapshot:
name_template: "{{ .Tag }}-next"
changelog:
sort: asc
filters:
exclude:
- '^docs:'
- '^test:'
`