Intel2Noise/research/searchlog.go
2022-10-17 19:39:06 +02:00

163 lines
3.6 KiB
Go

package main
import (
"flag"
"fmt"
"io/fs"
"log"
"os"
"runtime"
"strings"
"time"
)
// findPath Find the path in which the log files are stored.
func findPath() (logPath string) {
var err error
var homedir string
var Path string
var Paths []string
var PathsLinux []string
var PathsMac []string
var PathsWindows []string
// Determine the homedir of the user.
homedir, err = os.UserHomeDir()
if err != nil {
log.Fatal(err)
}
// Define Paths to check out
PathsLinux = append(PathsLinux, "/Documents/EVE/logs/")
PathsLinux = append(PathsLinux, "/.local/share/Steam/steamapps/compatdata/8500/pfx/drive_c/users/steamuser/My Documents/EVE/logs/Chatlogs/")
PathsMac = append(PathsMac, "/Documents/EVE/logs/")
PathsMac = append(PathsMac, "/Library/Application Support/EVE Online/p_drive/User/My Documents/EVE/")
PathsWindows = append(PathsWindows, "C:\\Users\\YourUserName\\Documents\\EVE\\logs")
PathsWindows = append(PathsWindows, "MyDocuments -> EVE -> Logs")
switch runtime.GOOS {
case "linux":
Paths = PathsLinux
case "darwin":
Paths = PathsMac
case "windows":
Paths = PathsWindows
}
for _, Path = range Paths {
if _, err := os.Stat(homedir + Path); !os.IsNotExist(err) {
logPath = homedir + Path
}
}
return
}
// OrchestrateIntelMonitoring routine to periodically check the logpath for the intel channels.
func OrchestrateIntelMonitoring(logPath string, intelChannel string) {
// Declare variables
var files []fs.FileInfo
var lastName string
var tempName string
// Enter a for-loop that periodically checks.
for {
files = fetchFileListing(logPath)
tempName = findLatestLog(logPath, intelChannel, files)
if !strings.EqualFold(lastName, tempName) {
lastName = tempName
fmt.Println("Found a new LogFile! Its " + lastName)
}
// Sleep a minute before we check again.
time.Sleep(time.Second * 60)
}
}
// fetchFileListing As it says on the tin.
func fetchFileListing(logPath string) (files []fs.FileInfo) {
// Declare Variables
var err error
var folder *os.File
// Open the folder we need to open.
folder, err = os.Open(logPath)
if err != nil {
log.Fatal(err)
}
// Read the files UwU
files, err = folder.Readdir(-1)
folder.Close()
if err != nil {
log.Fatal(err)
}
// Off you go! \o/
return
}
// findLatestLog Find the last log-file based on the Internet
func findLatestLog(logPath string, intelChannel string, files []fs.FileInfo) (lastName string) {
// Declare Variables
var lastTime time.Time
var tempTime time.Time
for _, file := range files {
file, err := os.Stat(logPath + file.Name())
if err != nil {
log.Fatal(err)
}
if strings.EqualFold(intelChannel, file.Name()[:len(intelChannel)]) {
tempTime = file.ModTime()
if tempTime.After(lastTime) {
lastTime = tempTime
lastName = logPath + file.Name()
}
}
}
// And there you are (or not)
return
}
// main Something Importang. Dunno
func main() {
// Declare variables
var logPath string
var IntelChannelsSplit []string
var IntelChannel string
// Handle Parameters
var intelChannels = flag.String("i", "Etherium Intel,Bean-Intel", "A commaseparated list of intel channels.")
flag.Parse()
// Split the CSV string into different parts
IntelChannelsSplit = strings.Split(*intelChannels, ",")
// Get the path we need to
logPath = findPath()
for _, IntelChannel = range IntelChannelsSplit {
IntelChannel = strings.ReplaceAll(IntelChannel, " ", "_")
go OrchestrateIntelMonitoring(logPath, IntelChannel)
}
for {
time.Sleep(time.Second * 3600)
fmt.Println("Hi. This is your hourly reminder that this program is still runnig and hasn't crashed!")
}
}