chore: Add simple parsers for journald and tail files
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
package parser
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"regexp"
|
||||
)
|
||||
|
||||
func ParseJournald(unit string) (Parser, error) {
|
||||
if unit == "" {
|
||||
return Parser{}, errors.New("unit name cannot be empty")
|
||||
}
|
||||
|
||||
validUnitName := regexp.MustCompile(`^[a-zA-Z0-9:_\.\-@]+$`)
|
||||
if !validUnitName.MatchString(unit) {
|
||||
return Parser{}, fmt.Errorf("invalid systemd unit name: %s", unit)
|
||||
}
|
||||
|
||||
cmd := exec.Command("journalctl", "-u", unit, "-f", "-n", "0", "-o", "short", "--no-pager")
|
||||
|
||||
stdout, err := cmd.StdoutPipe()
|
||||
if err != nil {
|
||||
return Parser{}, fmt.Errorf("stdout pipe error: %w", err)
|
||||
}
|
||||
|
||||
if err := cmd.Start(); err != nil {
|
||||
return Parser{}, fmt.Errorf("failed to start journalctl: %w", err)
|
||||
}
|
||||
|
||||
p := Parser{
|
||||
parser: bufio.NewScanner(stdout),
|
||||
ch: make(chan string, 100),
|
||||
cmd: cmd,
|
||||
file: nil,
|
||||
}
|
||||
|
||||
return p, nil
|
||||
}
|
||||
Reference in New Issue
Block a user