1
0
Fork 0
mirror of https://github.com/Luzifer/yaml-vault.git synced 2024-12-20 20:11:16 +00:00

allow templating in input file

This commit is contained in:
Knut Ahlers 2016-07-11 17:22:05 +02:00
parent 903d68aff2
commit ee0ea1e82e
Signed by: luzifer
GPG key ID: DC2729FDD34BE99E

30
main.go
View file

@ -1,7 +1,9 @@
package main package main
import ( import (
"bytes"
"fmt" "fmt"
"html/template"
"io/ioutil" "io/ioutil"
"log" "log"
"os" "os"
@ -172,6 +174,11 @@ func importToVault(client *api.Client) error {
return err return err
} }
keysRaw, err = parseImportFile(keysRaw)
if err != nil {
return err
}
var keys importFile var keys importFile
if err := yaml.Unmarshal(keysRaw, &keys); err != nil { if err := yaml.Unmarshal(keysRaw, &keys); err != nil {
return err return err
@ -186,3 +193,26 @@ func importToVault(client *api.Client) error {
return nil return nil
} }
func parseImportFile(in []byte) (out []byte, err error) {
funcMap := template.FuncMap{
"env": func(name string, v ...string) string {
defaultValue := ""
if len(v) > 0 {
defaultValue = v[0]
}
if value, ok := os.LookupEnv(name); ok {
return value
}
return defaultValue
},
}
t, err := template.New("input file").Funcs(funcMap).Parse(string(in))
if err != nil {
return nil, err
}
buf := bytes.NewBuffer([]byte{})
return buf.Bytes(), t.Execute(buf, nil)
}