1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-01-26 04:22:05 +02:00
goreleaser/config/config.go

156 lines
2.9 KiB
Go
Raw Normal View History

2016-12-21 10:35:34 -02:00
package config
import (
2016-12-28 23:40:59 -02:00
"errors"
2016-12-21 10:35:34 -02:00
"io/ioutil"
"log"
2016-12-21 10:35:34 -02:00
"os"
"path"
"path/filepath"
2016-12-21 10:35:34 -02:00
2016-12-30 09:27:35 -02:00
"github.com/goreleaser/releaser/config/git"
2016-12-21 10:35:34 -02:00
yaml "gopkg.in/yaml.v1"
)
var (
emptyBrew = Homebrew{}
filePatterns = []string{"LICENCE.*", "LICENSE.*", "README.*"}
)
2016-12-21 10:35:34 -02:00
2016-12-30 12:41:59 -02:00
// Homebrew contains the brew section
type Homebrew struct {
2016-12-29 13:14:52 -02:00
Repo string
Token string
Caveats string
2016-12-21 10:35:34 -02:00
}
2016-12-30 12:41:59 -02:00
// BuildConfig contains the build configuration section
2016-12-21 11:37:31 -02:00
type BuildConfig struct {
Oses []string
Arches []string
2016-12-29 12:37:11 -02:00
Main string
2016-12-21 11:37:31 -02:00
}
2016-12-30 12:41:59 -02:00
// GitInfo includes tags and diffs used in some point
2016-12-30 09:27:35 -02:00
type GitInfo struct {
CurrentTag string
PreviousTag string
Diff string
}
2016-12-30 12:41:59 -02:00
// ProjectConfig includes all project configuration
2016-12-21 10:35:34 -02:00
type ProjectConfig struct {
Repo string
2016-12-28 23:50:37 -02:00
BinaryName string `yaml:"binary_name"`
Files []string
2016-12-30 12:41:59 -02:00
Brew Homebrew
2016-12-21 10:35:34 -02:00
Token string
2016-12-21 11:37:31 -02:00
Build BuildConfig
2016-12-30 09:27:35 -02:00
Git GitInfo `yaml:"_"`
2016-12-21 10:35:34 -02:00
}
2016-12-30 12:41:59 -02:00
// Load config file
2016-12-21 10:35:34 -02:00
func Load(file string) (config ProjectConfig, err error) {
data, err := ioutil.ReadFile(file)
if err != nil {
return config, err
}
err = yaml.Unmarshal(data, &config)
2016-12-28 23:35:45 -02:00
config = fix(config)
2016-12-30 09:27:35 -02:00
config, err = fillGitData(config)
if err != nil {
return config, err
}
2016-12-28 23:35:45 -02:00
if config.BinaryName == "" {
2016-12-28 23:46:18 -02:00
return config, errors.New("missing binary_name")
2016-12-28 23:35:45 -02:00
}
if config.Repo == "" {
2016-12-28 23:46:18 -02:00
return config, errors.New("missing repo")
2016-12-28 23:35:45 -02:00
}
return config, err
2016-12-21 10:35:34 -02:00
}
func fix(config ProjectConfig) ProjectConfig {
2016-12-28 23:50:37 -02:00
if len(config.Files) == 0 {
2016-12-30 17:31:06 +00:00
config.Files = []string{}
for _, pattern := range filePatterns {
matches, err := globPath(pattern)
if err != nil {
log.Fatalf("Error searching for %q: %v", pattern, err)
2016-12-30 17:31:06 +00:00
}
config.Files = append(config.Files, matches...)
2016-12-21 10:35:34 -02:00
}
}
if config.Token == "" {
config.Token = os.Getenv("GITHUB_TOKEN")
}
if config.Brew != emptyBrew && config.Brew.Token == "" {
config.Brew.Token = config.Token
}
2016-12-29 14:03:17 -02:00
if config.Build.Main == "" {
config.Build.Main = "main.go"
}
2016-12-21 11:37:31 -02:00
if len(config.Build.Oses) == 0 {
config.Build.Oses = []string{"linux", "darwin"}
}
if len(config.Build.Arches) == 0 {
config.Build.Arches = []string{"amd64", "386"}
}
2016-12-30 17:31:06 +00:00
2016-12-21 10:35:34 -02:00
return config
}
2016-12-30 09:27:35 -02:00
func fillGitData(config ProjectConfig) (ProjectConfig, error) {
tag, err := git.CurrentTag()
if err != nil {
return config, err
}
previous, err := git.PreviousTag(tag)
if err != nil {
return config, err
}
log, err := git.Log(previous, tag)
if err != nil {
return config, err
}
config.Git.CurrentTag = tag
config.Git.PreviousTag = previous
config.Git.Diff = log
return config, nil
}
2016-12-21 14:42:23 -02:00
func contains(s string, ss []string) bool {
2016-12-21 10:35:34 -02:00
for _, sx := range ss {
if sx == s {
2016-12-21 14:42:23 -02:00
return true
2016-12-21 10:35:34 -02:00
}
}
2016-12-21 14:42:23 -02:00
return false
2016-12-21 10:35:34 -02:00
}
func globPath(p string) (m []string, err error) {
var cwd string
var dirs []string
if cwd, err = os.Getwd(); err != nil {
return
}
fp := path.Join(cwd, p)
if dirs, err = filepath.Glob(fp); err != nil {
return
}
// Normalise to avoid nested dirs in tarball
for _, dir := range dirs {
_, f := filepath.Split(dir)
m = append(m, f)
}
return
}