1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-01-10 03:47:03 +02:00
goreleaser/pipeline/defaults/defaults.go

95 lines
2.0 KiB
Go
Raw Normal View History

2017-01-14 16:34:22 +02:00
package defaults
import (
"errors"
2017-01-14 18:29:01 +02:00
"io/ioutil"
"strings"
2017-01-14 16:34:22 +02:00
"github.com/goreleaser/releaser/context"
)
2017-01-14 19:16:29 +02:00
var defaultFiles = []string{"licence", "license", "readme", "changelog"}
2017-01-14 16:34:22 +02:00
// Pipe for brew deployment
type Pipe struct{}
2017-01-14 20:41:32 +02:00
// Description of the pipe
2017-01-14 19:14:35 +02:00
func (Pipe) Description() string {
return "Setting defaults..."
2017-01-14 16:34:22 +02:00
}
// Run the pipe
2017-01-14 20:41:32 +02:00
func (Pipe) Run(ctx *context.Context) error {
if ctx.Config.Repo == "" {
repo, err := remoteRepo()
ctx.Config.Repo = repo
if err != nil {
return errors.New("failed reading repo from Git: " + err.Error())
}
}
2017-01-14 20:29:30 +02:00
if ctx.Config.BinaryName == "" {
ctx.Config.BinaryName = strings.Split(ctx.Config.Repo, "/")[1]
}
2017-01-14 18:06:57 +02:00
if ctx.Config.Build.Main == "" {
ctx.Config.Build.Main = "main.go"
2017-01-14 16:34:22 +02:00
}
2017-01-14 18:06:57 +02:00
if len(ctx.Config.Build.Oses) == 0 {
ctx.Config.Build.Oses = []string{"linux", "darwin"}
2017-01-14 16:34:22 +02:00
}
2017-01-14 18:06:57 +02:00
if len(ctx.Config.Build.Arches) == 0 {
ctx.Config.Build.Arches = []string{"amd64", "386"}
2017-01-14 16:34:22 +02:00
}
2017-01-14 18:06:57 +02:00
if ctx.Config.Build.Ldflags == "" {
ctx.Config.Build.Ldflags = "-s -w"
2017-01-14 16:34:22 +02:00
}
2017-01-14 18:06:57 +02:00
if ctx.Config.Archive.NameTemplate == "" {
ctx.Config.Archive.NameTemplate = "{{.BinaryName}}_{{.Os}}_{{.Arch}}"
2017-01-14 16:34:22 +02:00
}
2017-01-14 18:06:57 +02:00
if ctx.Config.Archive.Format == "" {
ctx.Config.Archive.Format = "tar.gz"
2017-01-14 16:34:22 +02:00
}
2017-01-14 18:06:57 +02:00
if len(ctx.Config.Archive.Replacements) == 0 {
ctx.Config.Archive.Replacements = map[string]string{
2017-01-14 16:34:22 +02:00
"darwin": "Darwin",
"linux": "Linux",
"freebsd": "FreeBSD",
"openbsd": "OpenBSD",
"netbsd": "NetBSD",
"windows": "Windows",
"386": "i386",
"amd64": "x86_64",
}
}
2017-01-14 18:06:57 +02:00
if len(ctx.Config.Files) != 0 {
2017-01-14 20:41:32 +02:00
return nil
2017-01-14 16:34:22 +02:00
}
2017-01-14 18:38:48 +02:00
files, err := findFiles()
2017-01-14 18:29:01 +02:00
if err != nil {
2017-01-14 20:41:32 +02:00
return err
2017-01-14 16:34:22 +02:00
}
2017-01-14 18:29:01 +02:00
ctx.Config.Files = files
2017-01-14 20:41:32 +02:00
return nil
2017-01-14 16:34:22 +02:00
}
2017-01-14 18:38:48 +02:00
func findFiles() (files []string, err error) {
all, err := ioutil.ReadDir(".")
2017-01-14 18:29:01 +02:00
if err != nil {
2017-01-14 16:34:22 +02:00
return
}
2017-01-14 18:29:01 +02:00
for _, file := range all {
2017-01-14 18:38:48 +02:00
if accept(file.Name()) {
files = append(files, file.Name())
2017-01-14 18:29:01 +02:00
}
2017-01-14 16:34:22 +02:00
}
return
}
2017-01-14 18:38:48 +02:00
func accept(file string) bool {
for _, accepted := range defaultFiles {
2017-01-14 19:16:29 +02:00
if strings.HasPrefix(strings.ToLower(file), accepted) {
2017-01-14 18:38:48 +02:00
return true
}
}
return false
}