2017-10-16 00:53:18 +09: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 "-"
|
2017-10-19 10:45:48 +09:00
|
|
|
// and converting upper cases to lower cases
|
2017-10-16 11:45:18 +09:00
|
|
|
func templateCleanImageName(s string) string {
|
2019-09-16 10:10:37 -07:00
|
|
|
if validImageName.MatchString(s) {
|
2017-10-16 00:53:18 +09:00
|
|
|
return s
|
|
|
|
}
|
|
|
|
b := []byte(strings.ToLower(s))
|
2017-10-19 10:45:48 +09:00
|
|
|
newb := make([]byte, len(b))
|
2017-10-16 00:53:18 +09:00
|
|
|
for i := range newb {
|
|
|
|
if isalphanumeric(b[i]) {
|
|
|
|
newb[i] = b[i]
|
|
|
|
} else {
|
|
|
|
newb[i] = '-'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return string(newb)
|
|
|
|
}
|
|
|
|
|
|
|
|
var TemplateFuncs = template.FuncMap{
|
2019-04-03 10:27:05 +02:00
|
|
|
"clean_resource_name": templateCleanImageName,
|
2017-10-16 00:53:18 +09:00
|
|
|
}
|