github.com/goreleaser/goreleaser@v1.25.1/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 stdctx "context" 11 "os" 12 "runtime" 13 "strings" 14 "time" 15 16 "github.com/goreleaser/goreleaser/internal/artifact" 17 "github.com/goreleaser/goreleaser/pkg/config" 18 ) 19 20 // GitInfo includes tags and diffs used in some point. 21 type GitInfo struct { 22 Branch string 23 CurrentTag string 24 PreviousTag string 25 Commit string 26 ShortCommit string 27 FullCommit string 28 FirstCommit string 29 CommitDate time.Time 30 URL string 31 Summary string 32 TagSubject string 33 TagContents string 34 TagBody string 35 Dirty bool 36 } 37 38 // Env is the environment variables. 39 type Env map[string]string 40 41 // Copy returns a copy of the environment. 42 func (e Env) Copy() Env { 43 out := Env{} 44 for k, v := range e { 45 out[k] = v 46 } 47 return out 48 } 49 50 // Strings returns the current environment as a list of strings, suitable for 51 // os executions. 52 func (e Env) Strings() []string { 53 result := make([]string, 0, len(e)) 54 for k, v := range e { 55 result = append(result, k+"="+v) 56 } 57 return result 58 } 59 60 // TokenType is either github or gitlab. 61 type TokenType string 62 63 const ( 64 // TokenTypeGitHub defines github as type of the token. 65 TokenTypeGitHub TokenType = "github" 66 // TokenTypeGitLab defines gitlab as type of the token. 67 TokenTypeGitLab TokenType = "gitlab" 68 // TokenTypeGitea defines gitea as type of the token. 69 TokenTypeGitea TokenType = "gitea" 70 ) 71 72 type Action uint8 73 74 const ( 75 ActionNone Action = iota 76 ActionBuild 77 ActionRelease 78 ) 79 80 // Context carries along some data through the pipes. 81 type Context struct { 82 stdctx.Context 83 Action Action 84 Config config.Project 85 Env Env 86 Token string 87 TokenType TokenType 88 Git GitInfo 89 Date time.Time 90 Artifacts *artifact.Artifacts 91 ReleaseURL string 92 ReleaseNotes string 93 ReleaseNotesFile string 94 ReleaseNotesTmpl string 95 ReleaseHeaderFile string 96 ReleaseHeaderTmpl string 97 ReleaseFooterFile string 98 ReleaseFooterTmpl string 99 Version string 100 ModulePath string 101 PartialTarget string 102 Snapshot bool 103 FailFast bool 104 Partial bool 105 SkipTokenCheck bool 106 Clean bool 107 PreRelease bool 108 Deprecated bool 109 Parallelism int 110 Semver Semver 111 Runtime Runtime 112 Skips map[string]bool 113 } 114 115 type Runtime struct { 116 Goos string 117 Goarch string 118 } 119 120 // Semver represents a semantic version. 121 type Semver struct { 122 Major uint64 123 Minor uint64 124 Patch uint64 125 Prerelease string 126 } 127 128 // New context. 129 func New(config config.Project) *Context { 130 return Wrap(stdctx.Background(), config) 131 } 132 133 // NewWithTimeout new context with the given timeout. 134 func NewWithTimeout(config config.Project, timeout time.Duration) (*Context, stdctx.CancelFunc) { 135 ctx, cancel := stdctx.WithTimeout(stdctx.Background(), timeout) // nosem 136 return Wrap(ctx, config), cancel 137 } 138 139 // Wrap wraps an existing context. 140 func Wrap(ctx stdctx.Context, config config.Project) *Context { 141 return &Context{ 142 Context: ctx, 143 Config: config, 144 Env: ToEnv(append(os.Environ(), config.Env...)), 145 Parallelism: 4, 146 Artifacts: artifact.New(), 147 Date: time.Now(), 148 Skips: map[string]bool{}, 149 Runtime: Runtime{ 150 Goos: runtime.GOOS, 151 Goarch: runtime.GOARCH, 152 }, 153 } 154 } 155 156 // ToEnv converts a list of strings to an Env (aka a map[string]string). 157 func ToEnv(env []string) Env { 158 r := Env{} 159 for _, e := range env { 160 k, v, ok := strings.Cut(e, "=") 161 if !ok || k == "" { 162 continue 163 } 164 r[k] = v 165 } 166 return r 167 }