1
0
Fork 0
mirror of https://github.com/Luzifer/yaml-vault.git synced 2024-12-30 00:31:21 +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
import (
"bytes"
"fmt"
"html/template"
"io/ioutil"
"log"
"os"
@ -172,6 +174,11 @@ func importToVault(client *api.Client) error {
return err
}
keysRaw, err = parseImportFile(keysRaw)
if err != nil {
return err
}
var keys importFile
if err := yaml.Unmarshal(keysRaw, &keys); err != nil {
return err
@ -186,3 +193,26 @@ func importToVault(client *api.Client) error {
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)
}