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

58 lines
1.5 KiB
Go
Raw Normal View History

// Package filenametemplate contains the code used to template names of
// goreleaser's packages and archives.
package filenametemplate
2017-12-27 01:10:37 +02:00
import (
"bytes"
"text/template"
2017-12-27 01:10:37 +02:00
"github.com/goreleaser/goreleaser/context"
"github.com/goreleaser/goreleaser/internal/artifact"
)
2017-12-27 02:51:28 +02:00
// Fields contains all accepted fields in the template string
2017-12-27 01:10:37 +02:00
type Fields struct {
Version string
Tag string
ProjectName string
Env map[string]string
Os string
Arch string
Arm string
Binary string
}
2017-12-27 02:51:28 +02:00
// NewFields returns a Fields instances filled with the data provided
2017-12-27 01:10:37 +02:00
func NewFields(ctx *context.Context, a artifact.Artifact, replacements map[string]string) Fields {
return Fields{
Env: ctx.Env,
Version: ctx.Version,
Tag: ctx.Git.CurrentTag,
ProjectName: ctx.Config.ProjectName,
Binary: a.Extra["Binary"],
2017-12-27 01:10:37 +02:00
Os: replace(replacements, a.Goos),
Arch: replace(replacements, a.Goarch),
Arm: replace(replacements, a.Goarm),
}
}
2017-12-27 02:51:28 +02:00
// Apply applies the given fields to the given template and returns the
// evaluation and any error that might occur.
2017-12-27 01:10:37 +02:00
func Apply(tmpl string, fields Fields) (string, error) {
t, err := template.New(tmpl).Parse(tmpl)
2017-12-27 01:10:37 +02:00
if err != nil {
return "", err
}
var out bytes.Buffer
err = t.Execute(&out, fields)
return out.String(), err
}
func replace(replacements map[string]string, original string) string {
result := replacements[original]
if result == "" {
return original
}
return result
}