-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplate.go
More file actions
134 lines (107 loc) · 3.13 KB
/
template.go
File metadata and controls
134 lines (107 loc) · 3.13 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package main
import (
"bytes"
"crypto/sha1"
"encoding/base64"
"fmt"
"io"
"text/template"
"time"
"github.com/pkg/errors"
)
// Funcs keep all functions and vars available for template
type Funcs struct {
vars *Vars
}
// NewFuncs creates new Funcs struct that includes initialized Vars.
func NewFuncs(vars *Vars) *Funcs {
return &Funcs{vars: vars}
}
// WSSEPasswordDigest returns password digest according to Web Service Security specification.
//
// Password_Digest = Base64 ( SHA-1 ( nonce + created + password ) )
// https://www.oasis-open.org/committees/download.php/13392/wss-v1.1-spec-pr-UsernameTokenProfile-01.htm
func (ctx *Funcs) WSSEPasswordDigest(nonce, created, password string) string {
h := sha1.New()
io.WriteString(h, nonce+created+password)
return base64.StdEncoding.EncodeToString(h.Sum(nil))
}
// Base64 transformation of provided string
func (ctx *Funcs) Base64(value string) string {
return base64.StdEncoding.EncodeToString([]byte(value))
}
// SHA1 returns string representation of SHA1 hash bytes
func (ctx *Funcs) SHA1(value string) string {
// fmt.Println("Calculating SHA1: " + value)
h := sha1.New()
io.WriteString(h, value)
return fmt.Sprintf("%x", h.Sum(nil))
}
// FormatDateTime presents time in string according to provided format
func (ctx *Funcs) FormatDateTime(fmt string, t time.Time) string {
return t.Format(fmt)
}
// DaysFromNow adds/substracts days from provided time
func (ctx *Funcs) DaysFromNow(days int) time.Time {
years := 0
months := 0
return time.Now().AddDate(years, months, days)
}
// CurrentTimestampSec returns current time in Unix format
func (ctx *Funcs) CurrentTimestampSec() int64 {
return time.Now().Unix()
}
// Now returns current time
// IANA timezone could be optionally specified
func (ctx *Funcs) Now(tz ...string) time.Time {
if len(tz) == 0 {
return time.Now()
}
loc, err := time.LoadLocation(tz[0])
if err != nil {
return time.Now()
}
return time.Now().In(loc)
}
// TemplateContext backs and executes template
type TemplateContext struct {
funcs *Funcs
vars *Vars
errors []error
}
// NewTemplateContext creates new processor.
func NewTemplateContext(vars *Vars) *TemplateContext {
return &TemplateContext{
funcs: NewFuncs(vars),
vars: vars,
}
}
// HasErrors checks either at least on error happend during processing or not.
func (ctx *TemplateContext) HasErrors() bool {
return len(ctx.errors) > 0
}
// Error returns all errors throwned during template(s) processing combine in one error
func (ctx *TemplateContext) Error() error {
msg := ""
for _, err := range ctx.errors {
msg = fmt.Sprintln(err.Error())
}
return errors.New(msg)
}
// ApplyTo takes value template and evaluates all variables and functions inside it.
func (ctx *TemplateContext) ApplyTo(tmpl string) string {
// variable's syntax could be used inside the template, evaluate vars first
tmpl = ctx.vars.ApplyTo(tmpl)
t, err := template.New("").Parse(tmpl)
if err != nil {
ctx.errors = append(ctx.errors, err)
return ""
}
output := bytes.NewBufferString("")
err = t.Execute(output, ctx.funcs)
if err != nil {
ctx.errors = append(ctx.errors, err)
return ""
}
return output.String()
}