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

157 lines
3.6 KiB
Go
Raw Normal View History

2017-04-14 20:39:32 +02:00
// Package context provides gorelease context which is passed through the
// pipeline.
//
// The context extends the standard library context and add a few more
// fields and other things, so pipes can gather data provided by previous
// pipes without really knowing each other.
2017-01-14 16:34:22 +02:00
package context
2017-02-23 21:01:54 +02:00
import (
stdctx "context"
"os"
"runtime"
"strings"
2017-12-29 21:07:06 +02:00
"time"
2017-02-23 21:01:54 +02:00
"github.com/goreleaser/goreleaser/internal/artifact"
"github.com/goreleaser/goreleaser/pkg/config"
2017-02-23 21:01:54 +02:00
)
2017-01-14 16:34:22 +02:00
// GitInfo includes tags and diffs used in some point.
2017-01-14 16:34:22 +02:00
type GitInfo struct {
Branch string
2018-10-03 22:44:31 +02:00
CurrentTag string
PreviousTag string
2018-10-03 22:44:31 +02:00
Commit string
ShortCommit string
FullCommit string
FirstCommit string
CommitDate time.Time
URL string
Summary string
TagSubject string
TagContents string
TagBody string
Dirty bool
2017-01-14 16:34:22 +02:00
}
// Env is the environment variables.
type Env map[string]string
// Copy returns a copy of the environment.
func (e Env) Copy() Env {
out := Env{}
for k, v := range e {
out[k] = v
}
return out
}
// Strings returns the current environment as a list of strings, suitable for
// os executions.
func (e Env) Strings() []string {
result := make([]string, 0, len(e))
for k, v := range e {
result = append(result, k+"="+v)
}
return result
}
// TokenType is either github or gitlab.
type TokenType string
feat: add gitlab for releases (#1038) * outlines gitlab client integration * makes client parameter more explicit * adds gitlab url to config * changes releaseID to string to adapt to gitlab * updates to latest gitlab client lib 0.18 * fixes copy paster in gitlab upload func * fixes gitlab typo in config * adds gitlab token to env and context * release now uses the client factory method * skips brew pipe if it is not a github release * add github tokentype to publish tests * skips scoop pipe if it is not a github release * corrects brew skip msg * adds gitlab token to main test * adds gitlab to release docs * validates config and errors accordingly * adapt release pipe name to include gitlab * fixes gitlab client after testing * moves not-configured brew and scoop pipe checks as first check * adds more debug to gitlab client * adapts changelog generation for gitlab markdown * adds debug log for gitlab changelog * env needs to run before changelog pipe * moves gitlab default download url to default pipe * moves multiple releases check to from config to release pipe * release differs now for github and gitlab * adds debug gitlab release update msgs * moves env pipe as second after before because it determines the token type other pipes depend on * adaptes error check on gitlab release creation * Revert "adaptes error check on gitlab release creation" This reverts commit 032024571c76140f8e2207ee01cc08088f37594b. * simplifies gitlab client logic. removes comments * skips tls verification for gitlab client if specified in config * updates the docs * adds clarification that brew and scoop are not supported if it is a gitlab release * fixes copy paster in release.md * adds missing blob pipe in defaults and publish due to missing in merge * updates comment in gitlab client
2019-06-29 16:02:40 +02:00
const (
// TokenTypeGitHub defines github as type of the token.
TokenTypeGitHub TokenType = "github"
// TokenTypeGitLab defines gitlab as type of the token.
TokenTypeGitLab TokenType = "gitlab"
// TokenTypeGitea defines gitea as type of the token.
TokenTypeGitea TokenType = "gitea"
feat: add gitlab for releases (#1038) * outlines gitlab client integration * makes client parameter more explicit * adds gitlab url to config * changes releaseID to string to adapt to gitlab * updates to latest gitlab client lib 0.18 * fixes copy paster in gitlab upload func * fixes gitlab typo in config * adds gitlab token to env and context * release now uses the client factory method * skips brew pipe if it is not a github release * add github tokentype to publish tests * skips scoop pipe if it is not a github release * corrects brew skip msg * adds gitlab token to main test * adds gitlab to release docs * validates config and errors accordingly * adapt release pipe name to include gitlab * fixes gitlab client after testing * moves not-configured brew and scoop pipe checks as first check * adds more debug to gitlab client * adapts changelog generation for gitlab markdown * adds debug log for gitlab changelog * env needs to run before changelog pipe * moves gitlab default download url to default pipe * moves multiple releases check to from config to release pipe * release differs now for github and gitlab * adds debug gitlab release update msgs * moves env pipe as second after before because it determines the token type other pipes depend on * adaptes error check on gitlab release creation * Revert "adaptes error check on gitlab release creation" This reverts commit 032024571c76140f8e2207ee01cc08088f37594b. * simplifies gitlab client logic. removes comments * skips tls verification for gitlab client if specified in config * updates the docs * adds clarification that brew and scoop are not supported if it is a gitlab release * fixes copy paster in release.md * adds missing blob pipe in defaults and publish due to missing in merge * updates comment in gitlab client
2019-06-29 16:02:40 +02:00
)
// Context carries along some data through the pipes.
2017-01-14 16:34:22 +02:00
type Context struct {
stdctx.Context
Config config.Project
Env Env
Token string
TokenType TokenType
Git GitInfo
Date time.Time
Artifacts *artifact.Artifacts
ReleaseURL string
ReleaseNotes string
ReleaseNotesFile string
ReleaseNotesTmpl string
ReleaseHeaderFile string
ReleaseHeaderTmpl string
ReleaseFooterFile string
ReleaseFooterTmpl string
Version string
ModulePath string
Snapshot bool
FailFast bool
SkipTokenCheck bool
Clean bool
PreRelease bool
Deprecated bool
Parallelism int
Semver Semver
Runtime Runtime
Skips map[string]bool
}
type Runtime struct {
Goos string
Goarch string
2019-01-19 20:57:58 +02:00
}
// Semver represents a semantic version.
2019-01-19 20:57:58 +02:00
type Semver struct {
Major uint64
Minor uint64
Patch uint64
2019-01-19 20:57:58 +02:00
Prerelease string
}
// New context.
func New(config config.Project) *Context {
return Wrap(stdctx.Background(), config)
2017-12-29 21:07:06 +02:00
}
// NewWithTimeout new context with the given timeout.
func NewWithTimeout(config config.Project, timeout time.Duration) (*Context, stdctx.CancelFunc) {
ctx, cancel := stdctx.WithTimeout(stdctx.Background(), timeout) // nosem
2018-01-22 00:07:18 +02:00
return Wrap(ctx, config), cancel
2017-12-29 21:07:06 +02:00
}
// Wrap wraps an existing context.
func Wrap(ctx stdctx.Context, config config.Project) *Context {
2017-01-14 16:34:22 +02:00
return &Context{
2017-12-29 21:07:06 +02:00
Context: ctx,
2017-07-15 21:49:52 +02:00
Config: config,
Env: ToEnv(append(os.Environ(), config.Env...)),
2017-07-15 21:49:52 +02:00
Parallelism: 4,
2017-12-17 21:25:04 +02:00
Artifacts: artifact.New(),
Date: time.Now(),
Skips: map[string]bool{},
Runtime: Runtime{
Goos: runtime.GOOS,
Goarch: runtime.GOARCH,
},
2017-01-14 16:34:22 +02:00
}
}
// ToEnv converts a list of strings to an Env (aka a map[string]string).
func ToEnv(env []string) Env {
r := Env{}
for _, e := range env {
k, v, ok := strings.Cut(e, "=")
if !ok || k == "" {
continue
}
r[k] = v
}
return r
}