Start refactor of code into what I think is a more modular way of working
This commit is contained in:
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user