github.com/ahmet2mir/goreleaser@v0.180.3-0.20210927151101-8e5ee5a9b8c5/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  	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  	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  	ReleaseNotesFile   string
    77  	ReleaseNotesTmpl   string
    78  	ReleaseHeaderFile  string
    79  	ReleaseHeaderTmpl  string
    80  	ReleaseFooterFile  string
    81  	ReleaseFooterTmpl  string
    82  	Version            string
    83  	ModulePath         string
    84  	Snapshot           bool
    85  	SkipPostBuildHooks bool
    86  	SkipPublish        bool
    87  	SkipAnnounce       bool
    88  	SkipSign           bool
    89  	SkipValidate       bool
    90  	RmDist             bool
    91  	PreRelease         bool
    92  	Deprecated         bool
    93  	Parallelism        int
    94  	Semver             Semver
    95  }
    96  
    97  // Semver represents a semantic version.
    98  type Semver struct {
    99  	Major      uint64
   100  	Minor      uint64
   101  	Patch      uint64
   102  	RawVersion string
   103  	Prerelease string
   104  }
   105  
   106  // New context.
   107  func New(config config.Project) *Context {
   108  	return Wrap(ctx.Background(), config)
   109  }
   110  
   111  // NewWithTimeout new context with the given timeout.
   112  func NewWithTimeout(config config.Project, timeout time.Duration) (*Context, ctx.CancelFunc) {
   113  	ctx, cancel := ctx.WithTimeout(ctx.Background(), timeout)
   114  	return Wrap(ctx, config), cancel
   115  }
   116  
   117  // Wrap wraps an existing context.
   118  func Wrap(ctx ctx.Context, config config.Project) *Context {
   119  	return &Context{
   120  		Context:     ctx,
   121  		Config:      config,
   122  		Env:         splitEnv(append(os.Environ(), config.Env...)),
   123  		Parallelism: 4,
   124  		Artifacts:   artifact.New(),
   125  		Date:        time.Now(),
   126  	}
   127  }
   128  
   129  func splitEnv(env []string) map[string]string {
   130  	// TODO: this might panic if there is no `=` sign
   131  	r := map[string]string{}
   132  	for _, e := range env {
   133  		p := strings.SplitN(e, "=", 2)
   134  		r[p[0]] = p[1]
   135  	}
   136  	return r
   137  }