mirror of
https://github.com/adnanh/webhook.git
synced 2025-10-04 13:41:03 +00:00
support for docker secrets in template
This commit is contained in:
parent
f187592147
commit
0b2c4416c2
2 changed files with 24 additions and 2 deletions
|
@ -19,8 +19,10 @@ import (
|
|||
"net"
|
||||
"net/textproto"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"regexp"
|
||||
"runtime"
|
||||
"strconv"
|
||||
"strings"
|
||||
"text/template"
|
||||
|
@ -757,8 +759,10 @@ func (h *Hooks) LoadFromFile(path string, asTemplate bool) error {
|
|||
}
|
||||
|
||||
if asTemplate {
|
||||
funcMap := template.FuncMap{"getenv": getenv}
|
||||
|
||||
funcMap := template.FuncMap{
|
||||
"getenv": getenv,
|
||||
"secret": dockerSecret,
|
||||
}
|
||||
tmpl, err := template.New("hooks").Funcs(funcMap).Parse(string(file))
|
||||
if err != nil {
|
||||
return err
|
||||
|
@ -956,3 +960,19 @@ func compare(a, b string) bool {
|
|||
func getenv(s string) string {
|
||||
return os.Getenv(s)
|
||||
}
|
||||
|
||||
// dockerSecret provides a template function to retrieve Docker secret.
|
||||
func dockerSecret(name string) string {
|
||||
_, file := filepath.Split(name)
|
||||
if runtime.GOOS == "windows" {
|
||||
file = filepath.Join("C:\\ProgramData\\Docker\\secrets", file)
|
||||
} else {
|
||||
file = filepath.Join("/run/secrets", file)
|
||||
}
|
||||
b, err := ioutil.ReadFile(file)
|
||||
if err != nil {
|
||||
log.Printf("error reading docker secret from %s %s", file, err)
|
||||
return ""
|
||||
}
|
||||
return string(b)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue