-
Notifications
You must be signed in to change notification settings - Fork 7
/
template.go
91 lines (81 loc) · 2.17 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
86
87
88
89
90
91
package template
import (
"encoding/json"
"fmt"
"io"
"text/template"
"time"
"github.com/dustin/go-humanize"
"github.com/lunarway/release-manager/internal/intent"
)
// Output parses the template text as a Go template. The empty interface is
// available as the root object in the template.
//
// Some utility functions are available for data manipulation.
func Output(destination io.Writer, name, text string, data interface{}) error {
t := template.New(name)
t.Funcs(FuncMap())
t, err := t.Parse(text)
if err != nil {
return fmt.Errorf("invalid template: %v", err)
}
return t.Execute(destination, data)
}
func tmplRightPad(s string, padding int) string {
template := fmt.Sprintf("%%-%ds", padding)
return fmt.Sprintf(template, s)
}
func tmplMaxLength(list interface{}, key string) (int, error) {
// this is a poor mans generic implementation of finding the maximum length of
// a value based on a key. We change the type of the list to a
// map[string]interface{} which makes key lookups safe.
bytes, err := json.Marshal(list)
if err != nil {
return 0, err
}
var mapedList []map[string]interface{}
err = json.Unmarshal(bytes, &mapedList)
if err != nil {
return 0, err
}
var max int
for _, m := range mapedList {
if s, ok := m[key].(string); ok {
if len(s) > max {
max = len(s)
}
}
}
return max, nil
}
func FuncMap() template.FuncMap {
return template.FuncMap{
"rightPad": tmplRightPad,
"maxLength": tmplMaxLength,
"add": func(a, b int) int {
return a + b
},
"dateFormat": func() string {
return "2006-01-02 15:04:05"
},
"humanizeTime": func(input time.Time) string {
return humanize.Time(input)
},
}
}
func IntentString(i intent.Intent) string {
switch i.Type {
case intent.TypeAutoRelease:
return "autorelease"
case intent.TypePromote:
return fmt.Sprintf("promotion from %s", i.Promote.FromEnvironment)
case intent.TypeReleaseArtifact:
return "artifact release"
case intent.TypeReleaseBranch:
return fmt.Sprintf("%s branch release", i.ReleaseBranch.Branch)
case intent.TypeRollback:
return fmt.Sprintf("rollback of %s", i.Rollback.PreviousArtifactID)
default:
return fmt.Sprintf("unknown intent type '%s'", i.Type)
}
}