forked from goreleaser/goreleaser
/
context.go
130 lines (116 loc) · 3.07 KB
/
context.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// 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.
package context
import (
ctx "context"
"os"
"strings"
"time"
"github.com/goreleaser/goreleaser/internal/artifact"
"github.com/goreleaser/goreleaser/pkg/config"
)
// GitInfo includes tags and diffs used in some point.
type GitInfo struct {
Branch string
CurrentTag string
Commit string
ShortCommit string
FullCommit string
CommitDate time.Time
URL string
}
// Env is the environment variables.
type Env map[string]string
// Copy returns a copy of the environment.
func (e Env) Copy() Env {
var 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 {
var 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
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"
)
// Context carries along some data through the pipes.
type Context struct {
ctx.Context
Config config.Project
Env Env
SkipTokenCheck bool
Token string
TokenType TokenType
Git GitInfo
Date time.Time
Artifacts artifact.Artifacts
ReleaseNotes string
ReleaseHeader string
ReleaseFooter string
Version string
Snapshot bool
SkipPostBuildHooks bool
SkipPublish bool
SkipSign bool
SkipValidate bool
RmDist bool
PreRelease bool
Deprecated bool
Parallelism int
Semver Semver
}
// Semver represents a semantic version.
type Semver struct {
Major uint64
Minor uint64
Patch uint64
RawVersion string
Prerelease string
}
// New context.
func New(config config.Project) *Context {
return Wrap(ctx.Background(), config)
}
// NewWithTimeout new context with the given timeout.
func NewWithTimeout(config config.Project, timeout time.Duration) (*Context, ctx.CancelFunc) {
ctx, cancel := ctx.WithTimeout(ctx.Background(), timeout)
return Wrap(ctx, config), cancel
}
// Wrap wraps an existing context.
func Wrap(ctx ctx.Context, config config.Project) *Context {
return &Context{
Context: ctx,
Config: config,
Env: splitEnv(append(os.Environ(), config.Env...)),
Parallelism: 4,
Artifacts: artifact.New(),
Date: time.Now(),
}
}
func splitEnv(env []string) map[string]string {
r := map[string]string{}
for _, e := range env {
p := strings.SplitN(e, "=", 2)
r[p[0]] = p[1]
}
return r
}