forked from kube-burner/kube-burner
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Take advantage of the custom template functions in the main config re…
…ndering Signed-off-by: Raul Sevilla <[email protected]>
- Loading branch information
1 parent
07493f8
commit 3b8b3a1
Showing
5 changed files
with
85 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// Copyright 2021 The Kube-burner Authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package util | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"math/rand" | ||
"text/template" | ||
"time" | ||
) | ||
|
||
type templateOption string | ||
|
||
const ( | ||
MissingKeyError templateOption = "missingkey=error" | ||
) | ||
|
||
func init() { | ||
rand.Seed(time.Now().UnixNano()) | ||
} | ||
|
||
//RenderTemplate renders a go-template and adds several custom functions | ||
func RenderTemplate(original []byte, inputData interface{}, options templateOption) ([]byte, error) { | ||
var rendered bytes.Buffer | ||
funcMap := template.FuncMap{ | ||
"add": func(a int, b int) int { | ||
return a + b | ||
}, | ||
"multiply": func(a int, b int) int { | ||
return a * b | ||
}, | ||
"rand": func(length int) string { | ||
var letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789") | ||
b := make([]rune, length) | ||
for i := range b { | ||
b[i] = letterRunes[rand.Intn(len(letterRunes))] | ||
} | ||
return string(b) | ||
}, | ||
"sequence": func(start int, end int) []int { | ||
var sequence = []int{} | ||
for i := start; i <= end; i++ { | ||
sequence = append(sequence, i) | ||
} | ||
return sequence | ||
}, | ||
} | ||
t, err := template.New("").Option(string(options)).Funcs(funcMap).Parse(string(original)) | ||
if err != nil { | ||
return nil, fmt.Errorf("Parsing error: %s", err) | ||
} | ||
err = t.Execute(&rendered, inputData) | ||
if err != nil { | ||
return nil, fmt.Errorf("Rendering error: %s", err) | ||
} | ||
return rendered.Bytes(), nil | ||
} |