package tools

import (
	"bytes"
	"fmt"
	"os/exec"
	"runtime"
)

func GetJournalctlLogsCount(serviceName, grepPattern string, hoursAgo int) ([]byte, error) {

	if runtime.GOOS != "linux" {
		return nil, fmt.Errorf("not a linux")
	}

	cmd := exec.Command("sh", "-c",
		fmt.Sprintf("journalctl -u %s --since '%d hours ago' | grep -c '%s'",
			serviceName, hoursAgo, grepPattern))

	var output bytes.Buffer
	cmd.Stdout = &output

	err := cmd.Run()
	if err != nil {
		return nil, err
	}

	return output.Bytes(), nil
}