1
0
Fork 0
mirror of https://github.com/Luzifer/duplicity-backup.git synced 2024-12-20 10:31:20 +00:00

Allow using of env template function

This commit is contained in:
Knut Ahlers 2016-06-25 14:11:51 +02:00
parent 18d7399ef9
commit ba56add430
Signed by: luzifer
GPG key ID: DC2729FDD34BE99E

View file

@ -1,12 +1,14 @@
package main
import (
"bytes"
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"strconv"
"text/template"
valid "github.com/asaskevich/govalidator"
"gopkg.in/yaml.v2"
@ -126,18 +128,42 @@ func (c *configFile) validate() error {
return nil
}
func getTemplateFuncMap() template.FuncMap {
return 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
},
}
}
func loadConfigFile(in io.Reader) (*configFile, error) {
fileContent, err := ioutil.ReadAll(in)
if err != nil {
return nil, err
}
buf := bytes.NewBuffer([]byte{})
tpl, err := template.New("config file").Funcs(getTemplateFuncMap()).Parse(string(fileContent))
if err != nil {
return nil, err
}
if err := tpl.Execute(buf, nil); err != nil {
return nil, err
}
hostname, _ := os.Hostname()
res := &configFile{
Hostname: hostname,
}
if err := yaml.Unmarshal(fileContent, res); err != nil {
if err := yaml.Unmarshal(buf.Bytes(), res); err != nil {
return nil, err
}