-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.go
85 lines (68 loc) · 1.67 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package k6dist
import (
"bytes"
"os"
"path/filepath"
"text/template"
"github.com/Masterminds/semver/v3"
sprig "github.com/go-task/slim-sprig/v3"
"github.com/grafana/k6dist/internal/registry"
)
type instanceData struct {
Name string
Version string
OS string
Arch string
ExeExt string
ZipExt string
}
type releaseData struct {
Name string
Version string
Registry registry.Registry
Footer string
}
func newInstsanceData(name string, version *semver.Version, platform *Platform) *instanceData {
exe := ""
zip := ".tar.gz"
if platform.OS == "windows" {
exe = ".exe"
zip = ".zip"
}
return &instanceData{
Name: name,
Version: version.Original(),
OS: platform.OS,
Arch: platform.Arch,
ExeExt: exe,
ZipExt: zip,
}
}
func newReleaseData(name string, version *semver.Version, reg registry.Registry) (*releaseData, error) {
footer, err := notesFooter(version, reg)
if err != nil {
return nil, err
}
return &releaseData{Name: name, Version: version.Original(), Registry: reg, Footer: footer}, nil
}
func expandTemplate(name string, tmplsrc string, data interface{}) (string, error) {
tmpl, err := template.New(name).Funcs(sprig.TxtFuncMap()).Parse(tmplsrc)
if err != nil {
return "", err
}
var buff bytes.Buffer
if err := tmpl.Execute(&buff, data); err != nil {
return "", err
}
return buff.String(), nil
}
func expandAsTargetPath(name string, tmplsrc string, data interface{}) (string, error) {
filename, err := expandTemplate(name, tmplsrc, data)
if err != nil {
return "", err
}
if err := os.MkdirAll(filepath.Dir(filename), 0o700); err != nil { //nolint:forbidigo
return "", err
}
return filename, nil
}