2013-09-07 21:42:50 -04:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"text/template"
|
|
|
|
)
|
|
|
|
|
|
|
|
func isalphanumeric(b byte) bool {
|
|
|
|
if '0' <= b && b <= '9' {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if 'a' <= b && b <= 'z' {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if 'A' <= b && b <= 'Z' {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clean up AMI name by replacing invalid characters with "-"
|
|
|
|
func templateCleanAMIName(s string) string {
|
2015-06-10 17:19:36 -04:00
|
|
|
allowed := []byte{'(', ')', ',', '/', '-', '_', ' '}
|
2013-09-07 21:42:50 -04:00
|
|
|
b := []byte(s)
|
|
|
|
newb := make([]byte, len(b))
|
|
|
|
for i, c := range b {
|
|
|
|
if isalphanumeric(c) || bytes.IndexByte(allowed, c) != -1 {
|
|
|
|
newb[i] = c
|
|
|
|
} else {
|
|
|
|
newb[i] = '-'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return string(newb[:])
|
|
|
|
}
|
|
|
|
|
|
|
|
var TemplateFuncs = template.FuncMap{
|
|
|
|
"clean_ami_name": templateCleanAMIName,
|
|
|
|
}
|