2017-04-14 20:39:32 +02:00
|
|
|
// Package env implements the Pipe interface providing validation of
|
|
|
|
// missing environment variables needed by the release process.
|
2017-01-14 18:06:57 +02:00
|
|
|
package env
|
|
|
|
|
|
|
|
import (
|
2018-02-03 20:51:19 +02:00
|
|
|
"bufio"
|
2017-01-14 18:06:57 +02:00
|
|
|
"os"
|
|
|
|
|
2020-04-09 15:28:58 +02:00
|
|
|
"github.com/apex/log"
|
2018-08-15 04:50:20 +02:00
|
|
|
"github.com/goreleaser/goreleaser/pkg/context"
|
2018-02-03 04:06:48 +02:00
|
|
|
homedir "github.com/mitchellh/go-homedir"
|
|
|
|
"github.com/pkg/errors"
|
2017-01-14 18:06:57 +02:00
|
|
|
)
|
|
|
|
|
2019-08-26 09:31:38 +02:00
|
|
|
// ErrMissingToken indicates an error when GITHUB_TOKEN, GITLAB_TOKEN and GITEA_TOKEN are all missing in the environment
|
|
|
|
var ErrMissingToken = errors.New("missing GITHUB_TOKEN, GITLAB_TOKEN and GITEA_TOKEN")
|
2019-06-29 16:02:40 +02:00
|
|
|
|
|
|
|
// ErrMultipleTokens indicates that multiple tokens are defined. ATM only one of them if allowed
|
|
|
|
// See https://github.com/goreleaser/goreleaser/pull/809
|
2019-08-26 09:31:38 +02:00
|
|
|
var ErrMultipleTokens = errors.New("multiple tokens defined. Only one is allowed")
|
2017-01-14 18:06:57 +02:00
|
|
|
|
|
|
|
// Pipe for env
|
|
|
|
type Pipe struct{}
|
|
|
|
|
2017-12-02 23:53:19 +02:00
|
|
|
func (Pipe) String() string {
|
|
|
|
return "loading environment variables"
|
2017-01-14 18:06:57 +02:00
|
|
|
}
|
|
|
|
|
2020-03-16 17:10:56 +02:00
|
|
|
func setDefaultTokenFiles(ctx *context.Context) {
|
2018-02-03 04:06:48 +02:00
|
|
|
var env = &ctx.Config.EnvFiles
|
|
|
|
if env.GitHubToken == "" {
|
|
|
|
env.GitHubToken = "~/.config/goreleaser/github_token"
|
|
|
|
}
|
2019-06-29 16:02:40 +02:00
|
|
|
if env.GitLabToken == "" {
|
|
|
|
env.GitLabToken = "~/.config/goreleaser/gitlab_token"
|
|
|
|
}
|
2019-08-26 09:31:38 +02:00
|
|
|
if env.GiteaToken == "" {
|
|
|
|
env.GiteaToken = "~/.config/goreleaser/gitea_token"
|
|
|
|
}
|
2018-02-03 04:06:48 +02:00
|
|
|
}
|
|
|
|
|
2017-01-14 18:06:57 +02:00
|
|
|
// Run the pipe
|
2018-02-03 04:06:48 +02:00
|
|
|
func (Pipe) Run(ctx *context.Context) error {
|
2020-03-16 17:10:56 +02:00
|
|
|
setDefaultTokenFiles(ctx)
|
2019-06-29 16:02:40 +02:00
|
|
|
githubToken, githubTokenErr := loadEnv("GITHUB_TOKEN", ctx.Config.EnvFiles.GitHubToken)
|
|
|
|
gitlabToken, gitlabTokenErr := loadEnv("GITLAB_TOKEN", ctx.Config.EnvFiles.GitLabToken)
|
2019-08-26 09:31:38 +02:00
|
|
|
giteaToken, giteaTokenErr := loadEnv("GITEA_TOKEN", ctx.Config.EnvFiles.GiteaToken)
|
2019-06-29 16:02:40 +02:00
|
|
|
|
2019-08-26 09:31:38 +02:00
|
|
|
numOfTokens := 0
|
|
|
|
if githubToken != "" {
|
|
|
|
numOfTokens++
|
|
|
|
}
|
|
|
|
if gitlabToken != "" {
|
|
|
|
numOfTokens++
|
|
|
|
}
|
|
|
|
if giteaToken != "" {
|
|
|
|
numOfTokens++
|
|
|
|
}
|
|
|
|
if numOfTokens > 1 {
|
2019-06-29 16:02:40 +02:00
|
|
|
return ErrMultipleTokens
|
|
|
|
}
|
|
|
|
|
2019-08-26 09:31:38 +02:00
|
|
|
noTokens := githubToken == "" && gitlabToken == "" && giteaToken == ""
|
|
|
|
noTokenErrs := githubTokenErr == nil && gitlabTokenErr == nil && giteaTokenErr == nil
|
|
|
|
|
2020-04-09 15:28:58 +02:00
|
|
|
if err := checkErrors(ctx, noTokens, noTokenErrs, gitlabTokenErr, githubTokenErr, giteaTokenErr); err != nil {
|
|
|
|
return err
|
2019-08-26 09:31:38 +02:00
|
|
|
}
|
|
|
|
|
2019-06-29 16:02:40 +02:00
|
|
|
if githubToken != "" {
|
2020-04-09 15:28:58 +02:00
|
|
|
log.Debug("token type: github")
|
2019-06-29 16:02:40 +02:00
|
|
|
ctx.TokenType = context.TokenTypeGitHub
|
|
|
|
ctx.Token = githubToken
|
|
|
|
}
|
|
|
|
|
|
|
|
if gitlabToken != "" {
|
2020-04-09 15:28:58 +02:00
|
|
|
log.Debug("token type: gitlab")
|
2019-06-29 16:02:40 +02:00
|
|
|
ctx.TokenType = context.TokenTypeGitLab
|
|
|
|
ctx.Token = gitlabToken
|
|
|
|
}
|
|
|
|
|
2019-08-26 09:31:38 +02:00
|
|
|
if giteaToken != "" {
|
2020-04-09 15:28:58 +02:00
|
|
|
log.Debug("token type: gitea")
|
2019-08-26 09:31:38 +02:00
|
|
|
ctx.TokenType = context.TokenTypeGitea
|
|
|
|
ctx.Token = giteaToken
|
|
|
|
}
|
|
|
|
|
2019-06-29 16:02:40 +02:00
|
|
|
return nil
|
2018-02-03 04:06:48 +02:00
|
|
|
}
|
|
|
|
|
2020-04-09 15:28:58 +02:00
|
|
|
func checkErrors(ctx *context.Context, noTokens, noTokenErrs bool, gitlabTokenErr, githubTokenErr, giteaTokenErr error) error {
|
|
|
|
if ctx.SkipPublish || ctx.Config.Release.Disable {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if noTokens && noTokenErrs {
|
|
|
|
return ErrMissingToken
|
|
|
|
}
|
|
|
|
|
|
|
|
if gitlabTokenErr != nil {
|
|
|
|
return errors.Wrap(gitlabTokenErr, "failed to load gitlab token")
|
|
|
|
}
|
|
|
|
|
|
|
|
if githubTokenErr != nil {
|
|
|
|
return errors.Wrap(githubTokenErr, "failed to load github token")
|
|
|
|
}
|
|
|
|
|
|
|
|
if giteaTokenErr != nil {
|
|
|
|
return errors.Wrap(giteaTokenErr, "failed to load gitea token")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-02-03 04:06:48 +02:00
|
|
|
func loadEnv(env, path string) (string, error) {
|
|
|
|
val := os.Getenv(env)
|
2018-02-03 04:16:30 +02:00
|
|
|
if val != "" {
|
|
|
|
return val, nil
|
2018-02-03 04:06:48 +02:00
|
|
|
}
|
2018-02-03 04:16:30 +02:00
|
|
|
path, err := homedir.Expand(path)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2018-09-04 14:26:45 +02:00
|
|
|
f, err := os.Open(path) // #nosec
|
2018-02-03 20:47:03 +02:00
|
|
|
if os.IsNotExist(err) {
|
2018-02-03 04:16:30 +02:00
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2018-02-03 20:51:19 +02:00
|
|
|
bts, _, err := bufio.NewReader(f).ReadLine()
|
|
|
|
return string(bts), err
|
2017-01-14 18:06:57 +02:00
|
|
|
}
|