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

228 lines
6.6 KiB
Go
Raw Normal View History

2017-04-14 20:39:32 +02:00
// Package config contains the model and loader of the goreleaser configuration
// file.
2016-12-21 14:35:34 +02:00
package config
import (
2017-07-08 17:05:57 +02:00
"fmt"
2017-05-11 04:14:17 +02:00
"io"
2016-12-21 14:35:34 +02:00
"io/ioutil"
2017-05-18 14:02:02 +02:00
"os"
2017-07-08 17:05:57 +02:00
"strings"
2017-06-28 01:06:45 +02:00
"github.com/apex/log"
2017-07-08 17:05:57 +02:00
yaml "gopkg.in/yaml.v2"
2016-12-21 14:35:34 +02:00
)
2017-03-23 02:01:29 +02:00
// Repo represents any kind of repo (github, gitlab, etc)
type Repo struct {
2017-04-28 18:18:32 +02:00
Owner string `yaml:",omitempty"`
Name string `yaml:",omitempty"`
2017-07-08 17:05:57 +02:00
// Capture all undefined fields and should be empty after loading
XXX map[string]interface{} `yaml:",inline"`
2017-03-23 02:01:29 +02:00
}
2017-03-23 02:27:04 +02:00
// String of the repo, e.g. owner/name
2017-03-23 02:01:29 +02:00
func (r Repo) String() string {
return r.Owner + "/" + r.Name
}
// Homebrew contains the brew section
type Homebrew struct {
2017-04-28 18:18:32 +02:00
GitHub Repo `yaml:",omitempty"`
Folder string `yaml:",omitempty"`
Caveats string `yaml:",omitempty"`
Plist string `yaml:",omitempty"`
Install string `yaml:",omitempty"`
Dependencies []string `yaml:",omitempty"`
2017-07-16 17:33:41 +02:00
Test string `yaml:",omitempty"`
2017-04-28 18:18:32 +02:00
Conflicts []string `yaml:",omitempty"`
Description string `yaml:",omitempty"`
Homepage string `yaml:",omitempty"`
2017-07-08 17:05:57 +02:00
// Capture all undefined fields and should be empty after loading
XXX map[string]interface{} `yaml:",inline"`
2016-12-21 14:35:34 +02:00
}
// Hooks define actions to run before and/or after something
type Hooks struct {
2017-04-28 18:18:32 +02:00
Pre string `yaml:",omitempty"`
Post string `yaml:",omitempty"`
2017-07-08 17:05:57 +02:00
// Capture all undefined fields and should be empty after loading
XXX map[string]interface{} `yaml:",inline"`
}
2017-04-27 01:08:25 +02:00
// IgnoredBuild represents a build ignored by the user
type IgnoredBuild struct {
Goos, Goarch, Goarm string
2017-07-08 17:05:57 +02:00
// Capture all undefined fields and should be empty after loading
XXX map[string]interface{} `yaml:",inline"`
2017-04-27 01:08:25 +02:00
}
// Build contains the build configuration section
type Build struct {
2017-05-01 17:04:31 +02:00
Goos []string `yaml:",omitempty"`
Goarch []string `yaml:",omitempty"`
Goarm []string `yaml:",omitempty"`
Ignore []IgnoredBuild `yaml:",omitempty"`
Main string `yaml:",omitempty"`
Ldflags string `yaml:",omitempty"`
Flags string `yaml:",omitempty"`
Binary string `yaml:",omitempty"`
Hooks Hooks `yaml:",omitempty"`
2017-05-11 15:43:25 +02:00
Env []string `yaml:",omitempty"`
2017-07-08 17:05:57 +02:00
// Capture all undefined fields and should be empty after loading
XXX map[string]interface{} `yaml:",inline"`
2016-12-21 15:37:31 +02:00
}
// FormatOverride is used to specify a custom format for a specific GOOS.
type FormatOverride struct {
2017-04-28 18:18:32 +02:00
Goos string `yaml:",omitempty"`
Format string `yaml:",omitempty"`
2017-07-08 17:05:57 +02:00
// Capture all undefined fields and should be empty after loading
XXX map[string]interface{} `yaml:",inline"`
}
// Archive config used for the archive
type Archive struct {
2017-04-28 18:18:32 +02:00
Format string `yaml:",omitempty"`
FormatOverrides []FormatOverride `yaml:"format_overrides,omitempty"`
NameTemplate string `yaml:"name_template,omitempty"`
2017-04-28 18:18:32 +02:00
Replacements map[string]string `yaml:",omitempty"`
Files []string `yaml:",omitempty"`
2017-07-08 17:05:57 +02:00
// Capture all undefined fields and should be empty after loading
XXX map[string]interface{} `yaml:",inline"`
}
// Release config used for the GitHub release
type Release struct {
2017-04-28 18:18:32 +02:00
GitHub Repo `yaml:",omitempty"`
Draft bool `yaml:",omitempty"`
2017-07-08 17:05:57 +02:00
// Capture all undefined fields and should be empty after loading
XXX map[string]interface{} `yaml:",inline"`
2017-01-14 18:06:57 +02:00
}
2017-01-30 01:55:32 +02:00
// FPM config
type FPM struct {
2017-04-28 18:18:32 +02:00
Formats []string `yaml:",omitempty"`
Dependencies []string `yaml:",omitempty"`
Conflicts []string `yaml:",omitempty"`
Vendor string `yaml:",omitempty"`
Homepage string `yaml:",omitempty"`
Maintainer string `yaml:",omitempty"`
Description string `yaml:",omitempty"`
License string `yaml:",omitempty"`
2017-07-08 17:05:57 +02:00
// Capture all undefined fields and should be empty after loading
XXX map[string]interface{} `yaml:",inline"`
2017-01-30 01:55:32 +02:00
}
2017-04-29 12:49:22 +02:00
// Snapshot config
type Snapshot struct {
NameTemplate string `yaml:"name_template,omitempty"`
2017-07-08 17:05:57 +02:00
// Capture all undefined fields and should be empty after loading
XXX map[string]interface{} `yaml:",inline"`
2017-01-30 01:55:32 +02:00
}
// Project includes all project configuration
type Project struct {
2017-07-02 03:42:10 +02:00
ProjectName string `yaml:"project_name,omitempty"`
Release Release `yaml:",omitempty"`
Brew Homebrew `yaml:",omitempty"`
Builds []Build `yaml:",omitempty"`
Archive Archive `yaml:",omitempty"`
FPM FPM `yaml:",omitempty"`
Snapshot Snapshot `yaml:",omitempty"`
2017-03-26 02:29:38 +02:00
2017-07-02 02:27:30 +02:00
// this is a hack ¯\_(ツ)_/¯
SingleBuild Build `yaml:"build,omitempty"`
2017-03-26 02:31:41 +02:00
// test only property indicating the path to the dist folder
2017-03-26 02:31:16 +02:00
Dist string `yaml:"-"`
2017-07-08 17:05:57 +02:00
// Capture all undefined fields and should be empty after loading
XXX map[string]interface{} `yaml:",inline"`
2016-12-21 14:35:34 +02:00
}
2016-12-30 16:41:59 +02:00
// Load config file
func Load(file string) (config Project, err error) {
2017-05-18 14:02:02 +02:00
f, err := os.Open(file)
2016-12-21 14:35:34 +02:00
if err != nil {
2017-05-18 14:02:02 +02:00
return
2016-12-21 14:35:34 +02:00
}
2017-07-05 04:51:45 +02:00
log.WithField("file", file).Info("loading config file")
2017-05-18 14:02:02 +02:00
return LoadReader(f)
}
2017-05-11 04:14:17 +02:00
2017-05-18 14:02:02 +02:00
// LoadReader config via io.Reader
2017-05-11 04:14:17 +02:00
func LoadReader(fd io.Reader) (config Project, err error) {
data, err := ioutil.ReadAll(fd)
if err != nil {
2017-05-11 04:35:44 +02:00
return config, err
}
err = yaml.Unmarshal(data, &config)
2017-07-02 02:27:30 +02:00
log.WithField("config", config).Debug("loaded config file")
2017-07-08 17:05:57 +02:00
err = checkOverflows(config)
2017-05-11 04:35:44 +02:00
return
2017-05-11 04:14:17 +02:00
}
2017-07-08 17:05:57 +02:00
func checkOverflows(config Project) error {
var checker = &overflowChecker{}
checker.check(config.XXX, "")
checker.check(config.Archive.XXX, "archive")
for i, ov := range config.Archive.FormatOverrides {
checker.check(ov.XXX, fmt.Sprintf("archive.format_overrides[%d]", i))
}
checker.check(config.Brew.XXX, "brew")
checker.check(config.Brew.GitHub.XXX, "brew.github")
for i, build := range config.Builds {
checker.check(build.XXX, fmt.Sprintf("builds[%d]", i))
checker.check(build.Hooks.XXX, fmt.Sprintf("builds[%d].hooks", i))
for j, ignored := range build.Ignore {
checker.check(ignored.XXX, fmt.Sprintf("builds[%d].ignored_builds[%d]", i, j))
}
}
checker.check(config.FPM.XXX, "fpm")
checker.check(config.Release.XXX, "release")
checker.check(config.Release.GitHub.XXX, "release.github")
checker.check(config.SingleBuild.XXX, "build")
checker.check(config.SingleBuild.Hooks.XXX, "builds.hooks")
for i, ignored := range config.SingleBuild.Ignore {
checker.check(ignored.XXX, fmt.Sprintf("builds.ignored_builds[%d]", i))
}
checker.check(config.Snapshot.XXX, "snapshot")
return checker.err()
}
type overflowChecker struct {
fields []string
}
func (o *overflowChecker) check(m map[string]interface{}, ctx string) {
for k := range m {
var key = fmt.Sprintf("%s.%s", ctx, k)
if ctx == "" {
key = k
}
o.fields = append(o.fields, key)
}
}
func (o *overflowChecker) err() error {
if len(o.fields) == 0 {
return nil
}
return fmt.Errorf(
"unknown fields in the config file: %s",
strings.Join(o.fields, ", "),
)
}