diff --git a/internal/releaser/github.go b/internal/releaser/github.go index 475b19a..62197d7 100644 --- a/internal/releaser/github.go +++ b/internal/releaser/github.go @@ -10,7 +10,7 @@ import ( ) // GITHUB identifer for github interface -const GITHUB = "github" +const GITHUB = "GitHub" // GitHubReleaser type struct type GitHubReleaser struct { diff --git a/internal/releaser/releaser.go b/internal/releaser/releaser.go index 4541a50..3e095bb 100644 --- a/internal/releaser/releaser.go +++ b/internal/releaser/releaser.go @@ -28,12 +28,12 @@ func New(c *config.ReleaseConfig) *Releasers { } //GetReleaser returns an initialized releaser -func (r *Releasers) GetReleaser(releaserType string) (Releaser, error) { - switch releaserType { +func (r *Releasers) GetReleaser() (Releaser, error) { + switch r.config.GitProvider.Name { case GITHUB: return NewGitHubReleaser(r.config), nil } - return nil, fmt.Errorf("Could not initialize a releaser from this type: %s", releaserType) + return nil, fmt.Errorf("Could not initialize a releaser from this type: %s", r.config.GitProvider.Name) } // tbd. http helper function diff --git a/pkg/config/config.go b/pkg/config/config.go index 70fdb80..b0a35f5 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -2,12 +2,18 @@ package config import ( + "fmt" "io/ioutil" + "os" + "strings" log "github.com/sirupsen/logrus" "gopkg.in/yaml.v2" ) +// List of all supported git providers +var gitPorviders = map[string]string{"GitHub": "", "GitLab": ""} + // ChangelogConfig struct type ChangelogConfig struct { PrintAll bool `yaml:"printAll,omitempty"` @@ -15,15 +21,9 @@ type ChangelogConfig struct { TemplatePath string `yaml:"templatePath,omitempty"` } -// GithubConfig struct -type GithubConfig struct { - URL string `yaml:"url"` - User string `yaml:"user"` - AccessToken string `yaml:"accessToken"` -} - -// GitlabConfig struct -type GitlabConfig struct { +// GitProvider struct +type GitProvider struct { + Name string `yaml:"name"` URL string `yaml:"url"` User string `yaml:"user"` AccessToken string `yaml:"accessToken"` @@ -40,8 +40,7 @@ type ReleaseConfig struct { Branch map[string]string `yaml:"branch"` Changelog ChangelogConfig `yaml:"changelog,omitempty"` Release string `yaml:"release,omitempty"` - Github GitlabConfig `yaml:"github,omitempty"` - Gitlab GitlabConfig `yaml:"gitlab,omitempty"` + GitProvider GitProvider `yaml:"provider,omitempty"` Assets []Asset `yaml:"assets"` IsPreRelease, IsDraft bool } @@ -62,5 +61,25 @@ func Read(configPath string) (*ReleaseConfig, error) { log.Debugf("Found config %+v", releaseConfig) + releaseConfig, err = checkProvider(releaseConfig) + if err != nil { + return &ReleaseConfig{}, err + } return &releaseConfig, nil } + +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 +}