Skip to content

Commit 6f1f64a

Browse files
committed
sfcache and code modernization
1 parent 9b0ccec commit 6f1f64a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+490
-842
lines changed

.golangci.yml

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,8 @@ linters:
186186
arguments: [10]
187187
- name: flag-parameter # fixes are difficult
188188
disabled: true
189+
- name: bare-return
190+
disabled: true
189191

190192
rowserrcheck:
191193
# database/sql is always checked.
@@ -231,14 +233,9 @@ linters:
231233
- path: _test\.go
232234
linters:
233235
- dupl
234-
- errcheck
235-
- gocognit
236236
- gosec
237237
- godot
238-
- gosmopolitan
239238
- govet # alignment
240-
- maintidx
241-
- nestif
242239
- noctx
243240
- perfsprint
244241
- revive

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ endif
2424
LINTERS :=
2525
FIXERS :=
2626

27-
SHELLCHECK_VERSION ?= v0.10.0
27+
SHELLCHECK_VERSION ?= v0.11.0
2828
SHELLCHECK_BIN := $(LINT_ROOT)/out/linters/shellcheck-$(SHELLCHECK_VERSION)-$(LINT_ARCH)
2929
$(SHELLCHECK_BIN):
3030
mkdir -p $(LINT_ROOT)/out/linters
@@ -45,7 +45,7 @@ shellcheck-fix: $(SHELLCHECK_BIN)
4545
$(SHELLCHECK_BIN) $(shell find . -name "*.sh") -f diff | { read -t 1 line || exit 0; { echo "$$line" && cat; } | git apply -p2; }
4646

4747
GOLANGCI_LINT_CONFIG := $(LINT_ROOT)/.golangci.yml
48-
GOLANGCI_LINT_VERSION ?= v2.5.0
48+
GOLANGCI_LINT_VERSION ?= v2.7.2
4949
GOLANGCI_LINT_BIN := $(LINT_ROOT)/out/linters/golangci-lint-$(GOLANGCI_LINT_VERSION)-$(LINT_ARCH)
5050
$(GOLANGCI_LINT_BIN):
5151
mkdir -p $(LINT_ROOT)/out/linters

cmd/sociopath/main.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717
"strings"
1818
"time"
1919

20-
"github.com/codeGROOVE-dev/sociopath/pkg/cache"
20+
"github.com/codeGROOVE-dev/sociopath/pkg/httpcache"
2121
"github.com/codeGROOVE-dev/sociopath/pkg/sociopath"
2222
)
2323

@@ -62,16 +62,14 @@ func main() {
6262
logger := slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: logLevel}))
6363

