2017-04-14 15:39:32 -03:00
|
|
|
// Package env implements the Pipe interface providing validation of
|
|
|
|
// missing environment variables needed by the release process.
|
2017-01-14 14:06:57 -02:00
|
|
|
package env
|
|
|
|
|
|
|
|
import (
|
2018-02-03 16:51:19 -02:00
|
|
|
"bufio"
|
2020-09-21 14:47:51 -03:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2017-01-14 14:06:57 -02:00
|
|
|
"os"
|
|
|
|
|
2020-04-09 10:28:58 -03:00
|
|
|
"github.com/apex/log"
|
2021-05-24 23:23:59 -03:00
|
|
|
"github.com/goreleaser/goreleaser/internal/tmpl"
|
2018-08-14 23:50:20 -03:00
|
|
|
"github.com/goreleaser/goreleaser/pkg/context"
|
2018-02-03 00:06:48 -02:00
|
|
|
homedir "github.com/mitchellh/go-homedir"
|
2017-01-14 14:06:57 -02:00
|
|
|
)
|
|
|
|
|
2020-05-26 00:48:10 -03:00
|
|
|
// ErrMissingToken indicates an error when GITHUB_TOKEN, GITLAB_TOKEN and GITEA_TOKEN are all missing in the environment.
|
2019-08-26 10:31:38 +03:00
|
|
|
var ErrMissingToken = errors.New("missing GITHUB_TOKEN, GITLAB_TOKEN and GITEA_TOKEN")
|
2019-06-29 16:02:40 +02:00
|
|
|
|
2020-05-26 00:48:10 -03:00
|
|
|
// ErrMultipleTokens indicates that multiple tokens are defined. ATM only one of them if allowed.
|
2019-06-29 16:02:40 +02:00
|
|
|
// See https://github.com/goreleaser/goreleaser/pull/809
|
2019-08-26 10:31:38 +03:00
|
|
|
var ErrMultipleTokens = errors.New("multiple tokens defined. Only one is allowed")
|
2017-01-14 14:06:57 -02:00
|
|
|
|
2020-05-26 00:48:10 -03:00
|
|
|
// Pipe for env.
|
2017-01-14 14:06:57 -02:00
|
|
|
type Pipe struct{}
|
|
|
|
|
2017-12-02 19:53:19 -02:00
|
|
|
func (Pipe) String() string {
|
|
|
|
return "loading environment variables"
|
2017-01-14 14:06:57 -02:00
|
|
|
}
|
|
|
|
|
2020-03-16 12:10:56 -03:00
|
|
|
func setDefaultTokenFiles(ctx *context.Context) {
|
2021-04-25 14:20:49 -03:00
|
|
|
env := &ctx.Config.EnvFiles
|
2018-02-03 00:06:48 -02:00
|
|
|
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 10:31:38 +03:00
|
|
|
if env.GiteaToken == "" {
|
|
|
|
env.GiteaToken = "~/.config/goreleaser/gitea_token"
|
|
|
|
}
|
2018-02-03 00:06:48 -02:00
|
|
|
}
|
|
|
|
|
2020-05-26 00:48:10 -03:00
|
|
|
// Run the pipe.
|
2018-02-03 00:06:48 -02:00
|
|
|
func (Pipe) Run(ctx *context.Context) error {
|
2021-05-24 23:23:59 -03:00
|
|
|
templ := tmpl.New(ctx).WithEnvS(os.Environ())
|
2021-11-11 22:56:03 -03:00
|
|
|
tEnv := []string{}
|
2021-05-24 23:23:59 -03:00
|
|
|
for i := range ctx.Config.Env {
|
|
|
|
env, err := templ.Apply(ctx.Config.Env[i])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-11-11 22:56:03 -03:00
|
|
|
tEnv = append(tEnv, env)
|
|
|
|
}
|
|
|
|
for k, v := range context.ToEnv(tEnv) {
|
|
|
|
ctx.Env[k] = v
|
2021-05-24 23:23:59 -03:00
|
|
|
}
|
|
|
|
|
2020-03-16 12:10:56 -03: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 10:31:38 +03:00
|
|
|
giteaToken, giteaTokenErr := loadEnv("GITEA_TOKEN", ctx.Config.EnvFiles.GiteaToken)
|
2019-06-29 16:02:40 +02:00
|
|
|
|
2019-08-26 10:31:38 +03: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 10:31:38 +03:00
|
|
|
noTokens := githubToken == "" && gitlabToken == "" && giteaToken == ""
|
|
|
|
noTokenErrs := githubTokenErr == nil && gitlabTokenErr == nil && giteaTokenErr == nil
|
|
|
|
|
2020-04-09 10:28:58 -03:00
|
|
|
if err := checkErrors(ctx, noTokens, noTokenErrs, gitlabTokenErr, githubTokenErr, giteaTokenErr); err != nil {
|
|
|
|
return err
|
2019-08-26 10:31:38 +03:00
|
|
|
}
|
|
|
|
|
2019-06-29 16:02:40 +02:00
|
|
|
if gitlabToken != "" {
|
2020-04-09 10:28:58 -03:00
|
|
|
log.Debug("token type: gitlab")
|
2019-06-29 16:02:40 +02:00
|
|
|
ctx.TokenType = context.TokenTypeGitLab
|
|
|
|
ctx.Token = gitlabToken
|
|
|
|
}
|
|
|
|
|
2019-08-26 10:31:38 +03:00
|
|
|
if giteaToken != "" {
|
2020-04-09 10:28:58 -03:00
|
|
|
log.Debug("token type: gitea")
|
2019-08-26 10:31:38 +03:00
|
|
|
ctx.TokenType = context.TokenTypeGitea
|
|
|
|
ctx.Token = giteaToken
|
|
|
|
}
|
|
|
|
|
2021-09-27 08:13:56 -03:00
|
|
|
if githubToken != "" {
|
|
|
|
log.Debug("token type: github")
|
|
|
|
ctx.Token = githubToken
|
|
|
|
}
|
|
|
|
|
|
|
|
if ctx.TokenType == "" {
|
|
|
|
ctx.TokenType = context.TokenTypeGitHub
|
|
|
|
}
|
|
|
|
|
2019-06-29 16:02:40 +02:00
|
|
|
return nil
|
2018-02-03 00:06:48 -02:00
|
|
|
}
|
|
|
|
|
2020-04-09 10:28:58 -03:00
|
|
|
func checkErrors(ctx *context.Context, noTokens, noTokenErrs bool, gitlabTokenErr, githubTokenErr, giteaTokenErr error) error {
|
2020-05-15 15:19:20 +01:00
|
|
|
if ctx.SkipTokenCheck || ctx.SkipPublish || ctx.Config.Release.Disable {
|
2020-04-09 10:28:58 -03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if noTokens && noTokenErrs {
|
|
|
|
return ErrMissingToken
|
|
|
|
}
|
|
|
|
|
|
|
|
if gitlabTokenErr != nil {
|
2020-09-21 14:47:51 -03:00
|
|
|
return fmt.Errorf("failed to load gitlab token: %w", gitlabTokenErr)
|
2020-04-09 10:28:58 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
if githubTokenErr != nil {
|
2020-09-21 14:47:51 -03:00
|
|
|
return fmt.Errorf("failed to load github token: %w", githubTokenErr)
|
2020-04-09 10:28:58 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
if giteaTokenErr != nil {
|
2020-09-21 14:47:51 -03:00
|
|
|
return fmt.Errorf("failed to load gitea token: %w", giteaTokenErr)
|
2020-04-09 10:28:58 -03:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-02-03 00:06:48 -02:00
|
|
|
func loadEnv(env, path string) (string, error) {
|
|
|
|
val := os.Getenv(env)
|
2018-02-03 00:16:30 -02:00
|
|
|
if val != "" {
|
|
|
|
return val, nil
|
2018-02-03 00:06:48 -02:00
|
|
|
}
|
2018-02-03 00:16:30 -02:00
|
|
|
path, err := homedir.Expand(path)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2018-09-04 09:26:45 -03:00
|
|
|
f, err := os.Open(path) // #nosec
|
2018-02-03 16:47:03 -02:00
|
|
|
if os.IsNotExist(err) {
|
2018-02-03 00:16:30 -02:00
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2021-04-25 14:41:36 +00:00
|
|
|
defer f.Close()
|
2018-02-03 16:51:19 -02:00
|
|
|
bts, _, err := bufio.NewReader(f).ReadLine()
|
|
|
|
return string(bts), err
|
2017-01-14 14:06:57 -02:00
|
|
|
}
|