packer-cn/builder/googlecompute/template_funcs.go

49 lines
835 B
Go
Raw Normal View History

2017-10-15 11:53:18 -04:00
package googlecompute
import (
"strings"
"text/template"
)
func isalphanumeric(b byte) bool {
if '0' <= b && b <= '9' {
return true
}
if 'a' <= b && b <= 'z' {
return true
}
return false
}
// Clean up image name by replacing invalid characters with "-"
// truncate up to 63 length, convert to a lower case
2017-10-15 22:45:18 -04:00
func templateCleanImageName(s string) string {
if reImageFamily.MatchString(s) {
2017-10-15 11:53:18 -04:00
return s
}
b := []byte(strings.ToLower(s))
l := 63
if len(b) < 63 {
l = len(b)
}
newb := make([]byte, l)
for i := range newb {
if isalphanumeric(b[i]) {
newb[i] = b[i]
} else {
newb[i] = '-'
}
}
if !('a' <= newb[0] && newb[0] <= 'z') {
newb[0] = 'a'
}
if newb[l-1] == '-' {
newb[l-1] = 'a'
}
2017-10-15 11:53:18 -04:00
return string(newb)
}
var TemplateFuncs = template.FuncMap{
2017-10-15 22:45:18 -04:00
"clean_image_name": templateCleanImageName,
2017-10-15 11:53:18 -04:00
}