2019-05-15 22:09:52 +02:00
|
|
|
// Package config provides defimition of .release.yml and read method
|
|
|
|
|
package config
|
|
|
|
|
|
|
|
|
|
import (
|
2019-06-13 19:31:56 +02:00
|
|
|
"fmt"
|
2019-05-15 22:09:52 +02:00
|
|
|
"io/ioutil"
|
2019-06-13 19:31:56 +02:00
|
|
|
"os"
|
|
|
|
|
"strings"
|
2019-05-15 22:09:52 +02:00
|
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
"gopkg.in/yaml.v2"
|
|
|
|
|
)
|
|
|
|
|
|
2019-06-13 19:31:56 +02:00
|
|
|
// List of all supported git providers
|
|
|
|
|
var gitPorviders = map[string]string{"GitHub": "", "GitLab": ""}
|
|
|
|
|
|
2019-05-15 22:09:52 +02:00
|
|
|
// ChangelogConfig struct
|
|
|
|
|
type ChangelogConfig struct {
|
2019-05-25 18:10:24 +02:00
|
|
|
PrintAll bool `yaml:"printAll,omitempty"`
|
2019-05-15 22:09:52 +02:00
|
|
|
Template string `yaml:"template,omitempty"`
|
|
|
|
|
TemplatePath string `yaml:"templatePath,omitempty"`
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-13 19:31:56 +02:00
|
|
|
// GitProvider struct
|
|
|
|
|
type GitProvider struct {
|
|
|
|
|
Name string `yaml:"name"`
|
2019-06-05 22:22:06 +02:00
|
|
|
URL string `yaml:"url"`
|
|
|
|
|
User string `yaml:"user"`
|
|
|
|
|
AccessToken string `yaml:"accessToken"`
|
2019-05-15 22:09:52 +02:00
|
|
|
}
|
|
|
|
|
|
2019-05-28 22:12:00 +02:00
|
|
|
type Asset struct {
|
|
|
|
|
Name string `yaml:"name"`
|
|
|
|
|
Compress bool `yaml:"compress"`
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-15 22:09:52 +02:00
|
|
|
// ReleaseConfig struct
|
|
|
|
|
type ReleaseConfig struct {
|
2019-06-05 22:22:06 +02:00
|
|
|
CommitFormat string `yaml:"commitFormat"`
|
|
|
|
|
Branch map[string]string `yaml:"branch"`
|
|
|
|
|
Changelog ChangelogConfig `yaml:"changelog,omitempty"`
|
|
|
|
|
Release string `yaml:"release,omitempty"`
|
2019-06-13 19:31:56 +02:00
|
|
|
GitProvider GitProvider `yaml:"provider,omitempty"`
|
2019-06-05 22:22:06 +02:00
|
|
|
Assets []Asset `yaml:"assets"`
|
|
|
|
|
IsPreRelease, IsDraft bool
|
2019-05-15 22:09:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Read ReleaseConfig
|
|
|
|
|
func Read(configPath string) (*ReleaseConfig, error) {
|
|
|
|
|
|
|
|
|
|
content, err := ioutil.ReadFile(configPath)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return &ReleaseConfig{}, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var releaseConfig ReleaseConfig
|
|
|
|
|
err = yaml.Unmarshal(content, &releaseConfig)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return &ReleaseConfig{}, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
log.Debugf("Found config %+v", releaseConfig)
|
|
|
|
|
|
2019-06-13 19:31:56 +02:00
|
|
|
releaseConfig, err = checkProvider(releaseConfig)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return &ReleaseConfig{}, err
|
|
|
|
|
}
|
2019-06-10 16:24:44 +02:00
|
|
|
return &releaseConfig, nil
|
2019-05-15 22:09:52 +02:00
|
|
|
}
|
2019-06-13 19:31:56 +02:00
|
|
|
|
|
|
|
|
func checkProvider(config ReleaseConfig) (ReleaseConfig, error) {
|
|
|
|
|
if config.GitProvider != (GitProvider{}) {
|
|
|
|
|
if _, ok := gitPorviders[config.GitProvider.Name]; !ok {
|
|
|
|
|
return ReleaseConfig{}, fmt.Errorf("config: provider: configured provider %s is not supported", config.GitProvider.Name)
|
|
|
|
|
}
|
|
|
|
|
envName := fmt.Sprintf("%s_ACCESS_TOKEN", strings.ToUpper(config.GitProvider.Name))
|
|
|
|
|
|
|
|
|
|
token, isSet := os.LookupEnv(envName)
|
|
|
|
|
if !isSet {
|
|
|
|
|
return ReleaseConfig{}, fmt.Errorf("config: Can not find environment variable %s", token)
|
|
|
|
|
}
|
|
|
|
|
config.GitProvider.AccessToken = token
|
|
|
|
|
}
|
|
|
|
|
return config, nil
|
|
|
|
|
}
|