-
Notifications
You must be signed in to change notification settings - Fork 23
/
plush.go
47 lines (41 loc) · 993 Bytes
/
plush.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
package fix
import (
"io/ioutil"
"time"
"github.com/gobuffalo/packr"
"github.com/gobuffalo/plush"
"github.com/gobuffalo/uuid"
"github.com/pkg/errors"
"golang.org/x/crypto/bcrypt"
)
func render(file packr.File) (string, error) {
b, err := ioutil.ReadAll(file)
if err != nil {
return "", errors.WithStack(err)
}
return plush.Render(string(b), plush.NewContextWith(map[string]interface{}{
"uuid": func() uuid.UUID {
u, _ := uuid.NewV4()
return u
},
"uuidNamed": uuidNamed,
"now": time.Now,
"hash": hash,
}))
}
func hash(s string, opts map[string]interface{}, help plush.HelperContext) (string, error) {
cost := bcrypt.DefaultCost
if i, ok := opts["cost"].(int); ok {
cost = i
}
ph, err := bcrypt.GenerateFromPassword([]byte(s), cost)
return string(ph), err
}
func uuidNamed(name string, help plush.HelperContext) uuid.UUID {
u, _ := uuid.NewV4()
if ux, ok := help.Value(name).(uuid.UUID); ok {
return ux
}
help.Set(name, u)
return u
}