1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-01-10 03:47:03 +02:00
goreleaser/internal/tmpl/tmpl.go

100 lines
2.2 KiB
Go
Raw Normal View History

// Package tmpl provides templating utilities for goreleser
package tmpl
import (
"bytes"
"text/template"
"time"
"github.com/goreleaser/goreleaser/context"
"github.com/goreleaser/goreleaser/internal/artifact"
"github.com/masterminds/semver"
"github.com/pkg/errors"
)
2018-07-09 06:13:09 +02:00
// Template holds data that can be applied to a template string
type Template struct {
fields fields
}
2018-07-09 07:35:44 +02:00
type fields map[string]interface{}
2018-07-09 07:35:44 +02:00
const (
// general keys
kProjectName = "ProjectName"
kVersion = "Version"
kTag = "Tag"
kCommit = "Commit"
kMajor = "Major"
kMinor = "Minor"
kPatch = "Patch"
kEnv = "Env"
// artifact-only keys
kOs = "Os"
kArch = "Arch"
kArm = "Arm"
kBinary = "Binary"
)
2018-07-09 06:13:09 +02:00
// New Template
func New(ctx *context.Context) *Template {
return &Template{
fields: fields{
2018-07-09 07:35:44 +02:00
kProjectName: ctx.Config.ProjectName,
kVersion: ctx.Version,
kTag: ctx.Git.CurrentTag,
kCommit: ctx.Git.Commit,
kEnv: ctx.Env,
},
}
}
2018-07-09 06:13:09 +02:00
// WithArtifacts populate fields from the artifact and replacements
func (t *Template) WithArtifact(a artifact.Artifact, replacements map[string]string) *Template {
2018-07-09 07:35:44 +02:00
var binary = a.Extra[kBinary]
if binary == "" {
2018-07-09 07:35:44 +02:00
binary = t.fields[kProjectName].(string)
}
2018-07-09 07:35:44 +02:00
t.fields[kOs] = replace(replacements, a.Goos)
t.fields[kArch] = replace(replacements, a.Goarch)
t.fields[kArm] = replace(replacements, a.Goarm)
t.fields[kBinary] = binary
return t
}
2018-07-09 06:13:09 +02:00
// 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{
"time": func(s string) string {
return time.Now().UTC().Format(s)
},
}).
Parse(s)
if err != nil {
return "", err
}
2018-07-09 07:35:44 +02:00
sv, err := semver.NewVersion(t.fields[kTag].(string))
if err != nil {
return "", errors.Wrap(err, "tmpl")
}
2018-07-09 07:35:44 +02:00
t.fields[kMajor] = sv.Major()
t.fields[kMinor] = sv.Minor()
t.fields[kPatch] = sv.Patch()
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
}