Skip to content

Commit b7c730b

Browse files
committed
feat: added stringsSplit, trimSpace, stringsReplace to the template name functionality
1 parent 84cf476 commit b7c730b

File tree

3 files changed

+64
-5
lines changed

3 files changed

+64
-5
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ The following data points can be used within your token name template. These are
176176
* path
177177
* ttl
178178
* access_level
179-
* scopes
179+
* scopes (csv string ex: api, sudo, read_api)
180180
* token_type
181181
* role_name
182182
* config_name
@@ -191,6 +191,9 @@ You can also use the following functions within your template:
191191
* `stringsJoin(elems []string, sep string) string` - joins a list of `elems` strings with a `sep`
192192
* `yesNoBool(in bool) string` - just return `yes` if `in` is true otherwise it returns `no`
193193
* `timeNowFormat(layout string) string` - layout is a go time format string layout
194+
* `stringsSplit(elems string, sep string) string` - splits a string `elems` with a `sep`
195+
* `trimSpace(s string) string` - trims the space from a string
196+
* `stringsReplace(s, old, new string, n int) string` - runs replace on the string
194197
195198
#### ttl
196199

internal/utils/name_tpl.go

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,29 @@ func timeNowFormat(layout string) string {
2727
return time.Now().UTC().Format(layout)
2828
}
2929

30+
func stringsJoin(elems []string, sep string) string {
31+
for i := range elems {
32+
elems[i] = strings.TrimSpace(elems[i])
33+
}
34+
return strings.Join(elems, sep)
35+
}
36+
37+
func stringsSplit(s, sep string) (out []string) {
38+
out = strings.Split(s, sep)
39+
for i := range out {
40+
out[i] = strings.TrimSpace(out[i])
41+
}
42+
return out
43+
}
44+
3045
var tplFuncMap = template.FuncMap{
31-
"randHexString": randHexString,
32-
"stringsJoin": strings.Join,
33-
"yesNoBool": yesNoBool,
34-
"timeNowFormat": timeNowFormat,
46+
"randHexString": randHexString,
47+
"stringsJoin": stringsJoin,
48+
"yesNoBool": yesNoBool,
49+
"timeNowFormat": timeNowFormat,
50+
"trimSpace": strings.TrimSpace,
51+
"stringsSplit": stringsSplit,
52+
"stringsReplace": strings.Replace,
3553
}
3654

3755
// TokenNameData defines an interface for objects that contain a token name and

internal/utils/name_tpl_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,44 @@ func TestTokenNameGenerator(t *testing.T) {
8080
false,
8181
},
8282

83+
{
84+
&tokenName{
85+
name: "{{ trimSpace .role_name }}",
86+
data: map[string]any{
87+
"role_name": " tests space trim ",
88+
},
89+
},
90+
"tests space trim",
91+
false,
92+
},
93+
94+
// with stringsSplit
95+
{
96+
&tokenName{
97+
name: "{{ .role_name }}-{{ .token_type }}-{{ stringsJoin (stringsSplit .scopes \",\") \"-\" }}-{{ yesNoBool .gitlab_revokes_token }}",
98+
data: map[string]any{
99+
"role_name": "test",
100+
"token_type": "personal",
101+
"scopes": "api, sudo",
102+
"gitlab_revokes_token": false,
103+
},
104+
},
105+
"test-personal-api-sudo-no",
106+
false,
107+
},
108+
109+
// stringsReplace
110+
{
111+
&tokenName{
112+
name: "{{ stringsReplace .role_name \" \" \"-\" -1 }}",
113+
data: map[string]any{
114+
"role_name": "test space replace",
115+
},
116+
},
117+
"test-space-replace",
118+
false,
119+
},
120+
83121
// with timeNowFormat
84122
{
85123
&tokenName{

0 commit comments

Comments
 (0)