github.phpd.cn/hashicorp/packer@v1.3.2/builder/googlecompute/template_funcs.go (about)

     1  package googlecompute
     2  
     3  import (
     4  	"strings"
     5  	"text/template"
     6  )
     7  
     8  func isalphanumeric(b byte) bool {
     9  	if '0' <= b && b <= '9' {
    10  		return true
    11  	}
    12  	if 'a' <= b && b <= 'z' {
    13  		return true
    14  	}
    15  	return false
    16  }
    17  
    18  // Clean up image name by replacing invalid characters with "-"
    19  // and converting upper cases to lower cases
    20  func templateCleanImageName(s string) string {
    21  	if reImageFamily.MatchString(s) {
    22  		return s
    23  	}
    24  	b := []byte(strings.ToLower(s))
    25  	newb := make([]byte, len(b))
    26  	for i := range newb {
    27  		if isalphanumeric(b[i]) {
    28  			newb[i] = b[i]
    29  		} else {
    30  			newb[i] = '-'
    31  		}
    32  	}
    33  	return string(newb)
    34  }
    35  
    36  var TemplateFuncs = template.FuncMap{
    37  	"clean_image_name": templateCleanImageName,
    38  }