github.com/amane3/goreleaser@v0.182.0/internal/pipe/env/env.go (about)

     1  // Package env implements the Pipe interface providing validation of
     2  // missing environment variables needed by the release process.
     3  package env
     4  
     5  import (
     6  	"bufio"
     7  	"errors"
     8  	"fmt"
     9  	"os"
    10  
    11  	"github.com/amane3/goreleaser/pkg/context"
    12  	"github.com/apex/log"
    13  	homedir "github.com/mitchellh/go-homedir"
    14  )
    15  
    16  // ErrMissingToken indicates an error when GITHUB_TOKEN, GITLAB_TOKEN and GITEA_TOKEN are all missing in the environment.
    17  var ErrMissingToken = errors.New("missing GITHUB_TOKEN, GITLAB_TOKEN and GITEA_TOKEN")
    18  
    19  // ErrMultipleTokens indicates that multiple tokens are defined. ATM only one of them if allowed.
    20  // See https://github.com/amane3/goreleaser/pull/809
    21  var ErrMultipleTokens = errors.New("multiple tokens defined. Only one is allowed")
    22  
    23  // Pipe for env.
    24  type Pipe struct{}
    25  
    26  func (Pipe) String() string {
    27  	return "loading environment variables"
    28  }
    29  
    30  func setDefaultTokenFiles(ctx *context.Context) {
    31  	var env = &ctx.Config.EnvFiles
    32  	if env.GitHubToken == "" {
    33  		env.GitHubToken = "~/.config/goreleaser/github_token"
    34  	}
    35  	if env.GitLabToken == "" {
    36  		env.GitLabToken = "~/.config/goreleaser/gitlab_token"
    37  	}
    38  	if env.GiteaToken == "" {
    39  		env.GiteaToken = "~/.config/goreleaser/gitea_token"
    40  	}
    41  }
    42  
    43  // Run the pipe.
    44  func (Pipe) Run(ctx *context.Context) error {
    45  	setDefaultTokenFiles(ctx)
    46  	githubToken, githubTokenErr := loadEnv("GITHUB_TOKEN", ctx.Config.EnvFiles.GitHubToken)
    47  	gitlabToken, gitlabTokenErr := loadEnv("GITLAB_TOKEN", ctx.Config.EnvFiles.GitLabToken)
    48  	giteaToken, giteaTokenErr := loadEnv("GITEA_TOKEN", ctx.Config.EnvFiles.GiteaToken)
    49  
    50  	numOfTokens := 0
    51  	if githubToken != "" {
    52  		numOfTokens++
    53  	}
    54  	if gitlabToken != "" {
    55  		numOfTokens++
    56  	}
    57  	if giteaToken != "" {
    58  		numOfTokens++
    59  	}
    60  	if numOfTokens > 1 {
    61  		return ErrMultipleTokens
    62  	}
    63  
    64  	noTokens := githubToken == "" && gitlabToken == "" && giteaToken == ""
    65  	noTokenErrs := githubTokenErr == nil && gitlabTokenErr == nil && giteaTokenErr == nil
    66  
    67  	if err := checkErrors(ctx, noTokens, noTokenErrs, gitlabTokenErr, githubTokenErr, giteaTokenErr); err != nil {
    68  		return err
    69  	}
    70  
    71  	if githubToken != "" {
    72  		log.Debug("token type: github")
    73  		ctx.TokenType = context.TokenTypeGitHub
    74  		ctx.Token = githubToken
    75  	}
    76  
    77  	if gitlabToken != "" {
    78  		log.Debug("token type: gitlab")
    79  		ctx.TokenType = context.TokenTypeGitLab
    80  		ctx.Token = gitlabToken
    81  	}
    82  
    83  	if giteaToken != "" {
    84  		log.Debug("token type: gitea")
    85  		ctx.TokenType = context.TokenTypeGitea
    86  		ctx.Token = giteaToken
    87  	}
    88  
    89  	return nil
    90  }
    91  
    92  func checkErrors(ctx *context.Context, noTokens, noTokenErrs bool, gitlabTokenErr, githubTokenErr, giteaTokenErr error) error {
    93  	if ctx.SkipTokenCheck || ctx.SkipPublish || ctx.Config.Release.Disable {
    94  		return nil
    95  	}
    96  
    97  	if noTokens && noTokenErrs {
    98  		return ErrMissingToken
    99  	}
   100  
   101  	if gitlabTokenErr != nil {
   102  		return fmt.Errorf("failed to load gitlab token: %w", gitlabTokenErr)
   103  	}
   104  
   105  	if githubTokenErr != nil {
   106  		return fmt.Errorf("failed to load github token: %w", githubTokenErr)
   107  	}
   108  
   109  	if giteaTokenErr != nil {
   110  		return fmt.Errorf("failed to load gitea token: %w", giteaTokenErr)
   111  	}
   112  	return nil
   113  }
   114  
   115  func loadEnv(env, path string) (string, error) {
   116  	val := os.Getenv(env)
   117  	if val != "" {
   118  		return val, nil
   119  	}
   120  	path, err := homedir.Expand(path)
   121  	if err != nil {
   122  		return "", err
   123  	}
   124  	f, err := os.Open(path) // #nosec
   125  	if os.IsNotExist(err) {
   126  		return "", nil
   127  	}
   128  	if err != nil {
   129  		return "", err
   130  	}
   131  	bts, _, err := bufio.NewReader(f).ReadLine()
   132  	return string(bts), err
   133  }