6464
// Setup cache
65-
var httpCache *cache.BDCache
65+
var httpCache *httpcache.Cache
6666
if !*noCache {
6767
var err error
68-
httpCache, err = cache.New(*cacheTTL)
68+
httpCache, err = httpcache.New(*cacheTTL)
6969
if err != nil {
7070
logger.Warn("failed to initialize cache, continuing without cache", "error", err)
7171
} else {
7272
defer func() {
73-
stats := httpCache.Stats()
74-
logger.Info("cache stats", "hits", stats.Hits, "misses", stats.Misses, "hit_rate", fmt.Sprintf("%.1f%%", stats.HitRate()))
7573
if err := httpCache.Close(); err != nil {
7674
logger.Warn("failed to close cache", "error", err)
7775
}

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ go 1.25.4
44

55
require (
66
github.com/browserutils/kooky v0.2.4
7-
github.com/codeGROOVE-dev/bdcache v0.6.1
8-
github.com/codeGROOVE-dev/bdcache/persist/localfs v0.0.0-20251121210535-3893c2b92813
7+
github.com/codeGROOVE-dev/sfcache v1.3.0
8+
github.com/codeGROOVE-dev/sfcache/pkg/persist/localfs v1.3.0
99
github.com/google/go-cmp v0.7.0
1010
)
1111

go.sum

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuy
1414
github.com/alecthomas/units v0.0.0-20211218093645-b94a6e3cc137/go.mod h1:OMCwj8VM1Kc9e19TLln2VL61YJF0x1XFtfdL4JdbSyE=
1515
github.com/browserutils/kooky v0.2.4 h1:szrKufBIaZRc6AXs8MF7+4rgcoSZNckQE2q0sJw49kw=
1616
github.com/browserutils/kooky v0.2.4/go.mod h1:Ez5Gw643UabvRkvEnWIgb8Q6qPzxanMuHCTTqlwBHuw=
17-
github.com/codeGROOVE-dev/bdcache v0.6.1 h1:iFvZTXG+tYcfGHD2Jt4Ud/x7/34O/6kTYP9L6XFQ4QQ=
18-
github.com/codeGROOVE-dev/bdcache v0.6.1/go.mod h1:wJEFNbumCspw3PoiW17afq7QaMSeWXtMDO8WOH3GY74=
19-
github.com/codeGROOVE-dev/bdcache/persist/localfs v0.0.0-20251121210535-3893c2b92813 h1:sjXDZCJllfGRAE3yGhI1Ns8/w1S72/RCqoJ0K/qeFHU=
20-
github.com/codeGROOVE-dev/bdcache/persist/localfs v0.0.0-20251121210535-3893c2b92813/go.mod h1:I6U3M/72lAouQCf9EoWPJPDop8gPbDlMl9CgWKjxREg=
17+
github.com/codeGROOVE-dev/sfcache v1.3.0 h1:Ew900GWXkZhMEU560kz0Nk1HvhGshiqaIASbTH+ihHg=
18+
github.com/codeGROOVE-dev/sfcache v1.3.0/go.mod h1:ksV5Y1RwKmOPZZiV0zXpsBOENGUCgO0fVgr/P8f/DJM=
19+
github.com/codeGROOVE-dev/sfcache/pkg/persist/localfs v1.3.0 h1:7zKbd7aHVzmbK2eEbdsf6Dknrte/o8+7I6GkNob8bGA=
20+
github.com/codeGROOVE-dev/sfcache/pkg/persist/localfs v1.3.0/go.mod h1:vHDjjehmi+yjXD+DXVG0rnc4iOBsiIknLThqpkYwA/c=
2121
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
2222
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
2323
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=

pkg/auth/static.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package auth
22

3-
import "context"
3+
import (
4+
"context"
5+
"maps"
6+
)
47

58
// StaticSource provides cookies from a static map.
69
// This is useful for testing or when cookies are provided via options.
@@ -20,8 +23,6 @@ func (s *StaticSource) Cookies(_ context.Context, _ string) (map[string]string,
2023
}
2124
// Return a copy to prevent mutation
2225
result := make(map[string]string, len(s.cookies))
23-
for k, v := range s.cookies {
24-
result[k] = v
25-
}
26+
maps.Copy(result, s.cookies)
2627
return result, nil
2728
}

pkg/bilibili/bilibili.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import (
1010
"strings"
1111
"time"
1212

13-
"github.com/codeGROOVE-dev/sociopath/pkg/cache"
1413
"github.com/codeGROOVE-dev/sociopath/pkg/htmlutil"
14+
"github.com/codeGROOVE-dev/sociopath/pkg/httpcache"
1515
"github.com/codeGROOVE-dev/sociopath/pkg/profile"
1616
)
1717

@@ -30,20 +30,20 @@ func AuthRequired() bool { return false }
3030
// Client handles Bilibili requests.
3131
type Client struct {
3232
httpClient *http.Client
33-
cache cache.HTTPCache
33+
cache *httpcache.Cache
3434
logger *slog.Logger
3535
}
3636

3737
// Option configures a Client.
3838
type Option func(*config)
3939

4040
type config struct {
41-
cache cache.HTTPCache
41+
cache *httpcache.Cache
4242
logger *slog.Logger
4343
}
4444

4545
// WithHTTPCache sets the HTTP cache.
46-
func WithHTTPCache(httpCache cache.HTTPCache) Option {
46+
func WithHTTPCache(httpCache *httpcache.Cache) Option {
4747
return func(c *config) { c.cache = httpCache }
4848
}
4949

@@ -83,7 +83,7 @@ func (c *Client) Fetch(ctx context.Context, urlStr string) (*profile.Profile, er
8383
req.Header.Set("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:146.0) Gecko/20100101 Firefox/146.0")
8484
req.Header.Set("Accept-Language", "zh-CN,zh;q=0.9,en;q=0.8")
8585

86-
body, err := cache.FetchURL(ctx, c.cache, c.httpClient, req, c.logger)
86+
body, err := httpcache.FetchURL(ctx, c.cache, c.httpClient, req, c.logger)
8787
if err != nil {
8888
return nil, err
8989
}

pkg/bilibili/bilibili_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//nolint:gosmopolitan // Chinese platform requires Chinese characters in tests
12
package bilibili
23

34
import (
@@ -86,7 +87,7 @@ func TestFetch(t *testing.T) {
8687
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
8788
w.Header().Set("Content-Type", "text/html; charset=utf-8")
8889
w.WriteHeader(http.StatusOK)
89-
_, _ = w.Write([]byte(mockHTML))
90+
_, _ = w.Write([]byte(mockHTML)) //nolint:errcheck // test helper
9091
}))
9192
defer server.Close()
9293

pkg/bluesky/bluesky.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
"strings"
1313
"time"
1414

15-
"github.com/codeGROOVE-dev/sociopath/pkg/cache"
15+
"github.com/codeGROOVE-dev/sociopath/pkg/httpcache"
1616
"github.com/codeGROOVE-dev/sociopath/pkg/profile"
1717
)
1818

@@ -29,20 +29,20 @@ func AuthRequired() bool { return false }
2929
// Client handles BlueSky requests.
3030
type Client struct {
3131
httpClient *http.Client
32-
cache cache.HTTPCache
32+
cache *httpcache.Cache
3333
logger *slog.Logger
3434
}
3535

3636
// Option configures a Client.
3737
type Option func(*config)
3838

3939
type config struct {
40-
cache cache.HTTPCache
40+
cache *httpcache.Cache
4141
logger *slog.Logger
4242
}
4343

4444
// WithHTTPCache sets the HTTP cache.
45-
func WithHTTPCache(httpCache cache.HTTPCache) Option {
45+
func WithHTTPCache(httpCache *httpcache.Cache) Option {
4646
return func(c *config) { c.cache = httpCache }
4747
}
4848

@@ -88,7 +88,7 @@ func (c *Client) Fetch(ctx context.Context, urlStr string) (*profile.Profile, er
8888
req.Header.Set("Accept", "application/json")
8989
req.Header.Set("User-Agent", "sociopath/1.0")
9090

91-
body, err := cache.FetchURL(ctx, c.cache, c.httpClient, req, c.logger)
91+
body, err := httpcache.FetchURL(ctx, c.cache, c.httpClient, req, c.logger)
9292
if err != nil {
9393
return nil, err
9494
}
@@ -153,7 +153,7 @@ func (c *Client) fetchPosts(ctx context.Context, handle string, limit int) (post
153153
req.Header.Set("Accept", "application/json")
154154
req.Header.Set("User-Agent", "sociopath/1.0")
155155

156-
body, err := cache.FetchURL(ctx, c.cache, c.httpClient, req, c.logger)
156+
body, err := httpcache.FetchURL(ctx, c.cache, c.httpClient, req, c.logger)
157157
if err != nil {
158158
return nil, ""
159159
}

pkg/bluesky/bluesky_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func TestFetch(t *testing.T) {
7979
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
8080
w.Header().Set("Content-Type", "application/json")
8181
w.WriteHeader(http.StatusOK)
82-
_, _ = w.Write([]byte(mockJSON))
82+
_, _ = w.Write([]byte(mockJSON)) //nolint:errcheck // test helper
8383
}))
8484
defer server.Close()
8585

0 commit comments

Comments
 (0)