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"
|
|
|
|
|
2018-08-15 04:50:20 +02:00
|
|
|
"github.com/goreleaser/goreleaser/internal/pipeline"
|
|
|
|
"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
|
|
|
)
|
|
|
|
|
2017-01-14 20:41:32 +02:00
|
|
|
// ErrMissingToken indicates an error when GITHUB_TOKEN is missing in the environment
|
2017-04-21 16:48:00 +02:00
|
|
|
var ErrMissingToken = errors.New("missing GITHUB_TOKEN")
|
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
|
|
|
}
|
|
|
|
|
2018-02-03 04:06:48 +02:00
|
|
|
// Default sets the pipe defaults
|
|
|
|
func (Pipe) Default(ctx *context.Context) error {
|
|
|
|
var env = &ctx.Config.EnvFiles
|
|
|
|
if env.GitHubToken == "" {
|
|
|
|
env.GitHubToken = "~/.config/goreleaser/github_token"
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
token, err := loadEnv("GITHUB_TOKEN", ctx.Config.EnvFiles.GitHubToken)
|
|
|
|
ctx.Token = token
|
2018-03-01 06:12:58 +02:00
|
|
|
if ctx.SkipPublish {
|
|
|
|
return pipeline.ErrSkipPublishEnabled
|
2017-04-18 18:10:13 +02:00
|
|
|
}
|
2018-05-09 02:03:21 +02:00
|
|
|
if ctx.Config.Release.Disable {
|
|
|
|
return pipeline.Skip("release pipe is disabled")
|
|
|
|
}
|
2018-02-03 04:06:48 +02:00
|
|
|
if ctx.Token == "" && err == nil {
|
2017-01-14 18:06:57 +02:00
|
|
|
return ErrMissingToken
|
|
|
|
}
|
2018-02-03 21:00:58 +02:00
|
|
|
return errors.Wrap(err, "failed to load github token")
|
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-02-03 20:51:19 +02:00
|
|
|
f, err := os.Open(path)
|
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
|
|
|
}
|