Skip to content

Commit

Permalink
support relative callbacks in template
Browse files Browse the repository at this point in the history
  • Loading branch information
sklevenz committed May 25, 2019
1 parent 5a04b39 commit f27b6cc
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 19 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
terraformer
dist
.DS_Store
gen
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion examples/aws/tf.template
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ resource "aws_route53_record" "some-dns-record" {
}

resource "dummy" {
callback = "{{ tfCallback "examples/aws/callback.sh" }}"
callback = "{{ tfCallback "callback.sh" }}"
}
16 changes: 16 additions & 0 deletions runSamples.sh
Original file line number Diff line number Diff line change
@@ -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
34 changes: 20 additions & 14 deletions terraformer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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")
Expand Down
4 changes: 1 addition & 3 deletions terraformer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit f27b6cc

Please sign in to comment.