github.com/szyn/goreleaser@v0.76.1-0.20180517112710-333da09a1297/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/config"
    16  	"github.com/goreleaser/goreleaser/internal/artifact"
    17  )
    18  
    19  // GitInfo includes tags and diffs used in some point
    20  type GitInfo struct {
    21  	CurrentTag string
    22  	Commit     string
    23  }
    24  
    25  // Context carries along some data through the pipes
    26  type Context struct {
    27  	ctx.Context
    28  	Config       config.Project
    29  	Env          map[string]string
    30  	Token        string
    31  	Git          GitInfo
    32  	Artifacts    artifact.Artifacts
    33  	ReleaseNotes string
    34  	Version      string
    35  	Snapshot     bool
    36  	SkipPublish  bool
    37  	SkipValidate bool
    38  	RmDist       bool
    39  	Debug        bool
    40  	Parallelism  int
    41  }
    42  
    43  // New context
    44  func New(config config.Project) *Context {
    45  	return Wrap(ctx.Background(), config)
    46  }
    47  
    48  // NewWithTimeout new context with the given timeout
    49  func NewWithTimeout(config config.Project, timeout time.Duration) (*Context, ctx.CancelFunc) {
    50  	ctx, cancel := ctx.WithTimeout(ctx.Background(), timeout)
    51  	return Wrap(ctx, config), cancel
    52  }
    53  
    54  // Wrap wraps an existing context
    55  func Wrap(ctx ctx.Context, config config.Project) *Context {
    56  	return &Context{
    57  		Context:     ctx,
    58  		Config:      config,
    59  		Env:         splitEnv(os.Environ()),
    60  		Parallelism: 4,
    61  		Artifacts:   artifact.New(),
    62  	}
    63  }
    64  
    65  func splitEnv(env []string) map[string]string {
    66  	r := map[string]string{}
    67  	for _, e := range env {
    68  		p := strings.SplitN(e, "=", 2)
    69  		r[p[0]] = p[1]
    70  	}
    71  	return r
    72  }