feat: load packet logs from directory source

This commit is contained in:
zhaoyilun
2026-07-10 01:09:44 +08:00
parent 8f526e9aa9
commit 87a787ee1b
9 changed files with 361 additions and 25 deletions

View File

@@ -3,7 +3,10 @@ package isphere
import (
"bufio"
"fmt"
"io/fs"
"os"
"path/filepath"
"sort"
"strings"
)
@@ -13,9 +16,59 @@ func LoadEncryptedPacketLogSourceFromFile(path string) (EncryptedPacketLogSource
return EncryptedPacketLogSource{}, fmt.Errorf("packet log file path is empty")
}
file, err := os.Open(trimmedPath)
lines, err := readEncryptedPacketLogLinesFromFile(trimmedPath)
if err != nil {
return EncryptedPacketLogSource{}, fmt.Errorf("open packet log file %q: %w", trimmedPath, err)
return EncryptedPacketLogSource{}, err
}
return EncryptedPacketLogSource{Lines: lines}, nil
}
func LoadEncryptedPacketLogSourceFromDirectory(path string) (EncryptedPacketLogSource, error) {
trimmedPath := strings.TrimSpace(path)
if trimmedPath == "" {
return EncryptedPacketLogSource{}, fmt.Errorf("packet log directory path is empty")
}
info, err := os.Stat(trimmedPath)
if err != nil {
return EncryptedPacketLogSource{}, fmt.Errorf("stat packet log directory %q: %w", trimmedPath, err)
}
if !info.IsDir() {
return EncryptedPacketLogSource{}, fmt.Errorf("packet log directory %q is not a directory", trimmedPath)
}
paths := []string{}
if err := filepath.WalkDir(trimmedPath, func(path string, entry fs.DirEntry, walkErr error) error {
if walkErr != nil {
return walkErr
}
if entry.IsDir() {
return nil
}
ext := strings.ToLower(filepath.Ext(entry.Name()))
if ext == ".log" || ext == ".txt" {
paths = append(paths, path)
}
return nil
}); err != nil {
return EncryptedPacketLogSource{}, fmt.Errorf("walk packet log directory %q: %w", trimmedPath, err)
}
sort.Strings(paths)
lines := []string{}
for _, path := range paths {
fileLines, err := readEncryptedPacketLogLinesFromFile(path)
if err != nil {
return EncryptedPacketLogSource{}, err
}
lines = append(lines, fileLines...)
}
return EncryptedPacketLogSource{Lines: lines}, nil
}
func readEncryptedPacketLogLinesFromFile(path string) ([]string, error) {
file, err := os.Open(path)
if err != nil {
return nil, fmt.Errorf("open packet log file %q: %w", path, err)
}
defer file.Close()
@@ -30,7 +83,7 @@ func LoadEncryptedPacketLogSourceFromFile(path string) (EncryptedPacketLogSource
lines = append(lines, line)
}
if err := scanner.Err(); err != nil {
return EncryptedPacketLogSource{}, fmt.Errorf("read packet log file %q: %w", trimmedPath, err)
return nil, fmt.Errorf("read packet log file %q: %w", path, err)
}
return EncryptedPacketLogSource{Lines: lines}, nil
return lines, nil
}

View File

@@ -2,6 +2,7 @@ package isphere
import (
"os"
"path/filepath"
"reflect"
"testing"
)
@@ -19,6 +20,23 @@ func TestLoadEncryptedPacketLogSourceFromFileReadsNonEmptyLines(t *testing.T) {
}
}
func TestLoadEncryptedPacketLogSourceFromDirectoryReadsSortedLogFiles(t *testing.T) {
root := t.TempDir()
writePacketLogFileAtPathForTest(t, root+"\\b.log", "\n line-b1 \n")
writePacketLogFileAtPathForTest(t, root+"\\a.log", "line-a1\n\nline-a2\n")
writePacketLogFileAtPathForTest(t, root+"\\nested\\c.txt", "line-c1\n")
writePacketLogFileAtPathForTest(t, root+"\\ignored.tmp", "ignored\n")
got, err := LoadEncryptedPacketLogSourceFromDirectory(root)
if err != nil {
t.Fatalf("LoadEncryptedPacketLogSourceFromDirectory returned error: %v", err)
}
want := EncryptedPacketLogSource{Lines: []string{"line-a1", "line-a2", "line-b1", "line-c1"}}
if !reflect.DeepEqual(got, want) {
t.Fatalf("source = %#v, want %#v", got, want)
}
}
func writePacketLogFileForTest(t *testing.T, content string) string {
t.Helper()
path := t.TempDir() + "\\packet.log"
@@ -27,3 +45,13 @@ func writePacketLogFileForTest(t *testing.T, content string) string {
}
return path
}
func writePacketLogFileAtPathForTest(t *testing.T, path string, content string) {
t.Helper()
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
t.Fatalf("create parent for %s: %v", path, err)
}
if err := os.WriteFile(path, []byte(content), 0o600); err != nil {
t.Fatalf("write test packet log %s: %v", path, err)
}
}