diff --git a/.gitignore b/.gitignore index fc5b313..a2b158a 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ terraformer dist .DS_Store +gen \ No newline at end of file diff --git a/README.md b/README.md index 5ca90ac..d2cb2be 100644 --- a/README.md +++ b/README.md @@ -116,7 +116,7 @@ Go template processing is can be easily extended by special functions. The first | feature | description | example | |---------|-------------|---------| | tfStringListFormater | formats a list with quoted elements | [1,2,3] -> ["1","2","3"] | -| tfCallback | call a script which prints to stdout | read dynamic values in tf.template | +| tfCallback | call a script which prints to stdout (relative to the template file) | read dynamic values in tf.template | | more to come | provide a pull request | f(x) | ## try out diff --git a/examples/aws/tf.template b/examples/aws/tf.template index 56a7199..eec1358 100644 --- a/examples/aws/tf.template +++ b/examples/aws/tf.template @@ -13,5 +13,5 @@ resource "aws_route53_record" "some-dns-record" { } resource "dummy" { - callback = "{{ tfCallback "examples/aws/callback.sh" }}" + callback = "{{ tfCallback "callback.sh" }}" } \ No newline at end of file diff --git a/runSamples.sh b/runSamples.sh new file mode 100755 index 0000000..f5ab7e9 --- /dev/null +++ b/runSamples.sh @@ -0,0 +1,16 @@ +mkdir -p gen/examples + +go run terraformer.go ctx > gen/examples/context1.yml +go run terraformer.go ctx --state=./examples/context/terraform.tfstate > gen/examples/context2.yml +go run terraformer.go ctx --callback=./examples/context/callback.sh > gen/examples/context3.yml +go run terraformer.go ctx ./examples/context/config1.yml > gen/examples/context4.yml + +go run terraformer.go gen ./examples/aws/tf.template ./examples/aws/context.yml > gen/examples/tf.main + +error=$(cat gen/examples/* | grep error) + +if [ "$error" == "" ]; then + ls -al gen/examples +else + echo "--$error--" +fi diff --git a/terraformer.go b/terraformer.go index b7a4f7f..4a2dbf3 100644 --- a/terraformer.go +++ b/terraformer.go @@ -20,21 +20,24 @@ var ( date string = "unknown" ) +var ( + app = kingpin.New("terraformer", "A go program that generates terraform files using go templates") + + commandGenerate = app.Command("generate", "generate a terraform file (main.tf), alias=gen").Alias("gen") + templateFile = commandGenerate.Arg("terraform-template", "path to a go template file").Required().ExistingFile() + contextFile = commandGenerate.Arg("context", "path to a yaml file").Required().ExistingFile() + + commandGenerateContext = app.Command("generate-context", "generate a context yaml file, alias=ctx").Alias("ctx") + stateFlag = commandGenerateContext.Flag("state", "(optional) path to a terraform.tfsate file").Short('s').ExistingFile() + templateFlag = commandGenerateContext.Flag("template", "(optional) path to a go template file").Short('t').ExistingFile() + callbackFlag = commandGenerateContext.Flag("callback", "(optional) list of executable script file printing YAML to stdout").Short('c').ExistingFile() + configFiles = commandGenerateContext.Arg("config-files", "(optional) list of yaml files").ExistingFiles() +) + func main() { - app := kingpin.New("terraformer", "A go program that generates terraform files using go templates") app.Version(printVersion()).Author("Stephan Klevenz") app.HelpFlag.Short('h') - commandGenerate := app.Command("generate", "generate a terraform file (main.tf), alias=gen").Alias("gen") - templateFile := commandGenerate.Arg("terraform-template", "path to a go template file").Required().ExistingFile() - contextFile := commandGenerate.Arg("context", "path to a yaml file").Required().ExistingFile() - - commandGenerateContext := app.Command("generate-context", "generate a context yaml file, alias=ctx").Alias("ctx") - stateFlag := commandGenerateContext.Flag("state", "(optional) path to a terraform.tfsate file").Short('s').ExistingFile() - templateFlag := commandGenerateContext.Flag("template", "(optional) path to a go template file").Short('t').ExistingFile() - callbackFlag := commandGenerateContext.Flag("callback", "(optional) list of executable script file printing YAML to stdout").Short('c').ExistingFile() - configFiles := commandGenerateContext.Arg("config-files", "(optional) list of yaml files").ExistingFiles() - s := kingpin.MustParse(app.Parse(os.Args[1:])) switch s { @@ -51,7 +54,7 @@ func main() { func generate(templateFile string, contextFile string) { funcMap := template.FuncMap{ "tfStringListFormater": tfStringListFormater, - "tfCallback": tfCallback, + "tfCallback": tfCallback, } templateName := path.Base(templateFile) @@ -137,7 +140,7 @@ func readYamlFile(filePath string) interface{} { func readYamlCallback(scriptFile string) interface{} { var err error yamlBytes, err := exec.Command(scriptFile).Output() - if (err != nil){ + if err != nil { fmt.Printf("error: %v\n", err) } @@ -195,8 +198,11 @@ func printVersion() string { func tfCallback(scriptFile string) string { + // call script relative to template file + scriptFile = path.Join(path.Dir(*templateFile), scriptFile) + out, err := exec.Command(scriptFile).Output() - if (err != nil){ + if err != nil { fmt.Printf("error: %v\n", err) } return strings.TrimSuffix(string(out), "\n") diff --git a/terraformer_test.go b/terraformer_test.go index 991b39e..1fe1581 100644 --- a/terraformer_test.go +++ b/terraformer_test.go @@ -65,12 +65,10 @@ func TestTfCallback(t *testing.T) { // var array []interface{}{1, 2, "4", 1.4} var result string - result = tfCallback("examples/unit-testing/callback-value.sh") + result = tfCallback("callback-value.sh") assert.Equal(t, "4711", result) } - - func captureStdout(f func()) string { old := os.Stdout r, w, _ := os.Pipe()