-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.go
71 lines (62 loc) · 1.95 KB
/
template.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
package modules
import (
"bytes"
"context"
"text/template"
"github.com/julian7/goshipdone/ctx"
"github.com/julian7/withenv"
)
// TemplateData is the data all template-based text replacement takes place.
// Modules are responsible of filling in the appropriate fields, and handle
// dependencies (for example, ArchiveName is often depends on ProjectName, therefore
// modules have to render them in ProjectName then ArchiveName order)
type TemplateData struct {
// Algo represents algorithm. Hashing and signing modules use them.
Algo string
// Arch defines target architecture
Arch string
// ArchName is a textual representation of Arch, optionally combining 32bit ARM versions
// eg. GOARCH=arm GOARM=6 means ArchName=armv6
ArchName string
// ArmVersion is the 32bit arm version (supported: 5, 6, 7)
ArmVersion int32
// ArchiveName defines a URL where the resource will be remotely available
ArchiveName string
// Env is a copy of environment variables set in ctx.Context
Env *withenv.Env
// Git is a copy of git-related info from ctx.Context
Git *ctx.GitData
// OS defines target operating system
OS string
// ProjectName defines local filename of the resource
ProjectName string
// Version defines artifact's version
Version string
// Ext contains executable extension
Ext string
}
func NewTemplate(cx context.Context) (*TemplateData, error) {
context, err := ctx.GetShipContext(cx)
if err != nil {
return nil, err
}
return &TemplateData{
Env: context.Env,
Git: context.Git,
ProjectName: context.ProjectName,
Version: context.Version,
}, nil
}
// Parse parses a string based on TemplateData, and returns output in string format
func (td *TemplateData) Parse(name, text string) (string, error) {
tmpl := template.New(name)
_, err := tmpl.Parse(text)
if err != nil {
return "", err
}
var out bytes.Buffer
if err := tmpl.Execute(&out, td); err != nil {
return "", err
}
return td.Env.Expand(out.String()), nil
}