1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-01-10 03:47:03 +02:00
goreleaser/config/config.go
Carlos Alexandro Becker 759bb2918e
config
2017-01-06 12:53:48 -02:00

156 lines
2.8 KiB
Go

package config
import (
"errors"
"io/ioutil"
"os"
"path"
"path/filepath"
"github.com/goreleaser/releaser/config/git"
yaml "gopkg.in/yaml.v1"
)
var filePatterns = []string{"LICENCE*", "LICENSE*", "README*", "CHANGELOG*"}
// Homebrew contains the brew section
type Homebrew struct {
Repo string
Token string `yaml:"-"`
Caveats string
}
// BuildConfig contains the build configuration section
type BuildConfig struct {
Oses []string
Arches []string
Main string
}
// GitInfo includes tags and diffs used in some point
type GitInfo struct {
CurrentTag string
PreviousTag string
Diff string
}
// ArchiveConfig
type ArchiveConfig struct {
Format string
}
// ProjectConfig includes all project configuration
type ProjectConfig struct {
Repo string
BinaryName string `yaml:"binary_name"`
Files []string
Brew Homebrew
Token string `yaml:"-"`
Build BuildConfig
Git GitInfo `yaml:"-"`
Archive ArchiveConfig
}
var defaults = ProjectConfig{
Token: os.Getenv("GITHUB_TOKEN"),
Build: BuildConfig{
Main: "main.go",
Oses: []string{"linux", "darwin"},
Arches: []string{"amd64", "386"},
},
Brew: Homebrew{
Token: os.Getenv("GITHUB_TOKEN"),
},
Archive: ArchiveConfig{
Format: "tar.gz",
},
}
// Load config file
func Load(file string) (ProjectConfig, error) {
var config = defaults
data, err := ioutil.ReadFile(file)
if err != nil {
return config, err
}
if err := yaml.Unmarshal(data, &config); err != nil {
return config, err
}
if err := config.fillFiles(); err != nil {
return config, err
}
if err := config.fillGitData(); err != nil {
return config, err
}
return config, config.validate()
}
func (config *ProjectConfig) validate() (err error) {
if config.BinaryName == "" {
return errors.New("missing binary_name")
}
if config.Repo == "" {
return errors.New("missing repo")
}
return
}
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
}
config.Files = append(config.Files, matches...)
}
return
}
func (config *ProjectConfig) fillGitData() (err error) {
tag, err := git.CurrentTag()
if err != nil {
return
}
previous, err := git.PreviousTag(tag)
if err != nil {
return
}
log, err := git.Log(previous, tag)
if err != nil {
return
}
config.Git.CurrentTag = tag
config.Git.PreviousTag = previous
config.Git.Diff = log
return
}
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
}