Vendor .golangci.yml byte-identical from sneak/prompts at cc440118 and move the Dockerfile lint phase to golangci-lint v2.14.0 by the digest REPO_POLICIES.md names. Fix the code to that config with flags, help text, output files, SQL and the order of steps unchanged; long functions are split into named steps. Judgement call: the extraction transaction is now rolled back on every early return; the old deferred rollback missed most failures and could dereference a nil transaction. Thirteen //nolint directives (gosec, unconvert, mnd, unqueryvet), each with its reason. Model: opus-5-5
74 lines
1.5 KiB
Go
74 lines
1.5 KiB
Go
package bsdaily
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"log/slog"
|
|
"os"
|
|
"path/filepath"
|
|
"sort"
|
|
"time"
|
|
)
|
|
|
|
var errNoSnapshots = errors.New("no daily snapshots found")
|
|
|
|
// FindLatestDailySnapshot returns the directory and date of the newest
|
|
// daily snapshot in SnapshotBase. It fails when there is none or when the
|
|
// newest one has no database file.
|
|
func FindLatestDailySnapshot() (string, time.Time, error) {
|
|
entries, err := os.ReadDir(SnapshotBase)
|
|
if err != nil {
|
|
return "", time.Time{}, fmt.Errorf("reading snapshot directory %s: %w",
|
|
SnapshotBase, err)
|
|
}
|
|
|
|
type snapshot struct {
|
|
name string
|
|
date time.Time
|
|
}
|
|
|
|
var snapshots []snapshot
|
|
|
|
for _, e := range entries {
|
|
if !e.IsDir() {
|
|
continue
|
|
}
|
|
|
|
m := snapshotPattern.FindStringSubmatch(e.Name())
|
|
if m == nil {
|
|
continue
|
|
}
|
|
|
|
d, err := time.Parse("2006-01-02", m[1])
|
|
if err != nil {
|
|
slog.Warn("skipping snapshot with unparseable date",
|
|
"name", e.Name(), "error", err)
|
|
|
|
continue
|
|
}
|
|
|
|
snapshots = append(snapshots, snapshot{name: e.Name(), date: d})
|
|
}
|
|
|
|
if len(snapshots) == 0 {
|
|
return "", time.Time{}, fmt.Errorf("%w in %s", errNoSnapshots, SnapshotBase)
|
|
}
|
|
|
|
sort.Slice(snapshots, func(i, j int) bool {
|
|
return snapshots[i].date.After(snapshots[j].date)
|
|
})
|
|
|
|
latest := snapshots[0]
|
|
dir := filepath.Join(SnapshotBase, latest.name)
|
|
|
|
dbPath := filepath.Join(dir, DBFilename)
|
|
|
|
_, err = os.Stat(dbPath)
|
|
if err != nil {
|
|
return "", time.Time{}, fmt.Errorf("database not found in snapshot %s: %w",
|
|
dir, err)
|
|
}
|
|
|
|
return dir, latest.date, nil
|
|
}
|