packer-cn/builder/googlecompute/template_funcs.go

42 lines
871 B
Go
Raw Normal View History

2017-10-15 11:53:18 -04:00
package googlecompute
import (
"strings"
"text/template"
packertpl "github.com/hashicorp/packer/common/template"
2017-10-15 11:53:18 -04:00
)
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 "-"
// and converting upper cases to lower cases
2017-10-15 22:45:18 -04:00
func templateCleanImageName(s string) string {
if validImageName.MatchString(s) {
2017-10-15 11:53:18 -04:00
return s
}
b := []byte(strings.ToLower(s))
newb := make([]byte, len(b))
2017-10-15 11:53:18 -04:00
for i := range newb {
if isalphanumeric(b[i]) {
newb[i] = b[i]
} else {
newb[i] = '-'
}
}
return string(newb)
}
var TemplateFuncs = template.FuncMap{
"clean_resource_name": templateCleanImageName,
"clean_image_name": packertpl.DeprecatedTemplateFunc("clean_image_name", "clean_resource_name", templateCleanImageName),
2017-10-15 11:53:18 -04:00
}