Skip to content

Commit

Permalink
Access environment variables through the template engine
Browse files Browse the repository at this point in the history
  • Loading branch information
Anthony Green committed Jan 1, 2024
1 parent 985512a commit a898429
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,12 @@ documentation](https://containrrr.dev/shoutrrr/v0.8/services/overview/).
| Zulip Chat | `zulip://bot-mail:bot-key@zulip-domain/?stream=name-or-id&topic=name` |

URLs are actually [go templates](https://pkg.go.dev/text/template)
that are processed before use. Data that you can provide to the
that are processed before use. Data that you can access from the
template engine includes:
- `.Timestamp` : the RFC3339 formatted timestamp for the matching log entry
- `.PID`: the process ID for the observed process
- `.Logline`: the matching log line
- `.Env`: a map to access environment variables (eg. `{{.Env.USER_PASSWORD}}`)

Similarly, the message sent may also be a template. If no `template`
is specified in the channel definition, then the logline is used as
Expand Down
15 changes: 15 additions & 0 deletions green-orb.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import (
"os"
"os/exec"
"regexp"
"strings"
"sync"
"syscall"
"text/template"
Expand Down Expand Up @@ -126,6 +127,7 @@ type TemplateData struct {
PID int
Logline string
Timestamp string
Env map[string]string
}

func loadYAMLConfig(filename string, config *Config) error {
Expand All @@ -142,6 +144,17 @@ func loadYAMLConfig(filename string, config *Config) error {
return nil
}

func envToMap() (map[string]string, error) {
envMap := make(map[string]string)
var err error

for _, v := range os.Environ() {
split_v := strings.Split(v, "=")
envMap[split_v[0]] = strings.Join(split_v[1:], "=")
}
return envMap, err
}

func startWorkers(notificationQueue <-chan Notification, numWorkers int64, wg *sync.WaitGroup) {
var messageString string

Expand All @@ -150,8 +163,10 @@ func startWorkers(notificationQueue <-chan Notification, numWorkers int64, wg *s
go func(workerID int) {
defer wg.Done()
for notification := range notificationQueue {
env, _ := envToMap()
td := TemplateData{PID: notification.PID,
Logline: notification.Message,
Env: env,
Timestamp: time.Now().Format(time.RFC3339)}
switch notification.Channel.Type {
case "notify":
Expand Down

0 comments on commit a898429

Please sign in to comment.