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

     1  package common
     2  
     3  import (
     4  	"bytes"
     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  	if 'A' <= b && b <= 'Z' {
    16  		return true
    17  	}
    18  	return false
    19  }
    20  
    21  // Clean up AMI name by replacing invalid characters with "-"
    22  // For allowed characters see docs for Name parameter
    23  // at http://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_CreateImage.html
    24  func templateCleanAMIName(s string) string {
    25  	allowed := []byte{'(', ')', '[', ']', ' ', '.', '/', '-', '\'', '@', '_'}
    26  	b := []byte(s)
    27  	newb := make([]byte, len(b))
    28  	for i, c := range b {
    29  		if isalphanumeric(c) || bytes.IndexByte(allowed, c) != -1 {
    30  			newb[i] = c
    31  		} else {
    32  			newb[i] = '-'
    33  		}
    34  	}
    35  	return string(newb[:])
    36  }
    37  
    38  var TemplateFuncs = template.FuncMap{
    39  	"clean_ami_name": templateCleanAMIName,
    40  }