mirror of
https://github.com/Luzifer/duplicity-backup.git
synced 2024-11-08 15:10:06 +00:00
Allow using of env template function
This commit is contained in:
parent
18d7399ef9
commit
ba56add430
1 changed files with 27 additions and 1 deletions
|
@ -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
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue