-
Notifications
You must be signed in to change notification settings - Fork 679
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
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ import ( | |
"log/syslog" | ||
"net" | ||
"os" | ||
"strings" | ||
"text/template" | ||
"time" | ||
|
||
|
@@ -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, "=")) { | ||
return strings.Split(value, "=")[1] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 { | ||
|
@@ -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} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would call the function There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
tmpl, err := template.New("syslog").Funcs(funcMap).Parse(tmplStr) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
There was a problem hiding this comment.
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.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, changed.