Start refactor of code into what I think is a more modular way of working
This commit is contained in:
@@ -50,43 +50,3 @@ func FileSearchUnix() []string {
|
||||
}
|
||||
return filesPotential
|
||||
}
|
||||
|
||||
// returns paths with eve chat log files
|
||||
func PathSearch() []string {
|
||||
var pathsWithLogs []string
|
||||
fmt.Println("PathSearch looking for chat log files..")
|
||||
|
||||
searchPath = append(searchPath, "~/.local/share/Steam/steamapps/compatdata/8500/pfx/drive_c/users/steamuser/My Documents/EVE/logs/Chatlogs")
|
||||
|
||||
userName, _ := user.Current()
|
||||
homeDir := userName.HomeDir
|
||||
|
||||
for _, d := range searchPath {
|
||||
|
||||
// if has trailing slash, remove it
|
||||
d = strings.TrimSuffix(d, "/")
|
||||
|
||||
// do a unix like expansion if the path starts like `~/`
|
||||
if strings.HasPrefix(d, "~/") {
|
||||
d = filepath.Join(homeDir, d[2:])
|
||||
}
|
||||
f, err := os.Open(d)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
files, err := f.Readdir(-1)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
err = f.Close()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
if len(files) > 0 {
|
||||
pathsWithLogs = append(pathsWithLogs, d)
|
||||
}
|
||||
fmt.Println(len(files))
|
||||
|
||||
}
|
||||
return pathsWithLogs
|
||||
}
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
package eve
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"github.com/starkers/safer-eve/pkg/evelogs"
|
||||
)
|
||||
|
||||
type logDirs []*evelogs.LogDir
|
||||
type logfiles []*evelogs.LogFile
|
||||
|
||||
func Init() error {
|
||||
fmt.Println("searching")
|
||||
|
||||
//fmt.Println(a)
|
||||
//fileList := FileSearchUnix()
|
||||
dirlist, err := GetLogDirectories()
|
||||
if err != nil {
|
||||
log.Fatal(fmt.Sprintf("Failed getting log directories %v", err))
|
||||
}
|
||||
logDirs, err := evelogs.DirsToLogDirs(dirlist)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
log.Println("Found logdirs: ")
|
||||
for _, logdir := range logDirs {
|
||||
log.Println(logdir.Path)
|
||||
}
|
||||
|
||||
//List := eve.GetActiveChannels(fileList)
|
||||
var logfiles []*evelogs.LogFile
|
||||
for _, logdir := range logDirs {
|
||||
logfilelist, err := logdir.GetLogFiles()
|
||||
if err != nil {
|
||||
log.Printf("Warning: %s", err)
|
||||
}
|
||||
logfiles = append(logfiles, logfilelist...)
|
||||
|
||||
}
|
||||
|
||||
//for _, d := range logDirs {
|
||||
// fmt.Println(d)
|
||||
// go eve.WatchPath(d)
|
||||
//}
|
||||
|
||||
// for _, f := range fileList {
|
||||
// if eve.IsCurrentLog(f) {
|
||||
// //if strings.Contains(f, "delve.imperium") {
|
||||
// // fmt.Printf("tailing file: %v\n", f)
|
||||
// go eve.Tail(f)
|
||||
// //}
|
||||
// }
|
||||
// //else {
|
||||
// // fmt.Println(f)
|
||||
// //}
|
||||
// }
|
||||
// time.Sleep(9999 * time.Hour)
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package eve
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os/user"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func GetLogDirectories() ([]string, error) {
|
||||
// Given assumptions about log directories in the eve application
|
||||
// installatin layout, simple method to retrieve folders for eve.
|
||||
// We can extend this to branch for or register platform later
|
||||
linuxPath := "~/.local/share/Steam/steamapps/compatdata/8500/pfx/drive_c/users/steamuser/My Documents/EVE/logs/"
|
||||
// linuxPath := "~/.local/share/Steam/steamapps/compatdata/8500/pfx/drive_c/users/steamuser/My Documents/EVE/notreal/"
|
||||
//TODO: if not linux
|
||||
// load ini or settings file of locations?
|
||||
// get result by probing install path for file system a bit.
|
||||
path := linuxPath
|
||||
var paths []string
|
||||
|
||||
if strings.HasPrefix(path, "~/") {
|
||||
userName, _ := user.Current()
|
||||
path = filepath.Join(userName.HomeDir, path[2:])
|
||||
}
|
||||
files, err := ioutil.ReadDir(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, f := range files {
|
||||
|
||||
if f.IsDir() {
|
||||
paths = append(paths, f.Name())
|
||||
}
|
||||
}
|
||||
return paths, nil
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
package evelogs
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
"os/user"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type LogDir struct {
|
||||
Path string
|
||||
}
|
||||
|
||||
func NewLogDir(path string) (*LogDir, error) {
|
||||
// do a unix like expansion if the path starts like `~/`
|
||||
if strings.HasPrefix(path, "~/") {
|
||||
userName, _ := user.Current()
|
||||
path = filepath.Join(userName.HomeDir, path[2:])
|
||||
}
|
||||
ld := &LogDir{Path: path}
|
||||
if !ld.DExists() {
|
||||
return nil, fmt.Errorf("Path %s does not exist or is a regular file", ld.Path)
|
||||
}
|
||||
return ld, nil
|
||||
}
|
||||
|
||||
func (ld *LogDir) DExists() bool {
|
||||
info, err := os.Stat(ld.Path)
|
||||
if os.IsNotExist(err) {
|
||||
return false
|
||||
}
|
||||
return info.IsDir()
|
||||
}
|
||||
|
||||
func (ld *LogDir) GetLogFiles() ([]*LogFile, error) {
|
||||
//TODO: iterate through the directory looking for logs
|
||||
|
||||
files, err := ioutil.ReadDir(ld.Path)
|
||||
var paths []string
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
return nil, err
|
||||
}
|
||||
for _, f := range files {
|
||||
if !f.IsDir() {
|
||||
paths = append(paths, ld.Path+string(os.PathSeparator)+f.Name())
|
||||
}
|
||||
}
|
||||
logfiles, err := PathsToLogFiles(paths)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
return nil, err
|
||||
}
|
||||
return logfiles, nil
|
||||
}
|
||||
|
||||
// Generate logDir instances from log dir paths
|
||||
func DirsToLogDirs(paths []string) ([]*LogDir, error) {
|
||||
var logDirs []*LogDir
|
||||
for _, dirpath := range paths {
|
||||
newLogDir, err := NewLogDir(dirpath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
logDirs = append(logDirs, newLogDir)
|
||||
}
|
||||
return logDirs, nil
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package evelogs
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
)
|
||||
|
||||
type LogFile struct {
|
||||
Path string
|
||||
}
|
||||
|
||||
// create a lofFile instance, if it's a file.
|
||||
func NewLogFile(path string) (*LogFile, error) {
|
||||
lf := &LogFile{Path: path}
|
||||
exists := lf.FileExists()
|
||||
if !exists {
|
||||
return nil, fmt.Errorf("File %s not found or is directory", lf.Path)
|
||||
}
|
||||
return lf, nil
|
||||
}
|
||||
|
||||
func (lf *LogFile) FileExists() bool {
|
||||
info, err := os.Stat(lf.Path)
|
||||
if os.IsNotExist(err) {
|
||||
return false
|
||||
}
|
||||
return !info.IsDir()
|
||||
}
|
||||
|
||||
func (lf *LogFile) TailFile() error {
|
||||
//lets start a goroutine to tail the file here
|
||||
return nil
|
||||
}
|
||||
|
||||
//create logFile instances from a list of paths
|
||||
func PathsToLogFiles(paths []string) ([]*LogFile, error) {
|
||||
var logFiles []*LogFile
|
||||
for _, f := range paths {
|
||||
nLogFile, err := NewLogFile(f)
|
||||
if err != nil {
|
||||
log.Printf("Error with %s: %s", f, err)
|
||||
return nil, err
|
||||
}
|
||||
logFiles = append(logFiles, nLogFile)
|
||||
}
|
||||
return logFiles, nil
|
||||
}
|
||||
Reference in New Issue
Block a user