-
Notifications
You must be signed in to change notification settings - Fork 7
/
template.go
50 lines (45 loc) · 1.31 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
package template
import (
"fmt"
"io"
"text/template"
"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(template.FuncMap{
"rightPad": tmplRightPad,
"dateFormat": func() string {
return "2006-01-02 15:04:05"
},
})
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 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)
}
}