-
-
Notifications
You must be signed in to change notification settings - Fork 912
/
tmpl.go
134 lines (121 loc) · 3.3 KB
/
tmpl.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
131
132
133
134
// Package tmpl provides templating utilities for goreleser
package tmpl
import (
"bytes"
"strings"
"text/template"
"time"
"github.com/goreleaser/goreleaser/internal/artifact"
"github.com/goreleaser/goreleaser/pkg/context"
)
// Template holds data that can be applied to a template string
type Template struct {
fields fields
}
type fields map[string]interface{}
const (
// general keys
projectName = "ProjectName"
version = "Version"
tag = "Tag"
commit = "Commit"
shortCommit = "ShortCommit"
fullCommit = "FullCommit"
gitURL = "GitURL"
major = "Major"
minor = "Minor"
patch = "Patch"
env = "Env"
date = "Date"
timestamp = "Timestamp"
// artifact-only keys
os = "Os"
arch = "Arch"
arm = "Arm"
binary = "Binary"
artifactName = "ArtifactName"
// gitlab only
artifactUploadHash = "ArtifactUploadHash"
)
// New Template
func New(ctx *context.Context) *Template {
return &Template{
fields: fields{
projectName: ctx.Config.ProjectName,
version: ctx.Version,
tag: ctx.Git.CurrentTag,
commit: ctx.Git.Commit,
shortCommit: ctx.Git.ShortCommit,
fullCommit: ctx.Git.FullCommit,
gitURL: ctx.Git.URL,
env: ctx.Env,
date: time.Now().UTC().Format(time.RFC3339),
timestamp: time.Now().UTC().Unix(),
major: ctx.Semver.Major,
minor: ctx.Semver.Minor,
patch: ctx.Semver.Patch,
// TODO: no reason not to add prerelease here too I guess
},
}
}
// WithEnvS overrides template's env field with the given KEY=VALUE list of
// environment variables
func (t *Template) WithEnvS(envs []string) *Template {
var result = map[string]string{}
for _, env := range envs {
var parts = strings.SplitN(env, "=", 2)
result[parts[0]] = parts[1]
}
return t.WithEnv(result)
}
// WithEnv overrides template's env field with the given environment map
func (t *Template) WithEnv(e map[string]string) *Template {
t.fields[env] = e
return t
}
// WithArtifact populates fields from the artifact and replacements
func (t *Template) WithArtifact(a *artifact.Artifact, replacements map[string]string) *Template {
var bin = a.Extra[binary]
if bin == nil {
bin = t.fields[projectName]
}
t.fields[os] = replace(replacements, a.Goos)
t.fields[arch] = replace(replacements, a.Goarch)
t.fields[arm] = replace(replacements, a.Goarm)
t.fields[binary] = bin.(string)
t.fields[artifactName] = a.Name
if val, ok := a.Extra["ArtifactUploadHash"]; ok {
t.fields[artifactUploadHash] = val
} else {
t.fields[artifactUploadHash] = ""
}
return t
}
// Apply applies the given string against the fields stored in the template.
func (t *Template) Apply(s string) (string, error) {
var out bytes.Buffer
tmpl, err := template.New("tmpl").
Option("missingkey=error").
Funcs(template.FuncMap{
"replace": strings.ReplaceAll,
"time": func(s string) string {
return time.Now().UTC().Format(s)
},
"tolower": strings.ToLower,
"toupper": strings.ToUpper,
"trim": strings.TrimSpace,
}).
Parse(s)
if err != nil {
return "", err
}
err = tmpl.Execute(&out, t.fields)
return out.String(), err
}
func replace(replacements map[string]string, original string) string {
result := replacements[original]
if result == "" {
return original
}
return result
}