github.com/joselitofilho/goreleaser@v0.155.1-0.20210123221854-e4891856c593/pkg/context/context.go (about)

     1  // Package context provides gorelease context which is passed through the
     2  // pipeline.
     3  //
     4  // The context extends the standard library context and add a few more
     5  // fields and other things, so pipes can gather data provided by previous
     6  // pipes without really knowing each other.
     7  package context
     8  
     9  import (
    10  	ctx "context"
    11  	"os"
    12  	"strings"
    13  	"time"
    14  
    15  	"github.com/goreleaser/goreleaser/internal/artifact"
    16  	"github.com/goreleaser/goreleaser/pkg/config"
    17  )
    18  
    19  // GitInfo includes tags and diffs used in some point.
    20  type GitInfo struct {
    21  	Branch      string
    22  	CurrentTag  string
    23  	Commit      string
    24  	ShortCommit string
    25  	FullCommit  string
    26  	CommitDate  time.Time
    27  	URL         string
    28  }
    29  
    30  // Env is the environment variables.
    31  type Env map[string]string
    32  
    33  // Copy returns a copy of the environment.
    34  func (e Env) Copy() Env {
    35  	var out = Env{}
    36  	for k, v := range e {
    37  		out[k] = v
    38  	}
    39  	return out
    40  }
    41  
    42  // Strings returns the current environment as a list of strings, suitable for
    43  // os executions.
    44  func (e Env) Strings() []string {
    45  	var result = make([]string, 0, len(e))
    46  	for k, v := range e {
    47  		result = append(result, k+"="+v)
    48  	}
    49  	return result
    50  }
    51  
    52  // TokenType is either github or gitlab.
    53  type TokenType string
    54  
    55  const (
    56  	// TokenTypeGitHub defines github as type of the token.
    57  	TokenTypeGitHub TokenType = "github"
    58  	// TokenTypeGitLab defines gitlab as type of the token.
    59  	TokenTypeGitLab TokenType = "gitlab"
    60  	// TokenTypeGitea defines gitea as type of the token.
    61  	TokenTypeGitea TokenType = "gitea"
    62  )
    63  
    64  // Context carries along some data through the pipes.
    65  type Context struct {
    66  	ctx.Context
    67  	Config             config.Project
    68  	Env                Env
    69  	SkipTokenCheck     bool
    70  	Token              string
    71  	TokenType          TokenType
    72  	Git                GitInfo
    73  	Date               time.Time
    74  	Artifacts          artifact.Artifacts
    75  	ReleaseNotes       string
    76  	ReleaseHeader      string
    77  	ReleaseFooter      string
    78  	Version            string
    79  	Snapshot           bool
    80  	SkipPostBuildHooks bool
    81  	SkipPublish        bool
    82  	SkipSign           bool
    83  	SkipValidate       bool
    84  	RmDist             bool
    85  	PreRelease         bool
    86  	Deprecated         bool
    87  	Parallelism        int
    88  	Semver             Semver
    89  	BuildIDs           []string
    90  }
    91  
    92  // Semver represents a semantic version.
    93  type Semver struct {
    94  	Major      uint64
    95  	Minor      uint64
    96  	Patch      uint64
    97  	RawVersion string
    98  	Prerelease string
    99  }
   100  
   101  // New context.
   102  func New(config config.Project) *Context {
   103  	return Wrap(ctx.Background(), config)
   104  }
   105  
   106  // NewWithTimeout new context with the given timeout.
   107  func NewWithTimeout(config config.Project, timeout time.Duration) (*Context, ctx.CancelFunc) {
   108  	ctx, cancel := ctx.WithTimeout(ctx.Background(), timeout)
   109  	return Wrap(ctx, config), cancel
   110  }
   111  
   112  // Wrap wraps an existing context.
   113  func Wrap(ctx ctx.Context, config config.Project) *Context {
   114  	return &Context{
   115  		Context:     ctx,
   116  		Config:      config,
   117  		Env:         splitEnv(append(os.Environ(), config.Env...)),
   118  		Parallelism: 4,
   119  		Artifacts:   artifact.New(),
   120  		Date:        time.Now(),
   121  	}
   122  }
   123  
   124  func splitEnv(env []string) map[string]string {
   125  	r := map[string]string{}
   126  	for _, e := range env {
   127  		p := strings.SplitN(e, "=", 2)
   128  		r[p[0]] = p[1]
   129  	}
   130  	return r
   131  }