Skip to content

Commit

Permalink
Add AllTargets() function
Browse files Browse the repository at this point in the history
  • Loading branch information
g41797 committed Oct 29, 2023
1 parent 4364259 commit adf0a20
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ All badly formatted messages should be published to "badmessages-topic"

List of targets for the message producer can get from *syslogsidecar.Targets* function:
```go
// Returns list of "targets" for the message according to facility and severity
// Returns list of non-repeating "targets" for the message according to facility and severity
// of the message and content of syslogconf.json file.
// Usually error returned for the case of absent or wrong syslogconf.json file.
// nil, nil - means no defined targets for the message.
Expand All @@ -334,6 +334,13 @@ for _, topic := range topics {
mpr.produceToTopic(msg, topic)
}
.......................................
```

Additional helper function - *syslogsidecar.AllTargets()*:
```go
// Returns list of all non-repeating "targets" existing in syslogconf.json file
// and error for absent or wrong syslogconf.json file.
func AllTargets() ([]string, error)
```

## Implementations are based on syslogsidecar
Expand Down
36 changes: 35 additions & 1 deletion syslogconf.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"github.com/g41797/sputnik/sidecar"
)

// Returns list of "targets" for the message according to facility and severity
// Returns list of non-repeating "targets" for the message according to facility and severity
// of the message and content of syslogconf.json file.
// Usually error returned for the case of absent or wrong syslogconf.json file.
// nil, nil - means no defined targets for the message.
Expand Down Expand Up @@ -53,9 +53,43 @@ func Targets(msg sputnik.Msg) ([]string, error) {

var targets []string

trgmap := make(map[string]bool)

for _, finder := range tFinders {
target, _ := finder.gettarget(facility, severiry)
if len(target) != 0 {
if _, exists := trgmap[target]; !exists {
trgmap[target] = true
targets = append(targets, target)
}
}
}

return targets, nil
}

// Returns list of all non-repeating "targets" existing in syslogconf.json file
// and error for absent or wrong syslogconf.json file.
func AllTargets() ([]string, error) {

bfonce.Do(buildFinders)

if tfError != nil {
return nil, tfError
}

if len(tFinders) == 0 {
return nil, fmt.Errorf("empty list of finders")
}

var targets []string

trgmap := make(map[string]bool)

for _, finder := range tFinders {
target := finder.target
if _, exists := trgmap[target]; !exists {
trgmap[target] = true
targets = append(targets, target)
}
}
Expand Down

0 comments on commit adf0a20

Please sign in to comment.