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

166 lines
3.2 KiB
Go
Raw Normal View History

2016-12-21 14:35:34 +02:00
package config
import (
2016-12-29 03:40:59 +02:00
"errors"
2016-12-21 14:35:34 +02:00
"io/ioutil"
"os"
"path"
"path/filepath"
2016-12-21 14:35:34 +02:00
2016-12-30 13:27:35 +02:00
"github.com/goreleaser/releaser/config/git"
2016-12-21 14:35:34 +02:00
yaml "gopkg.in/yaml.v1"
)
2017-01-02 21:42:00 +02:00
var filePatterns = []string{"LICENCE*", "LICENSE*", "README*", "CHANGELOG*"}
2016-12-21 14:35:34 +02:00
2016-12-30 16:41:59 +02:00
// Homebrew contains the brew section
type Homebrew struct {
2016-12-29 17:14:52 +02:00
Repo string
2017-01-03 11:43:55 +02:00
Token string `yaml:"-"`
2016-12-29 17:14:52 +02:00
Caveats string
2016-12-21 14:35:34 +02:00
}
2016-12-30 16:41:59 +02:00
// BuildConfig contains the build configuration section
2016-12-21 15:37:31 +02:00
type BuildConfig struct {
Oses []string
Arches []string
2016-12-29 16:37:11 +02:00
Main string
2016-12-21 15:37:31 +02:00
}
2016-12-30 16:41:59 +02:00
// GitInfo includes tags and diffs used in some point
2016-12-30 13:27:35 +02:00
type GitInfo struct {
CurrentTag string
PreviousTag string
Diff string
}
2017-01-06 17:30:02 +02:00
// ArchiveConfig
type ArchiveConfig struct {
Format string
}
2016-12-30 16:41:59 +02:00
// ProjectConfig includes all project configuration
2016-12-21 14:35:34 +02:00
type ProjectConfig struct {
2017-01-04 14:13:49 +02:00
Repo string
BinaryName string `yaml:"binary_name"`
Files []string
Brew Homebrew
Token string `yaml:"-"`
Build BuildConfig
Git GitInfo `yaml:"-"`
2017-01-06 15:42:58 +02:00
NameTemplate string `yaml:"name_template"`
Archive ArchiveConfig
2016-12-21 14:35:34 +02:00
}
2016-12-30 16:41:59 +02:00
// Load config file
2016-12-21 14:35:34 +02:00
func Load(file string) (config ProjectConfig, err error) {
data, err := ioutil.ReadFile(file)
if err != nil {
return config, err
}
2017-01-02 14:45:05 +02:00
if err := yaml.Unmarshal(data, &config); err != nil {
return config, err
}
2016-12-31 14:03:25 +02:00
config.fillBasicData()
if err := config.fillFiles(); err != nil {
return config, err
}
if err := config.fillGitData(); err != nil {
2016-12-30 13:27:35 +02:00
return config, err
}
2016-12-31 17:20:22 +02:00
return config, config.validate()
2016-12-31 14:03:25 +02:00
}
2016-12-31 17:20:22 +02:00
func (config *ProjectConfig) validate() (err error) {
2016-12-29 03:35:45 +02:00
if config.BinaryName == "" {
2016-12-31 14:03:25 +02:00
return errors.New("missing binary_name")
2016-12-29 03:35:45 +02:00
}
if config.Repo == "" {
2016-12-31 14:03:25 +02:00
return errors.New("missing repo")
2016-12-29 03:35:45 +02:00
}
2016-12-31 14:03:25 +02:00
return
2016-12-21 14:35:34 +02:00
}
2016-12-31 14:03:25 +02:00
func (config *ProjectConfig) fillFiles() (err error) {
if len(config.Files) != 0 {
return
}
config.Files = []string{}
for _, pattern := range filePatterns {
matches, err := globPath(pattern)
if err != nil {
return err
2016-12-21 14:35:34 +02:00
}
2016-12-31 14:03:25 +02:00
config.Files = append(config.Files, matches...)
2016-12-21 14:35:34 +02:00
}
2016-12-31 14:03:25 +02:00
return
}
func (config *ProjectConfig) fillBasicData() {
2016-12-21 14:35:34 +02:00
if config.Token == "" {
config.Token = os.Getenv("GITHUB_TOKEN")
}
2017-01-02 21:42:00 +02:00
if config.Brew.Repo != "" {
2016-12-21 14:35:34 +02:00
config.Brew.Token = config.Token
}
2016-12-29 18:03:17 +02:00
if config.Build.Main == "" {
config.Build.Main = "main.go"
}
2016-12-21 15: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"}
}
2017-01-04 14:13:49 +02:00
if config.NameTemplate == "" {
config.NameTemplate = "{{.BinaryName}}_{{.Os}}_{{.Arch}}"
}
2017-01-06 17:30:02 +02:00
if config.Archive.Format == "" {
config.Archive.Format = "tar.gz"
2017-01-04 14:13:49 +02:00
}
2016-12-21 14:35:34 +02:00
}
2016-12-31 14:03:25 +02:00
func (config *ProjectConfig) fillGitData() (err error) {
2016-12-30 13:27:35 +02:00
tag, err := git.CurrentTag()
if err != nil {
2016-12-31 14:03:25 +02:00
return
2016-12-30 13:27:35 +02:00
}
previous, err := git.PreviousTag(tag)
if err != nil {
2016-12-31 14:03:25 +02:00
return
2016-12-30 13:27:35 +02:00
}
log, err := git.Log(previous, tag)
if err != nil {
2016-12-31 14:03:25 +02:00
return
2016-12-30 13:27:35 +02:00
}
config.Git.CurrentTag = tag
config.Git.PreviousTag = previous
config.Git.Diff = log
2016-12-31 14:03:25 +02:00
return
2016-12-21 14: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
}