diff --git a/.golangci.yml b/.golangci.yml index f784990..d5f4765 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -163,7 +163,7 @@ linters: varnamelen: # 1 is the strictest the linter allows: it only spares a name used on the line after its declaration. max-distance: 1 - ignore-names: [i, j, ok, id, iv] + ignore-names: [i, j, ok, id, iv, db, fd, n] ignore-decls: - t *testing.T - b *testing.B diff --git a/CLAUDE.md b/CLAUDE.md index 0c94cd1..b4f81d4 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -59,7 +59,7 @@ make payload-clean # rm internal/abe/payload/*.bin - **C payload**: all sources in `internal/abe/native/` are first-party (no vendored C). See RFC-016 §9.2 for why Stephen Fewer's reflective loader was rejected. - **Error handling**: `fmt.Errorf("context: %w", err)` for wrapping, never `_ =` to ignore errors - **Logging**: `log.Debugf` for record-level diagnostics, `log.Infof` for user-facing progress/status, `log.Warnf` for unexpected conditions. Extract methods should return errors, not log them. -- **Naming**: follow Go conventions and the settled names in `rfcs/004-naming-decisions.md` — `browser.Browser` not `BrowserConfig`, `Extract` not `BrowsingData`; spell names out (`extractors`, `snapshotDir`, `profile`), leaving only `i`/`j`, `err`, `ctx`, `ok`, `t`/`tb`, `id`, `iv`, and one-letter receivers short (`varnamelen` enforces it) +- **Naming**: follow Go conventions and the settled names in `rfcs/004-naming-decisions.md` — `browser.Browser` not `BrowserConfig`, `Extract` not `BrowsingData`; spell names out (`extractors`, `snapshotDir`, `profile`), leaving only `i`/`j`, `err`, `ctx`, `ok`, `t`/`tb`, `id`, `iv`, `db`, `fd`, `n`, and one-letter receivers short (`varnamelen` enforces it) - **Comments**: none by default; when one is needed, one line of at most 120 characters that says why, never what the code does (up to 3 lines only when the reason cannot fit); a comment on a declaration starts with its name (`// AssertJSONFallback checks …`) - **Tests**: `internal/browser/browsertest` builds real fixture files (SQLite, JSON, LevelDB, plist) into a `t.TempDir()` — never a live browser, never a database mock; goldens live in each package's `testdata/` and `HBD_UPDATE_GOLDEN=1 go test ./...` regenerates them - **Architecture**: see `rfcs/README.md` — design RFCs 000–011, format and technique RFCs 013–021 diff --git a/internal/app/archive.go b/internal/app/archive.go index 9fc6339..d033bf8 100644 --- a/internal/app/archive.go +++ b/internal/app/archive.go @@ -50,7 +50,7 @@ func archiveFiles(plan Plan, dirs []browser.UserDataDir) []snapshot.File { } for _, dir := range dirs { for _, profile := range dir.Profiles { - for _, fileSpec := range chromium.ArchiveFileSpecs(dir) { + for _, fileSpec := range chromium.ArchiveFileSpecs() { if file, ok, err := firstOnDisk(dir, profile, fileSpec); err == nil && ok { add(file) } diff --git a/internal/app/keys.go b/internal/app/keys.go index 51aec26..e89099c 100644 --- a/internal/app/keys.go +++ b/internal/app/keys.go @@ -59,7 +59,7 @@ func retrieveKeys(ctx context.Context, dir browser.UserDataDir, keys masterKeyRe } defer closeSnapshot(snapshotDir) var files []snapshot.File - for _, fileSpec := range chromium.KeyFileSpecs(dir) { + for _, fileSpec := range chromium.KeyFileSpecs() { if file, ok, err := firstOnDisk(dir, browser.Profile{}, fileSpec); err == nil && ok { files = append(files, file) } diff --git a/internal/browser/chromium/chromium.go b/internal/browser/chromium/chromium.go index df9a78e..4cf2d00 100644 --- a/internal/browser/chromium/chromium.go +++ b/internal/browser/chromium/chromium.go @@ -172,12 +172,12 @@ func KeyLocator(dir browser.UserDataDir, dirSnapshot snapshot.Snapshot) (masterk } // KeyFileSpecs are the files KeyLocator reads from a snapshot. -func KeyFileSpecs(_ browser.UserDataDir) []browser.FileSpec { +func KeyFileSpecs() []browser.FileSpec { return []browser.FileSpec{localState} } // ArchiveFileSpecs add each profile's marker, by which restore rediscovers the profiles, and Local State as v1.1.0 did. -func ArchiveFileSpecs(_ browser.UserDataDir) []browser.FileSpec { +func ArchiveFileSpecs() []browser.FileSpec { fileSpecs := []browser.FileSpec{localState} for _, marker := range profileMarkers { fileSpecs = append(fileSpecs, browser.FileSpec{Paths: []string{marker}, Optional: true}) diff --git a/internal/browser/chromium/chromium_test.go b/internal/browser/chromium/chromium_test.go index 09c6cff..0360c2b 100644 --- a/internal/browser/chromium/chromium_test.go +++ b/internal/browser/chromium/chromium_test.go @@ -329,5 +329,5 @@ func TestArchiveFileSpecs(t *testing.T) { localState, {Paths: []string{"Preferences"}, Optional: true}, {Paths: []string{"Preferences_02"}, Optional: true}, - }, ArchiveFileSpecs(browser.UserDataDir{})) + }, ArchiveFileSpecs()) } diff --git a/internal/browser/chromium/extract_bookmark.go b/internal/browser/chromium/extract_bookmark.go index c020723..054a173 100644 --- a/internal/browser/chromium/extract_bookmark.go +++ b/internal/browser/chromium/extract_bookmark.go @@ -22,7 +22,7 @@ func extractBookmarks(path string, out browser.Collector) error { walkBookmarks(value, "", &bookmarks) return true }) - emit(out, bookmarks) + browser.AddAll(out, bookmarks) return nil } diff --git a/internal/browser/chromium/extract_cookie.go b/internal/browser/chromium/extract_cookie.go index d7ebbe9..87aeb14 100644 --- a/internal/browser/chromium/extract_cookie.go +++ b/internal/browser/chromium/extract_cookie.go @@ -67,7 +67,7 @@ func extractCookies(masterKeys masterkey.Set, path string, out browser.Collector if err != nil { return err } - emit(out, cookies) + browser.AddAll(out, cookies) return nil } diff --git a/internal/browser/chromium/extract_creditcard.go b/internal/browser/chromium/extract_creditcard.go index 4ffd423..611bdc5 100644 --- a/internal/browser/chromium/extract_creditcard.go +++ b/internal/browser/chromium/extract_creditcard.go @@ -57,7 +57,7 @@ func extractCreditCards(masterKeys masterkey.Set, path string, out browser.Colle if err != nil { return err } - emit(out, cards) + browser.AddAll(out, cards) return nil } @@ -110,6 +110,6 @@ func extractYandexCreditCards(masterKeys masterkey.Set, path string, out browser if err != nil { return err } - emit(out, cards) + browser.AddAll(out, cards) return nil } diff --git a/internal/browser/chromium/extract_download.go b/internal/browser/chromium/extract_download.go index 4d49d57..4535fba 100644 --- a/internal/browser/chromium/extract_download.go +++ b/internal/browser/chromium/extract_download.go @@ -32,6 +32,6 @@ func extractDownloads(path string, out browser.Collector) error { if err != nil { return err } - emit(out, downloads) + browser.AddAll(out, downloads) return nil } diff --git a/internal/browser/chromium/extract_extension.go b/internal/browser/chromium/extract_extension.go index 184a331..eaa3272 100644 --- a/internal/browser/chromium/extract_extension.go +++ b/internal/browser/chromium/extract_extension.go @@ -63,7 +63,7 @@ func extractExtensionsWithKeys(path string, keys []string, out browser.Collector return true }) - emit(out, extensions) + browser.AddAll(out, extensions) return nil } diff --git a/internal/browser/chromium/extract_history.go b/internal/browser/chromium/extract_history.go index 815c3b6..8a423f6 100644 --- a/internal/browser/chromium/extract_history.go +++ b/internal/browser/chromium/extract_history.go @@ -30,6 +30,6 @@ func extractHistories(path string, out browser.Collector) error { if err != nil { return err } - emit(out, histories) + browser.AddAll(out, histories) return nil } diff --git a/internal/browser/chromium/extract_password.go b/internal/browser/chromium/extract_password.go index ef6698c..bfe0177 100644 --- a/internal/browser/chromium/extract_password.go +++ b/internal/browser/chromium/extract_password.go @@ -47,7 +47,7 @@ func extractPasswordsWithQuery(masterKeys masterkey.Set, path string, store reco if err != nil { return err } - emit(out, logins) + browser.AddAll(out, logins) return nil } @@ -85,6 +85,6 @@ func extractYandexPasswords(masterKeys masterkey.Set, path string, out browser.C if err != nil { return err } - emit(out, logins) + browser.AddAll(out, logins) return nil } diff --git a/internal/browser/chromium/extract_storage.go b/internal/browser/chromium/extract_storage.go index 97bc90d..a7c3baa 100644 --- a/internal/browser/chromium/extract_storage.go +++ b/internal/browser/chromium/extract_storage.go @@ -51,7 +51,7 @@ func extractLocalStorage(path string, out browser.Collector) error { } entries = append(entries, entry) } - emit(out, entries) + browser.AddAll(out, entries) return cursor.Error() } @@ -211,7 +211,7 @@ func extractSessionStorage(path string, out browser.Collector) error { Value: value, }) } - emit(out, entries) + browser.AddAll(out, entries) return mapCursor.Error() } diff --git a/internal/browser/chromium/extractors.go b/internal/browser/chromium/extractors.go index 4a146a4..f3946ad 100644 --- a/internal/browser/chromium/extractors.go +++ b/internal/browser/chromium/extractors.go @@ -109,13 +109,3 @@ func extractPasswordStores(ctx context.Context, profileSnapshot browser.ProfileS } return errors.Join(errs...) } - -// emit hands rows to out by pointer, because out sets each record's Browser and Profile in place. -func emit[T any, P interface { - *T - record.Record -}](out browser.Collector, rows []T) { - for i := range rows { - out.Add(P(&rows[i])) - } -} diff --git a/internal/browser/extractor.go b/internal/browser/extractor.go index 109edf2..255716d 100644 --- a/internal/browser/extractor.go +++ b/internal/browser/extractor.go @@ -47,6 +47,16 @@ type Collector interface { Fail(err error) } +// AddAll hands rows to out by pointer, because out sets each record's Browser and Profile in place. +func AddAll[T any, P interface { + *T + record.Record +}](out Collector, rows []T) { + for i := range rows { + out.Add(P(&rows[i])) + } +} + type ProfileSnapshot struct { Profile Profile snapshot snapshot.Snapshot diff --git a/internal/browser/firefox/extract_bookmark.go b/internal/browser/firefox/extract_bookmark.go index 2c8700c..3262322 100644 --- a/internal/browser/firefox/extract_bookmark.go +++ b/internal/browser/firefox/extract_bookmark.go @@ -36,7 +36,7 @@ func extractBookmarks(path string, out browser.Collector) error { if err != nil { return err } - emit(out, bookmarks) + browser.AddAll(out, bookmarks) return nil } diff --git a/internal/browser/firefox/extract_cookie.go b/internal/browser/firefox/extract_cookie.go index a9697fe..b38e3d8 100644 --- a/internal/browser/firefox/extract_cookie.go +++ b/internal/browser/firefox/extract_cookie.go @@ -54,6 +54,6 @@ func extractCookies(path string, out browser.Collector) error { if err != nil { return err } - emit(out, cookies) + browser.AddAll(out, cookies) return nil } diff --git a/internal/browser/firefox/extract_download.go b/internal/browser/firefox/extract_download.go index 7758d16..23f42c2 100644 --- a/internal/browser/firefox/extract_download.go +++ b/internal/browser/firefox/extract_download.go @@ -46,6 +46,6 @@ func extractDownloads(path string, out browser.Collector) error { if err != nil { return err } - emit(out, downloads) + browser.AddAll(out, downloads) return nil } diff --git a/internal/browser/firefox/extract_extension.go b/internal/browser/firefox/extract_extension.go index 2c6ef9c..16ac727 100644 --- a/internal/browser/firefox/extract_extension.go +++ b/internal/browser/firefox/extract_extension.go @@ -32,6 +32,6 @@ func extractExtensions(path string, out browser.Collector) error { Enabled: value.Get("active").Bool(), }) } - emit(out, extensions) + browser.AddAll(out, extensions) return nil } diff --git a/internal/browser/firefox/extract_history.go b/internal/browser/firefox/extract_history.go index 5c4c65e..6fac569 100644 --- a/internal/browser/firefox/extract_history.go +++ b/internal/browser/firefox/extract_history.go @@ -31,6 +31,6 @@ func extractHistories(path string, out browser.Collector) error { if err != nil { return err } - emit(out, histories) + browser.AddAll(out, histories) return nil } diff --git a/internal/browser/firefox/extract_password.go b/internal/browser/firefox/extract_password.go index d0f6f80..6dbe312 100644 --- a/internal/browser/firefox/extract_password.go +++ b/internal/browser/firefox/extract_password.go @@ -60,6 +60,6 @@ func extractPasswords(masterKey func() []byte, path string, out browser.Collecto Store: record.StoreLocal, }) } - emit(out, logins) + browser.AddAll(out, logins) return nil } diff --git a/internal/browser/firefox/extract_storage.go b/internal/browser/firefox/extract_storage.go index 33ef66b..a99c2d1 100644 --- a/internal/browser/firefox/extract_storage.go +++ b/internal/browser/firefox/extract_storage.go @@ -28,7 +28,7 @@ func extractLocalStorage(path string, out browser.Collector) error { if err != nil { return err } - emit(out, entries) + browser.AddAll(out, entries) return nil } diff --git a/internal/browser/firefox/firefox.go b/internal/browser/firefox/firefox.go index bb6a777..2867113 100644 --- a/internal/browser/firefox/firefox.go +++ b/internal/browser/firefox/firefox.go @@ -11,7 +11,6 @@ import ( "github.com/moond4rk/hackbrowserdata/internal/browser" "github.com/moond4rk/hackbrowserdata/internal/category" - "github.com/moond4rk/hackbrowserdata/internal/record" ) var ( @@ -161,13 +160,3 @@ func profileKey(profileSnapshot browser.ProfileSnapshot, loginsPath string) ([]b } return retrieveMasterKey(key4Path, loginsPath) } - -// emit hands rows to out by pointer, because out sets each record's Browser and Profile in place. -func emit[T any, P interface { - *T - record.Record -}](out browser.Collector, rows []T) { - for i := range rows { - out.Add(P(&rows[i])) - } -} diff --git a/internal/browser/safari/extract_bookmark.go b/internal/browser/safari/extract_bookmark.go index 6615fab..ed38e44 100644 --- a/internal/browser/safari/extract_bookmark.go +++ b/internal/browser/safari/extract_bookmark.go @@ -42,7 +42,7 @@ func extractBookmarks(path string, out browser.Collector) error { var bookmarks []record.Bookmark walkBookmarks(root.Children, "", &bookmarks) - emit(out, bookmarks) + browser.AddAll(out, bookmarks) return nil } diff --git a/internal/browser/safari/extract_cookie.go b/internal/browser/safari/extract_cookie.go index 6e28013..f53ebb0 100644 --- a/internal/browser/safari/extract_cookie.go +++ b/internal/browser/safari/extract_cookie.go @@ -35,7 +35,7 @@ func extractCookies(path string, out browser.Collector) error { }) } } - emit(out, cookies) + browser.AddAll(out, cookies) return nil } diff --git a/internal/browser/safari/extract_download.go b/internal/browser/safari/extract_download.go index 615b12e..8ba6d6a 100644 --- a/internal/browser/safari/extract_download.go +++ b/internal/browser/safari/extract_download.go @@ -47,7 +47,7 @@ func extractDownloads(path, ownerUUID string, out browser.Collector) error { TotalBytes: entry.TotalBytes, }) } - emit(out, downloads) + browser.AddAll(out, downloads) return nil } diff --git a/internal/browser/safari/extract_extension.go b/internal/browser/safari/extract_extension.go index f25992c..bdb1a79 100644 --- a/internal/browser/safari/extract_extension.go +++ b/internal/browser/safari/extract_extension.go @@ -37,7 +37,7 @@ func extractExtensions(paths []string, out browser.Collector) error { Enabled: entry.enabled, }) } - emit(out, extensions) + browser.AddAll(out, extensions) return nil } diff --git a/internal/browser/safari/extract_history.go b/internal/browser/safari/extract_history.go index f1bf87c..921cb1e 100644 --- a/internal/browser/safari/extract_history.go +++ b/internal/browser/safari/extract_history.go @@ -42,6 +42,6 @@ func extractHistories(path string, out browser.Collector) error { if err != nil { return err } - emit(out, histories) + browser.AddAll(out, histories) return nil } diff --git a/internal/browser/safari/extract_password.go b/internal/browser/safari/extract_password.go index 13d4a33..91dd47c 100644 --- a/internal/browser/safari/extract_password.go +++ b/internal/browser/safari/extract_password.go @@ -29,7 +29,7 @@ func extractPasswords(ctx context.Context, loginKeychain LoginKeychain, out brow Store: record.StoreLocal, }) } - emit(out, logins) + browser.AddAll(out, logins) return nil } diff --git a/internal/browser/safari/extract_storage.go b/internal/browser/safari/extract_storage.go index 3fd61d6..39761fa 100644 --- a/internal/browser/safari/extract_storage.go +++ b/internal/browser/safari/extract_storage.go @@ -67,7 +67,7 @@ func extractLocalStorage(root string, out browser.Collector) error { }) } } - emit(out, entries) + browser.AddAll(out, entries) return nil } diff --git a/internal/browser/safari/safari.go b/internal/browser/safari/safari.go index a5abdda..5e6d6b9 100644 --- a/internal/browser/safari/safari.go +++ b/internal/browser/safari/safari.go @@ -15,7 +15,6 @@ import ( "github.com/moond4rk/hackbrowserdata/internal/browser" "github.com/moond4rk/hackbrowserdata/internal/category" - "github.com/moond4rk/hackbrowserdata/internal/record" ) // Paths below are relative to the user data dir, ~/Library. @@ -172,13 +171,3 @@ func extensionsFrom(library func(string) string) browser.Extractor { }, } } - -// emit hands rows to out by pointer, because out sets each record's Browser and Profile in place. -func emit[T any, P interface { - *T - record.Record -}](out browser.Collector, rows []T) { - for i := range rows { - out.Add(P(&rows[i])) - } -} diff --git a/internal/log/log.go b/internal/log/log.go index 110362b..54a145c 100644 --- a/internal/log/log.go +++ b/internal/log/log.go @@ -1,8 +1,11 @@ package log -import "fmt" +import ( + "fmt" + "os" +) -var defaultLogger = NewLogger(nil) +var defaultLogger = newLogger(os.Stderr) func SetVerbose() { defaultLogger.SetLevel(DebugLevel) diff --git a/internal/log/log_test.go b/internal/log/log_test.go index 9795129..f34dd20 100644 --- a/internal/log/log_test.go +++ b/internal/log/log_test.go @@ -16,7 +16,7 @@ func TestSetVerbose(t *testing.T) { } func TestSetLevelInvalidPanics(t *testing.T) { - logger := NewLogger(newBase(io.Discard)) - assert.Panics(t, func() { logger.SetLevel(DebugLevel - 1) }) - assert.Panics(t, func() { logger.SetLevel(WarnLevel + 1) }) + testLogger := newLogger(io.Discard) + assert.Panics(t, func() { testLogger.SetLevel(DebugLevel - 1) }) + assert.Panics(t, func() { testLogger.SetLevel(WarnLevel + 1) }) } diff --git a/internal/log/logger.go b/internal/log/logger.go index 4b47d53..aa5e4b1 100644 --- a/internal/log/logger.go +++ b/internal/log/logger.go @@ -3,7 +3,6 @@ package log import ( "fmt" "io" - "os" "path/filepath" "runtime" "strings" @@ -11,75 +10,43 @@ import ( "sync/atomic" ) -func NewLogger(base Base) *Logger { - if base == nil { - base = newBase(os.Stderr) - } - return &Logger{base: base, minLevel: InfoLevel} -} - -type Logger struct { - base Base - +type logger struct { + out io.Writer + mutex sync.Mutex minLevel Level } -func (l *Logger) canLogAt(level Level) bool { +func newLogger(out io.Writer) *logger { + return &logger{out: out, minLevel: InfoLevel} +} + +func (l *logger) canLogAt(level Level) bool { return level >= Level(atomic.LoadInt32((*int32)(&l.minLevel))) } -func (l *Logger) SetLevel(level Level) { +func (l *logger) SetLevel(level Level) { if level < DebugLevel || level > WarnLevel { panic("log: invalid log level") } atomic.StoreInt32((*int32)(&l.minLevel), int32(level)) } -// baseCallerSkip skips the frames between runtime.Caller and the caller: base.Log, logMsg, and Debugf/Infof/Warnf. -const baseCallerSkip = 3 - -func (l *Logger) logMsg(level Level, message string) { - if !l.canLogAt(level) { - return - } - l.base.Log(baseCallerSkip, level, message) -} - -func (l *Logger) Debugf(format string, args ...any) { - l.logMsg(DebugLevel, fmt.Sprintf(format, args...)) -} - -func (l *Logger) Infof(format string, args ...any) { - l.logMsg(InfoLevel, fmt.Sprintf(format, args...)) -} - -func (l *Logger) Warnf(format string, args ...any) { - l.logMsg(WarnLevel, fmt.Sprintf(format, args...)) -} - -type Base interface { - Log(callerSkip int, lvl Level, msg string) -} - -type baseLogger struct { - out io.Writer - mutex sync.Mutex -} - -func newBase(out io.Writer) *baseLogger { - return &baseLogger{out: out} -} +// callerSkip skips the frames between runtime.Caller and the caller: logMsg, and Debugf/Infof/Warnf. +const callerSkip = 2 // continuation lines a message's later lines up under its first, past the "[DBG] " label. const continuation = " " -func (l *baseLogger) Log(callerSkip int, level Level, message string) { +func (l *logger) logMsg(level Level, message string) { + if !l.canLogAt(level) { + return + } message = strings.TrimRight(message, "\n") if strings.Contains(message, "\n") { message = strings.ReplaceAll(message, "\n", "\n"+continuation) } - label := l.formatLabel(level) + label := "[" + level.String() + "]" var line string if level == DebugLevel { _, file, lineNumber, ok := runtime.Caller(callerSkip) @@ -99,6 +66,14 @@ func (l *baseLogger) Log(callerSkip int, level Level, message string) { l.mutex.Unlock() } -func (l *baseLogger) formatLabel(level Level) string { - return "[" + level.String() + "]" +func (l *logger) Debugf(format string, args ...any) { + l.logMsg(DebugLevel, fmt.Sprintf(format, args...)) +} + +func (l *logger) Infof(format string, args ...any) { + l.logMsg(InfoLevel, fmt.Sprintf(format, args...)) +} + +func (l *logger) Warnf(format string, args ...any) { + l.logMsg(WarnLevel, fmt.Sprintf(format, args...)) } diff --git a/internal/log/logger_test.go b/internal/log/logger_test.go index 2f6a525..b51e9d4 100644 --- a/internal/log/logger_test.go +++ b/internal/log/logger_test.go @@ -37,9 +37,9 @@ func TestLoggerDebug(t *testing.T) { testCase.wantedPattern = fmt.Sprintf(`^\[DBG\] \w+\.go:\d+: %s\n$`, testCase.message) t.Run(testCase.description, func(t *testing.T) { var buffer bytes.Buffer - logger := NewLogger(newBase(&buffer)) - logger.SetLevel(DebugLevel) - logger.Debugf("%s", message) + testLogger := newLogger(&buffer) + testLogger.SetLevel(DebugLevel) + testLogger.Debugf("%s", message) got := buffer.String() assert.Regexp(t, testCase.wantedPattern, got) }) @@ -54,8 +54,8 @@ func TestLoggerInfo(t *testing.T) { testCase.wantedPattern = fmt.Sprintf(`^\[INF\] %s\n$`, testCase.message) t.Run(testCase.description, func(t *testing.T) { var buffer bytes.Buffer - logger := NewLogger(newBase(&buffer)) - logger.Infof("%s", message) + testLogger := newLogger(&buffer) + testLogger.Infof("%s", message) got := buffer.String() assert.Regexp(t, testCase.wantedPattern, got) }) @@ -70,8 +70,8 @@ func TestLoggerWarn(t *testing.T) { testCase.wantedPattern = fmt.Sprintf(`^\[WRN\] %s\n$`, testCase.message) t.Run(testCase.description, func(t *testing.T) { var buffer bytes.Buffer - logger := NewLogger(newBase(&buffer)) - logger.Warnf("%s", message) + testLogger := newLogger(&buffer) + testLogger.Warnf("%s", message) got := buffer.String() assert.Regexp(t, testCase.wantedPattern, got) }) @@ -112,9 +112,9 @@ func TestLoggerDebugf(t *testing.T) { testCase.wantedPattern = fmt.Sprintf(`^\[DBG\] \w+\.go:\d+: %s\n$`, message) t.Run(testCase.description, func(t *testing.T) { var buffer bytes.Buffer - logger := NewLogger(newBase(&buffer)) - logger.SetLevel(DebugLevel) - logger.Debugf(testCase.format, testCase.args...) + testLogger := newLogger(&buffer) + testLogger.SetLevel(DebugLevel) + testLogger.Debugf(testCase.format, testCase.args...) got := buffer.String() assert.Regexp(t, testCase.wantedPattern, got) }) @@ -129,8 +129,8 @@ func TestLoggerInfof(t *testing.T) { testCase.wantedPattern = fmt.Sprintf(`^\[INF\] %s\n$`, message) t.Run(testCase.description, func(t *testing.T) { var buffer bytes.Buffer - logger := NewLogger(newBase(&buffer)) - logger.Infof(testCase.format, testCase.args...) + testLogger := newLogger(&buffer) + testLogger.Infof(testCase.format, testCase.args...) got := buffer.String() assert.Regexp(t, testCase.wantedPattern, got) }) @@ -145,8 +145,8 @@ func TestLoggerWarnf(t *testing.T) { testCase.wantedPattern = fmt.Sprintf(`^\[WRN\] %s\n$`, message) t.Run(testCase.description, func(t *testing.T) { var buffer bytes.Buffer - logger := NewLogger(newBase(&buffer)) - logger.Warnf(testCase.format, testCase.args...) + testLogger := newLogger(&buffer) + testLogger.Warnf(testCase.format, testCase.args...) got := buffer.String() assert.Regexp(t, testCase.wantedPattern, got) }) @@ -158,21 +158,21 @@ func TestLoggerWithLowerLevels(t *testing.T) { ops := []struct { name string level Level - logFunc func(*Logger) + logFunc func(*logger) }{ - {"Debugf", DebugLevel, func(logger *Logger) { logger.Debugf("hello") }}, - {"Infof", InfoLevel, func(logger *Logger) { logger.Infof("hello") }}, - {"Warnf", WarnLevel, func(logger *Logger) { logger.Warnf("hello") }}, + {"Debugf", DebugLevel, func(testLogger *logger) { testLogger.Debugf("hello") }}, + {"Infof", InfoLevel, func(testLogger *logger) { testLogger.Infof("hello") }}, + {"Warnf", WarnLevel, func(testLogger *logger) { testLogger.Warnf("hello") }}, } for _, setLevel := range levels { for _, operation := range ops { var buffer bytes.Buffer - logger := NewLogger(newBase(&buffer)) - logger.SetLevel(setLevel) + testLogger := newLogger(&buffer) + testLogger.SetLevel(setLevel) expectedOutput := operation.level >= setLevel - operation.logFunc(logger) + operation.logFunc(testLogger) output := buffer.String() if expectedOutput { @@ -186,35 +186,35 @@ func TestLoggerWithLowerLevels(t *testing.T) { func TestDefaultLevelIsInfo(t *testing.T) { var buffer bytes.Buffer - logger := NewLogger(newBase(&buffer)) + testLogger := newLogger(&buffer) - logger.Debugf("debug msg") + testLogger.Debugf("debug msg") assert.Empty(t, buffer.String(), "Debug should be suppressed at default InfoLevel") - logger.Infof("info msg") + testLogger.Infof("info msg") assert.Contains(t, buffer.String(), "info msg") } func TestDebugIncludesFileLine(t *testing.T) { var buffer bytes.Buffer - logger := NewLogger(newBase(&buffer)) - logger.SetLevel(DebugLevel) - logger.Debugf("test location") + testLogger := newLogger(&buffer) + testLogger.SetLevel(DebugLevel) + testLogger.Debugf("test location") got := buffer.String() assert.Regexp(t, `^\[DBG\] logger_test\.go:\d+: test location\n$`, got) } func TestInfoHasLabel(t *testing.T) { var buffer bytes.Buffer - logger := NewLogger(newBase(&buffer)) - logger.Infof("clean message") + testLogger := newLogger(&buffer) + testLogger.Infof("clean message") assert.Equal(t, "[INF] clean message\n", buffer.String()) } func TestMultilineMessageIndented(t *testing.T) { var buffer bytes.Buffer - logger := NewLogger(newBase(&buffer)) - logger.Warnf("line1\nline2\nline3") + testLogger := newLogger(&buffer) + testLogger.Warnf("line1\nline2\nline3") got := buffer.String() assert.Equal(t, "[WRN] line1\n line2\n line3\n", got) } diff --git a/rfcs/004-naming-decisions.md b/rfcs/004-naming-decisions.md index 98937c1..a239bfc 100644 --- a/rfcs/004-naming-decisions.md +++ b/rfcs/004-naming-decisions.md @@ -251,7 +251,7 @@ A category says what kind of data is meant; a record is one row of that data. Th **Decision.** A variable, parameter, or field is named for what it holds: `for dataCategory, extractor := range defaultExtractors`, `snapshotDir, err := snapshot.Temp()`. Go's own guidance prefers short names in short scopes, but a reader of this code, human or agent, should not have to decode `c`, `e`, `ex`, or `d`, whose type is often not on the same line. -- **What stays short.** The idioms every Go reader knows: `i` and `j` as indexes, `err`, `ctx`, `ok`, `t` and `tb` in tests, the terms `id` and `iv`, and one-letter method receivers, whose type is on the same line. +- **What stays short.** The idioms every Go reader knows: `i` and `j` as indexes, `err`, `ctx`, `ok`, `t` and `tb` in tests, the terms `id` and `iv`, `db` for a database handle, `fd` for a file descriptor, `n` for a count, and one-letter method receivers, whose type is on the same line. - **Package names.** A variable cannot take the name of its package — `browser`, `category`, `record`, `keychain` — without shadowing it, so it takes its role: `supportedBrowser`, `dataCategory`, `row`, `loginKeychain`. - **Enforcement.** `varnamelen` rejects a one- or two-letter name used beyond the next line; `max-distance: 1` is the strictest it allows. The Windows API and ABE packages are excluded until they get their own pass. diff --git a/rfcs/005-browser-definitions.md b/rfcs/005-browser-definitions.md index bef94ec..5dcc0dd 100644 --- a/rfcs/005-browser-definitions.md +++ b/rfcs/005-browser-definitions.md @@ -80,7 +80,7 @@ The Chromium base supplies two more, for the files no single category owns (belo ```go // internal/browser/chromium only func KeyLocator(dir browser.UserDataDir, dirSnapshot snapshot.Snapshot) (masterkey.Locator, error) -func ArchiveFileSpecs(dir browser.UserDataDir) []browser.FileSpec +func ArchiveFileSpecs() []browser.FileSpec ``` Safari's `Extractors` takes one more argument, the run's login keychain, which its password extractor captures. Only the default profile's map gets a password extractor, because the keychain belongs to the user, not to one profile ([RFC-006](006-key-retrieval-and-crypto.md)). `LoginKeychain` is the one-method interface Safari declares; `*keychain.Keychain` satisfies it.