1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-01-08 03:31:59 +02:00
goreleaser/pipeline/env/env.go

71 lines
1.6 KiB
Go
Raw Normal View History

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 04:06:48 +02:00
"io/ioutil"
2017-01-14 18:06:57 +02:00
"os"
2017-01-15 00:01:32 +02:00
"github.com/goreleaser/goreleaser/context"
2017-08-20 21:35:46 +02:00
"github.com/goreleaser/goreleaser/pipeline"
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
var ErrMissingToken = errors.New("missing GITHUB_TOKEN")
2017-01-14 18:06:57 +02:00
// Pipe for env
type Pipe struct{}
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
2017-04-29 12:49:22 +02:00
if !ctx.Publish {
2017-08-20 21:35:46 +02:00
return pipeline.Skip("publishing is disabled")
2017-04-29 12:49:22 +02:00
}
if !ctx.Validate {
2017-08-20 21:35:46 +02:00
return pipeline.Skip("--skip-validate is set")
}
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 04:06:48 +02:00
if err != nil {
return errors.Wrap(err, "failed to load github token")
}
return nil
}
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
}
if _, err := os.Stat(path); os.IsNotExist(err) {
return "", nil
}
bts, err := ioutil.ReadFile(path)
if err != nil {
return "", err
}
return string(bts), nil
2017-01-14 18:06:57 +02:00
}