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:
parent
903d68aff2
commit
ee0ea1e82e
1 changed files with 30 additions and 0 deletions
30
main.go
30
main.go
|
@ -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)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue