github.com/devster/tarreleaser@v0.0.0-20221207180803-c608f4eb8918/pkg/tmpl/tmpl.go (about)

     1  package tmpl
     2  
     3  import (
     4  	"bytes"
     5  	"github.com/devster/tarreleaser/pkg/context"
     6  	"text/template"
     7  	"time"
     8  )
     9  
    10  // Template holds data that can be applied to a template string
    11  type Template struct {
    12  	fields fields
    13  }
    14  
    15  type fields map[string]interface{}
    16  
    17  func New(ctx *context.Context) *Template {
    18  	return &Template{
    19  		fields: fields{
    20  			"Tag":         ctx.Git.CurrentTag,
    21  			"ShortCommit": ctx.Git.ShortCommit,
    22  			"FullCommit":  ctx.Git.FullCommit,
    23  			"Branch":      ctx.Git.Branch,
    24  			"Commit":      ctx.Git.Commit,
    25  			"Date":        ctx.Date.Format(time.RFC3339),
    26  			"Timestamp":   ctx.Date.Unix(),
    27  			"Env":         ctx.Env,
    28  			"Archive":     ctx.Archive,
    29  		},
    30  	}
    31  }
    32  
    33  func (t *Template) Apply(s string) (string, error) {
    34  	var out bytes.Buffer
    35  	tmpl, err := template.New("tmpl").Option("missingkey=error").Parse(s)
    36  	if err != nil {
    37  		return "", err
    38  	}
    39  
    40  	err = tmpl.Execute(&out, t.fields)
    41  	return out.String(), err
    42  }