2019-05-31 18:37:43 -04:00
|
|
|
package common
|
2018-08-01 02:35:29 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"text/template"
|
2019-10-14 16:22:06 -04:00
|
|
|
|
|
|
|
packertpl "github.com/hashicorp/packer/common/template"
|
2018-08-01 02:35:29 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func isValidByteValue(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 b == '.' || b == '_' || b == '-'
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clean up image name by replacing invalid characters with "-"
|
|
|
|
// Names are not allowed to end in '.', '-', or '_' and are trimmed.
|
|
|
|
func templateCleanImageName(s string) string {
|
|
|
|
b := []byte(s)
|
|
|
|
newb := make([]byte, len(b))
|
|
|
|
for i := range newb {
|
|
|
|
if isValidByteValue(b[i]) {
|
|
|
|
newb[i] = b[i]
|
|
|
|
} else {
|
|
|
|
newb[i] = '-'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
newb = bytes.TrimRight(newb, "-_.")
|
|
|
|
return string(newb)
|
|
|
|
}
|
|
|
|
|
|
|
|
var TemplateFuncs = template.FuncMap{
|
2019-04-03 04:27:05 -04:00
|
|
|
"clean_resource_name": templateCleanImageName,
|
2019-10-14 14:35:16 -04:00
|
|
|
"clean_image_name": packertpl.DeprecatedTemplateFunc("clean_image_name", "clean_resource_name", templateCleanImageName),
|
2018-08-01 02:35:29 -04:00
|
|
|
}
|