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

     1  package arm
     2  
     3  import (
     4  	"bytes"
     5  	"text/template"
     6  )
     7  
     8  func isValidByteValue(b byte) bool {
     9  	if '0' <= b && b <= '9' {
    10  		return true
    11  	}
    12  	if 'a' <= b && b <= 'z' {
    13  		return true
    14  	}
    15  	if 'A' <= b && b <= 'Z' {
    16  		return true
    17  	}
    18  	return b == '.' || b == '_' || b == '-'
    19  }
    20  
    21  // Clean up image name by replacing invalid characters with "-"
    22  // Names are not allowed to end in '.', '-', or  '_' and are trimmed.
    23  func templateCleanImageName(s string) string {
    24  	if ok, _ := assertManagedImageName(s, ""); ok {
    25  		return s
    26  	}
    27  	b := []byte(s)
    28  	newb := make([]byte, len(b))
    29  	for i := range newb {
    30  		if isValidByteValue(b[i]) {
    31  			newb[i] = b[i]
    32  		} else {
    33  			newb[i] = '-'
    34  		}
    35  	}
    36  
    37  	newb = bytes.TrimRight(newb, "-_.")
    38  	return string(newb)
    39  }
    40  
    41  var TemplateFuncs = template.FuncMap{
    42  	"clean_image_name": templateCleanImageName,
    43  }