-
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.
Add helper functions for the template
- Loading branch information
1 parent
730116f
commit 5766162
Showing
4 changed files
with
59 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package main | ||
|
||
import ( | ||
"html/template" | ||
"strings" | ||
"time" | ||
|
||
"sigs.k8s.io/yaml" | ||
) | ||
|
||
var templateFuncs = template.FuncMap{ | ||
"fromMultiYAML": fromMultiYAML, | ||
"toYAML": toYAML, | ||
"parseTime": parseTime, | ||
} | ||
|
||
// fromMultiYAML turns a string of multiple YAML documents | ||
// (https://yaml.org/spec/1.2.2/#22-structures) into a slice of maps. | ||
func fromMultiYAML(marshalled string) ([]map[string]interface{}, error) { | ||
parts := strings.Split(strings.TrimPrefix(strings.TrimSpace(marshalled), "---"), "---") | ||
res := []map[string]interface{}{} | ||
for _, p := range parts { | ||
v := make(map[string]interface{}) | ||
err := yaml.Unmarshal([]byte(p), &v) | ||
if err != nil { | ||
return nil, err | ||
} | ||
res = append(res, v) | ||
} | ||
return res, nil | ||
} | ||
|
||
// toYAML turns the given object into a YAML string. | ||
func toYAML(unmarshalled any) (string, error) { | ||
b, err := yaml.Marshal(unmarshalled) | ||
return string(b), err | ||
} | ||
|
||
// parseTime parses a string using the specified layout into a time.Time. | ||
func parseTime(layout, t string) (time.Time, error) { | ||
return time.Parse(layout, t) | ||
} |
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