/
helpers.go
114 lines (99 loc) · 2.43 KB
/
helpers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package helpers
import (
"fmt"
"path/filepath"
"github.com/ccdcoe/go-peek/pkg/models/events"
"github.com/ccdcoe/go-peek/pkg/utils"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
)
type DirSource struct {
Paths []string
Type events.Atomic
}
type DirSources []DirSource
func (d DirSources) MapFunc() func(string) events.Atomic {
m := make(map[string]events.Atomic)
for _, s := range d {
for _, p := range s.Paths {
m[p] = s.Type
}
}
return func(pth string) events.Atomic {
return m[filepath.Clean(pth)]
}
}
func (d DirSources) Files() []string {
f := make([]string, 0)
for _, file := range d {
f = append(f, file.Paths...)
}
return f
}
func GetUxSockistingFromViper() DirSources {
var pth []string
var err error
paths := make(DirSources, 0)
for _, event := range events.Atomics {
// Early return if event type is not configured
if pth = viper.GetStringSlice(fmt.Sprintf("stream.%s.uxsock", event)); pth == nil || len(pth) == 0 {
log.WithFields(log.Fields{
"type": event.String(),
}).Trace("input not configured")
continue
}
for i, p := range pth {
if p, err = utils.ExpandHome(p); err != nil {
log.WithFields(log.Fields{
"path": pth,
}).Fatal("invalid path")
}
pth[i] = filepath.Clean(p)
}
paths = append(paths, DirSource{
Paths: pth,
Type: event,
})
log.WithFields(log.Fields{
"type": event.String(),
"path": pth,
}).Debug("configured input source")
}
log.Tracef("found %d unix sockets", len(paths))
return paths
}
func GetDirListingFromViper() DirSources {
var pth []string
var err error
paths := make(DirSources, 0)
for _, event := range events.Atomics {
// Early return if event type is not configured
if pth = viper.GetStringSlice(fmt.Sprintf("stream.%s.dir", event)); pth == nil || len(pth) == 0 {
log.WithFields(log.Fields{
"type": event.String(),
}).Trace("input not configured")
continue
}
for i, p := range pth {
if p, err = utils.ExpandHome(p); err != nil || !utils.StringIsValidDir(p) {
log.WithFields(log.Fields{
"path": pth,
}).Fatal("invalid path")
}
pth[i] = filepath.Clean(p)
}
paths = append(paths, DirSource{
Paths: pth,
Type: event,
})
logContext := log.WithFields(log.Fields{
"type": event.String(),
"event": "configured input source",
})
for _, p := range pth {
logContext.Debug(p)
}
}
log.Tracef("found %d logfile directories", len(paths))
return paths
}