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 (
|
2022-06-22 06:48:11 +02:00
|
|
|
stdctx "context"
|
2017-12-06 01:01:47 +02:00
|
|
|
"os"
|
2022-02-01 03:36:22 +02:00
|
|
|
"runtime"
|
2017-04-14 17:07:40 +02:00
|
|
|
"strings"
|
2017-12-29 21:07:06 +02:00
|
|
|
"time"
|
2017-02-23 21:01:54 +02:00
|
|
|
|
2017-12-17 19:18:46 +02:00
|
|
|
"github.com/goreleaser/goreleaser/internal/artifact"
|
2018-08-15 04:50:20 +02:00
|
|
|
"github.com/goreleaser/goreleaser/pkg/config"
|
2017-02-23 21:01:54 +02:00
|
|
|
)
|
2017-01-14 16:34:22 +02:00
|
|
|
|
2020-05-26 05:48:10 +02:00
|
|
|
// GitInfo includes tags and diffs used in some point.
|
2017-01-14 16:34:22 +02:00
|
|
|
type GitInfo struct {
|
2020-11-18 20:50:31 +02:00
|
|
|
Branch string
|
2018-10-03 22:44:31 +02:00
|
|
|
CurrentTag string
|
2021-11-24 14:12:24 +02:00
|
|
|
PreviousTag string
|
2018-10-03 22:44:31 +02:00
|
|
|
Commit string
|
|
|
|
ShortCommit string
|
|
|
|
FullCommit string
|
2022-12-29 15:41:59 +02:00
|
|
|
FirstCommit string
|
2020-07-06 22:09:22 +02:00
|
|
|
CommitDate time.Time
|
2018-10-04 14:38:19 +02:00
|
|
|
URL string
|
2021-11-25 05:01:56 +02:00
|
|
|
Summary string
|
2021-12-06 21:52:26 +02:00
|
|
|
TagSubject string
|
|
|
|
TagContents string
|
2022-02-25 03:57:51 +02:00
|
|
|
TagBody string
|
2023-05-02 06:04:18 +02:00
|
|
|
Dirty bool
|
2017-01-14 16:34:22 +02:00
|
|
|
}
|
|
|
|
|
2020-05-26 05:48:10 +02:00
|
|
|
// Env is the environment variables.
|
2019-04-09 14:15:05 +02:00
|
|
|
type Env map[string]string
|
|
|
|
|
2020-01-30 05:21:45 +02:00
|
|
|
// Copy returns a copy of the environment.
|
|
|
|
func (e Env) Copy() Env {
|
2021-03-22 13:55:01 +02:00
|
|
|
out := Env{}
|
2020-01-30 05:21:45 +02:00
|
|
|
for k, v := range e {
|
|
|
|
out[k] = v
|
|
|
|
}
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2019-04-09 14:15:05 +02:00
|
|
|
// Strings returns the current environment as a list of strings, suitable for
|
|
|
|
// os executions.
|
|
|
|
func (e Env) Strings() []string {
|
2021-03-22 13:55:01 +02:00
|
|
|
result := make([]string, 0, len(e))
|
2019-04-09 14:15:05 +02:00
|
|
|
for k, v := range e {
|
|
|
|
result = append(result, k+"="+v)
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2020-05-26 05:48:10 +02:00
|
|
|
// TokenType is either github or gitlab.
|
2019-08-13 20:28:03 +02:00
|
|
|
type TokenType string
|
2019-06-29 16:02:40 +02:00
|
|
|
|
|
|
|
const (
|
2020-05-26 05:48:10 +02:00
|
|
|
// TokenTypeGitHub defines github as type of the token.
|
2019-08-13 20:28:03 +02:00
|
|
|
TokenTypeGitHub TokenType = "github"
|
2020-05-26 05:48:10 +02:00
|
|
|
// TokenTypeGitLab defines gitlab as type of the token.
|
2019-08-13 20:28:03 +02:00
|
|
|
TokenTypeGitLab TokenType = "gitlab"
|
2020-05-26 05:48:10 +02:00
|
|
|
// TokenTypeGitea defines gitea as type of the token.
|
2019-08-26 09:31:38 +02:00
|
|
|
TokenTypeGitea TokenType = "gitea"
|
2019-06-29 16:02:40 +02:00
|
|
|
)
|
|
|
|
|
2020-05-26 05:48:10 +02:00
|
|
|
// Context carries along some data through the pipes.
|
2017-01-14 16:34:22 +02:00
|
|
|
type Context struct {
|
2022-06-22 06:48:11 +02:00
|
|
|
stdctx.Context
|
2023-09-16 22:01:20 +02:00
|
|
|
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
|
2022-02-01 03:36:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type Runtime struct {
|
|
|
|
Goos string
|
|
|
|
Goarch string
|
2019-01-19 20:57:58 +02:00
|
|
|
}
|
|
|
|
|
2020-05-26 05:48:10 +02:00
|
|
|
// Semver represents a semantic version.
|
2019-01-19 20:57:58 +02:00
|
|
|
type Semver struct {
|
2019-10-06 17:39:53 +02:00
|
|
|
Major uint64
|
|
|
|
Minor uint64
|
|
|
|
Patch uint64
|
2019-01-19 20:57:58 +02:00
|
|
|
Prerelease string
|
2017-04-14 17:07:40 +02:00
|
|
|
}
|
|
|
|
|
2020-05-26 05:48:10 +02:00
|
|
|
// New context.
|
2017-01-15 18:37:00 +02:00
|
|
|
func New(config config.Project) *Context {
|
2022-06-22 06:48:11 +02:00
|
|
|
return Wrap(stdctx.Background(), config)
|
2017-12-29 21:07:06 +02:00
|
|
|
}
|
|
|
|
|
2020-05-26 05:48:10 +02:00
|
|
|
// NewWithTimeout new context with the given timeout.
|
2022-06-22 06:48:11 +02:00
|
|
|
func NewWithTimeout(config config.Project, timeout time.Duration) (*Context, stdctx.CancelFunc) {
|
2022-09-11 20:32:23 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-05-26 05:48:10 +02:00
|
|
|
// Wrap wraps an existing context.
|
2022-06-22 06:48:11 +02:00
|
|
|
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,
|
2021-11-12 03:56:03 +02:00
|
|
|
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(),
|
2020-07-06 22:09:22 +02:00
|
|
|
Date: time.Now(),
|
2023-09-16 22:01:20 +02:00
|
|
|
Skips: map[string]bool{},
|
2022-02-01 03:36:22 +02:00
|
|
|
Runtime: Runtime{
|
|
|
|
Goos: runtime.GOOS,
|
|
|
|
Goarch: runtime.GOARCH,
|
|
|
|
},
|
2017-01-14 16:34:22 +02:00
|
|
|
}
|
|
|
|
}
|
2017-12-06 01:01:47 +02:00
|
|
|
|
2021-11-12 03:56:03 +02:00
|
|
|
// ToEnv converts a list of strings to an Env (aka a map[string]string).
|
|
|
|
func ToEnv(env []string) Env {
|
|
|
|
r := Env{}
|
2017-12-06 01:01:47 +02:00
|
|
|
for _, e := range env {
|
2022-03-17 04:28:13 +02:00
|
|
|
k, v, ok := strings.Cut(e, "=")
|
|
|
|
if !ok || k == "" {
|
2021-11-12 03:56:03 +02:00
|
|
|
continue
|
|
|
|
}
|
2022-03-17 04:28:13 +02:00
|
|
|
r[k] = v
|
2017-12-06 01:01:47 +02:00
|
|
|
}
|
|
|
|
return r
|
|
|
|
}
|