-
Notifications
You must be signed in to change notification settings - Fork 0
/
filemanager.go
97 lines (84 loc) · 2.19 KB
/
filemanager.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
package loghamster
import "os"
// InputFile is a file reader for files in the filesystem
type InputFile struct {
Name string // A logical name for a file (like authlog)
Path string
Watch bool
file *os.File
}
// OutputFile is a file reader for files in the filesystem
type OutputFile struct {
Name string // A logical name for a file (like authlog)
Path string
Compress bool
file *os.File
}
// NewInputFile will initialize a logfile input
func NewInputFile(path string, name string) *InputFile {
return &InputFile{Name: name, Path: path}
}
// Open will open a logfile
func (f InputFile) Open() error {
fh, err := os.Open(f.Path)
f.file = fh
return err
}
// FileManager holds all configured input and outputs
type FileManager struct {
Inputs []InputFile
Outputs []OutputFile
}
// NewFileManager returns a stream manager to help finding
// inputs/outputs by rules
func NewFileManager() *FileManager {
return &FileManager{}
}
// AddInput adds a new file input to the stream manager
func (mgr *FileManager) AddInput(input InputFile) {
mgr.Inputs = append(mgr.Inputs, input)
}
// AddOutput adds a new file input to the stream manager
func (mgr *FileManager) AddOutput(output OutputFile) {
mgr.Outputs = append(mgr.Outputs, output)
}
// FindInputByName will return an InputFile if found by name
// otherwise nil
func (mgr *FileManager) FindInputByName(name string) *InputFile {
for _, file := range mgr.Inputs {
if file.Name == name {
return &file
}
}
return nil
}
// FindInputByPath will return an InputFile if found by path
// otherwise nil
func (mgr *FileManager) FindInputByPath(path string) *InputFile {
for _, file := range mgr.Inputs {
if file.Path == path {
return &file
}
}
return nil
}
// FindOutputByName will return an InputFile if found by name
// otherwise nil
func (mgr *FileManager) FindOutputByName(name string) *OutputFile {
for _, file := range mgr.Outputs {
if file.Name == name {
return &file
}
}
return nil
}
// FindOutputByPath will return an OutputFile if found by path
// otherwise nil
func (mgr *FileManager) FindOutputByPath(path string) *OutputFile {
for _, file := range mgr.Outputs {
if file.Path == path {
return &file
}
}
return nil
}