github.com/jpreese/tflint@v0.19.2-0.20200908152133-b01686250fb6/rules/awsrules/generator-utils/main.go (about) 1 package utils 2 3 import ( 4 "fmt" 5 "os" 6 "strings" 7 "text/template" 8 9 "github.com/serenize/snaker" 10 ) 11 12 // ToCamel converts a string to CamelCase 13 func ToCamel(str string) string { 14 exceptions := map[string]string{ 15 "ami": "AMI", 16 "db": "DB", 17 "alb": "ALB", 18 "elb": "ELB", 19 "elasticache": "ElastiCache", 20 "iam": "IAM", 21 } 22 for pattern, conv := range exceptions { 23 str = strings.Replace(str, "_"+pattern+"_", "_"+conv+"_", -1) 24 str = strings.Replace(str, pattern+"_", conv+"_", -1) 25 str = strings.Replace(str, "_"+pattern, "_"+conv, -1) 26 } 27 return snaker.SnakeToCamel(str) 28 } 29 30 // GenerateFile generates a new file from the passed template and metadata 31 func GenerateFile(fileName string, tmplName string, meta interface{}) { 32 file, err := os.Create(fileName) 33 if err != nil { 34 panic(err) 35 } 36 37 tmpl := template.Must(template.ParseFiles(tmplName)) 38 err = tmpl.Execute(file, meta) 39 if err != nil { 40 panic(err) 41 } 42 } 43 44 // GenerateFileWithLogs generates a new file from the passed template and metadata 45 // The difference from GenerateFile function is to output logs 46 func GenerateFileWithLogs(fileName string, tmplName string, meta interface{}) { 47 GenerateFile(fileName, tmplName, meta) 48 fmt.Printf("Create: %s\n", fileName) 49 }