-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathname_tpl.go
51 lines (46 loc) · 1 KB
/
name_tpl.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
package gitlab
import (
"crypto/rand"
"fmt"
"strings"
"text/template"
"time"
_ "unsafe"
)
func yesNoBool(in bool) string {
if in {
return "yes"
}
return "no"
}
func randHexString(bytes int) string {
buf := make([]byte, bytes)
_, _ = rand.Read(buf)
return fmt.Sprintf("%x", buf)
}
func timeNowFormat(layout string) string {
return time.Now().UTC().Format(layout)
}
var tplFuncMap = template.FuncMap{
"randHexString": randHexString,
"stringsJoin": strings.Join,
"yesNoBool": yesNoBool,
"timeNowFormat": timeNowFormat,
}
func TokenName(role *EntryRole) (name string, err error) {
if role == nil {
return "", fmt.Errorf("role: %w", ErrNilValue)
}
var tpl *template.Template
tpl, err = template.New("name").Funcs(tplFuncMap).Parse(role.Name)
if err != nil {
return "", err
}
buf := new(strings.Builder)
var data = role.LogicalResponseData()
data["unix_timestamp_utc"] = time.Now().UTC().Unix()
delete(data, "name")
err = tpl.Execute(buf, data)
name = buf.String()
return name, err
}