feat: load packet logs from configured source
This commit is contained in:
36
internal/isphere/file_source.go
Normal file
36
internal/isphere/file_source.go
Normal file
@@ -0,0 +1,36 @@
|
||||
package isphere
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func LoadEncryptedPacketLogSourceFromFile(path string) (EncryptedPacketLogSource, error) {
|
||||
trimmedPath := strings.TrimSpace(path)
|
||||
if trimmedPath == "" {
|
||||
return EncryptedPacketLogSource{}, fmt.Errorf("packet log file path is empty")
|
||||
}
|
||||
|
||||
file, err := os.Open(trimmedPath)
|
||||
if err != nil {
|
||||
return EncryptedPacketLogSource{}, fmt.Errorf("open packet log file %q: %w", trimmedPath, err)
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
scanner := bufio.NewScanner(file)
|
||||
scanner.Buffer(make([]byte, 64*1024), 16*1024*1024)
|
||||
lines := []string{}
|
||||
for scanner.Scan() {
|
||||
line := strings.TrimSpace(scanner.Text())
|
||||
if line == "" {
|
||||
continue
|
||||
}
|
||||
lines = append(lines, line)
|
||||
}
|
||||
if err := scanner.Err(); err != nil {
|
||||
return EncryptedPacketLogSource{}, fmt.Errorf("read packet log file %q: %w", trimmedPath, err)
|
||||
}
|
||||
return EncryptedPacketLogSource{Lines: lines}, nil
|
||||
}
|
||||
Reference in New Issue
Block a user