46 lines
874 B
Go
46 lines
874 B
Go
package file
|
|
|
|
import (
|
|
"errors"
|
|
"os"
|
|
|
|
"gitea.d3m0k1d.ru/d3m0k1d/HellreigN/agent/internal/logsource"
|
|
"github.com/hpcloud/tail"
|
|
)
|
|
|
|
var _ logsource.LogSource = new(FileLogSource)
|
|
|
|
type FileLogSource struct {
|
|
*tail.Tail
|
|
}
|
|
|
|
func New(filepath string) (fls *FileLogSource, err error) {
|
|
if _, err := os.Stat(filepath); os.IsNotExist(err) {
|
|
if err := os.WriteFile(filepath, []byte{}, 0600); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
t, err := tail.TailFile(filepath, tail.Config{
|
|
Follow: true,
|
|
Location: &tail.SeekInfo{
|
|
Offset: 100,
|
|
Whence: 2,
|
|
},
|
|
})
|
|
if err != nil {
|
|
return
|
|
}
|
|
return &FileLogSource{t}, nil
|
|
}
|
|
func (f *FileLogSource) ReadLine() (string, error) {
|
|
select {
|
|
case <-f.Dead():
|
|
return "", errors.Join(logsource.ErrDead, f.Err())
|
|
case line := <-f.Lines:
|
|
return line.Text, line.Err
|
|
}
|
|
}
|
|
func (f *FileLogSource) Close() error {
|
|
return f.Stop()
|
|
}
|