diff options
| author | Paul Buetow <paul@buetow.org> | 2026-04-14 10:39:16 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2026-04-14 10:39:16 +0300 |
| commit | ff749457e392288e29dbf553bf8e0e64cc8b6401 (patch) | |
| tree | 4d43b1de40007ebcc880bd596114e097a30cc590 /internal/storage | |
| parent | b0ac28f90ddf85e85b0308229569292fd6f7f9a2 (diff) | |
refactor: extract record line parser to internal/recordline (m3)
Deduplicate parseRecordLine from goprecords and storage; shared
recordline.Parse with Fields type. Tests live in recordline package;
DB import path still covered by goprecords TestImportFromDir.
Made-with: Cursor
Diffstat (limited to 'internal/storage')
| -rw-r--r-- | internal/storage/db.go | 47 |
1 files changed, 2 insertions, 45 deletions
diff --git a/internal/storage/db.go b/internal/storage/db.go index ea9d764..14d7050 100644 --- a/internal/storage/db.go +++ b/internal/storage/db.go @@ -7,9 +7,9 @@ import ( "fmt" "os" "path/filepath" - "strconv" "strings" + "codeberg.org/snonux/goprecords/internal/recordline" _ "modernc.org/sqlite" ) @@ -128,14 +128,6 @@ func LoadRecords(ctx context.Context, db *sql.DB) ([]Record, error) { return out, nil } -type recordLine struct { - Uptime uint64 - BootTime uint64 - OS string - KernelName string - KernelMajor string -} - func importFile(ctx context.Context, insert *sql.Stmt, path, host string) error { f, err := os.Open(path) if err != nil { @@ -149,7 +141,7 @@ func importFile(ctx context.Context, insert *sql.Stmt, path, host string) error return ctx.Err() default: } - rec, ok := parseRecordLine(sc.Text()) + rec, ok := recordline.Parse(sc.Text()) if !ok { continue } @@ -162,38 +154,3 @@ func importFile(ctx context.Context, insert *sql.Stmt, path, host string) error } return nil } - -func parseRecordLine(line string) (recordLine, bool) { - line = strings.TrimSpace(line) - if line == "" { - return recordLine{}, false - } - parts := strings.SplitN(line, ":", 3) - if len(parts) != 3 { - return recordLine{}, false - } - uptime, _ := strconv.ParseUint(parts[0], 10, 64) - bootTime, _ := strconv.ParseUint(parts[1], 10, 64) - osStr := parts[2] - kernelName := osStr - if i := strings.Index(osStr, " "); i > 0 { - kernelName = osStr[:i] - } - kernelMajor := kernelName + " " - rest := osStr - if i := strings.Index(osStr, " "); i >= 0 { - rest = osStr[i+1:] - } - if j := strings.Index(rest, "."); j >= 0 { - kernelMajor += rest[:j] + "..." - } else { - kernelMajor += rest + "..." - } - return recordLine{ - Uptime: uptime, - BootTime: bootTime, - OS: osStr, - KernelName: kernelName, - KernelMajor: kernelMajor, - }, true -} |
