Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add template function to syslog adapter to traverse .Container.Config.Env #182

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion adapters/syslog/syslog.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"log/syslog"
"net"
"os"
"strings"
"text/template"
"time"

Expand All @@ -29,6 +30,15 @@ func getopt(name, dfault string) string {
return value
}

func tplGetEnvVar(env []string, key string) string {
for _, value := range env {
if strings.HasPrefix(value, fmt.Sprintf("%s%s", key, "=")) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe do the fmt.Sprintf("%s%s", key, "=") once in the beginning of the function? Saves a few allocs.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, changed.

return strings.Split(value, "=")[1]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return value[strings.Index(value, "=")+1:]

}
}
return ""
}

func NewSyslogAdapter(route *router.Route) (router.LogAdapter, error) {
transport, found := router.AdapterTransports.Lookup(route.AdapterTransport("udp"))
if !found {
Expand Down Expand Up @@ -67,7 +77,10 @@ func NewSyslogAdapter(route *router.Route) (router.LogAdapter, error) {
default:
return nil, errors.New("unsupported syslog format: " + format)
}
tmpl, err := template.New("syslog").Parse(tmplStr)

funcMap := template.FuncMap{
"getEnvVar": tplGetEnvVar}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would call the function getenv more in the line of other default template functions and the normal os.Getenv go function.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really understand your comment here. What I'm trying to do is create a new template function so you can get environment variables from the slice .Container.Config.Env , as you can see in the first code bit in my first comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I understand. I just though it would be nice if the naming was more 'go-like'. Doesn't really matter much.
Cheers

tmpl, err := template.New("syslog").Funcs(funcMap).Parse(tmplStr)
if err != nil {
return nil, err
}
Expand Down