From 2434c9d56941d8ef4be97314be68ef7c13f38403 Mon Sep 17 00:00:00 2001 From: Rustam Gilyazov <16064414+rusq@users.noreply.github.com> Date: Sun, 10 Nov 2024 11:56:26 +1000 Subject: [PATCH] stfu linter --- auth/auth_ui/validation.go | 42 ++++++------- auth/browser/client.go | 6 +- cmd/slackdump/internal/diag/encrypt.go | 6 +- cmd/slackdump/internal/diag/rawoutput.go | 5 +- cmd/slackdump/internal/diag/search.go | 4 +- cmd/slackdump/internal/diag/thread.go | 4 +- .../internal/emoji/emojidl/emoji_test.go | 5 -- cmd/slackdump/internal/export/v3.go | 10 +-- cmd/slackdump/internal/export/v3_test.go | 26 -------- cmd/slackdump/internal/golang/base/ui.go | 6 +- cmd/slackdump/internal/list/users.go | 1 - cmd/slackdump/internal/ui/ask/timerange.go | 1 - .../internal/ui/bubbles/btime/btime.go | 7 --- cmd/slackdump/internal/wizard/config.go | 30 --------- downloader/deprecated_test.go | 6 +- go.mod | 4 +- go.sum | 8 +-- internal/cache/auth.go | 2 +- internal/chunk/dirproc/dirproc.go | 7 +-- internal/chunk/dirproc/dirproc_test.go | 63 +++++++++++++++++++ internal/edge/edge.go | 4 +- internal/fixtures/api.go | 2 +- internal/fixtures/fixtures.go | 11 +++- internal/viewer/renderer/slack.go | 9 ++- stream/stream_test.go | 6 +- 25 files changed, 150 insertions(+), 125 deletions(-) delete mode 100644 cmd/slackdump/internal/wizard/config.go create mode 100644 internal/chunk/dirproc/dirproc_test.go diff --git a/auth/auth_ui/validation.go b/auth/auth_ui/validation.go index 01d1c88e..94e019bc 100644 --- a/auth/auth_ui/validation.go +++ b/auth/auth_ui/validation.go @@ -10,28 +10,28 @@ var ( ErrRequired = errors.New("can not be empty") ) -func valURLSafe(s string) error { - for _, c := range s { - if !isRuneURLSafe(c) { - return ErrNotURLSafe - } - } - return nil -} +// func valURLSafe(s string) error { +// for _, c := range s { +// if !isRuneURLSafe(c) { +// return ErrNotURLSafe +// } +// } +// return nil +// } -func isRuneURLSafe(r rune) bool { - switch { - case 'a' <= r && r <= 'z': - return true - case 'A' <= r && r <= 'Z': - return true - case '0' <= r && r <= '9': - return true - case r == '-' || r == '.' || r == '_' || r == '~': - return true - } - return false -} +// func isRuneURLSafe(r rune) bool { +// switch { +// case 'a' <= r && r <= 'z': +// return true +// case 'A' <= r && r <= 'Z': +// return true +// case '0' <= r && r <= '9': +// return true +// case r == '-' || r == '.' || r == '_' || r == '~': +// return true +// } +// return false +// } func valRequired(s string) error { if s == "" { diff --git a/auth/browser/client.go b/auth/browser/client.go index b1842412..0a8a976f 100644 --- a/auth/browser/client.go +++ b/auth/browser/client.go @@ -81,7 +81,11 @@ func (cl *Client) Authenticate(ctx context.Context) (string, []*http.Cookie, err if err != nil { return "", nil, err } - defer pw.Stop() + defer func() { + if err := pw.Stop(); err != nil { + l().Printf("failed to stop playwright: %v", err) + } + }() opts := playwright.BrowserTypeLaunchOptions{ Headless: _b(false), diff --git a/cmd/slackdump/internal/diag/encrypt.go b/cmd/slackdump/internal/diag/encrypt.go index a2c591a3..7767de90 100644 --- a/cmd/slackdump/internal/diag/encrypt.go +++ b/cmd/slackdump/internal/diag/encrypt.go @@ -7,11 +7,11 @@ import ( "os" "strings" + "github.com/ProtonMail/go-crypto/openpgp" + "github.com/ProtonMail/go-crypto/openpgp/armor" + "github.com/ProtonMail/go-crypto/openpgp/packet" "github.com/rusq/slackdump/v3/cmd/slackdump/internal/cfg" "github.com/rusq/slackdump/v3/cmd/slackdump/internal/golang/base" - "golang.org/x/crypto/openpgp" - "golang.org/x/crypto/openpgp/armor" - "golang.org/x/crypto/openpgp/packet" ) // pub rsa4096 2020-03-22 [SC] [expires: 2029-03-21] diff --git a/cmd/slackdump/internal/diag/rawoutput.go b/cmd/slackdump/internal/diag/rawoutput.go index 731e55ee..c166feda 100644 --- a/cmd/slackdump/internal/diag/rawoutput.go +++ b/cmd/slackdump/internal/diag/rawoutput.go @@ -186,7 +186,10 @@ func sendReq(w io.Writer, cl *http.Client, ep string, v url.Values) (bool, error log.Printf("error while retrieving body: %s", err) } if resp.StatusCode != http.StatusOK { - io.Copy(w, bytes.NewReader(data)) + _, err := io.Copy(w, bytes.NewReader(data)) + if err != nil { + return false, err + } return false, fmt.Errorf("server NOT OK: %s", resp.Status) } if len(data) == 0 { diff --git a/cmd/slackdump/internal/diag/search.go b/cmd/slackdump/internal/diag/search.go index d3654256..d5318fc8 100644 --- a/cmd/slackdump/internal/diag/search.go +++ b/cmd/slackdump/internal/diag/search.go @@ -107,7 +107,9 @@ func runSearch(ctx context.Context, cmd *base.Command, args []string) error { lg.Printf("cursor %s", sm.NextCursor) p.Cursor = sm.NextCursor - lim.Wait(ctx) + if err := lim.Wait(ctx); err != nil { + return err + } } return nil diff --git a/cmd/slackdump/internal/diag/thread.go b/cmd/slackdump/internal/diag/thread.go index de8c8a6c..0f2f25aa 100644 --- a/cmd/slackdump/internal/diag/thread.go +++ b/cmd/slackdump/internal/diag/thread.go @@ -108,7 +108,7 @@ func generateThread(ctx context.Context, client *slack.Client, channelID string, l := network.NewLimiter(network.Tier3, network.DefLimits.Tier3.Burst, int(network.DefLimits.Tier3.Boost)) pb := progressbar.Default(int64(numMsg)) pb.Describe("posting messages") - defer pb.Finish() + defer func() { _ = pb.Finish() }() for i := 0; i < numMsg; i++ { if err := network.WithRetry(ctx, l, 3, func() error { _, _, err := client.PostMessageContext(ctx, channelID, slack.MsgOptionTS(ts), slack.MsgOptionText(fmt.Sprintf("message: %d", i), false)) @@ -144,7 +144,7 @@ func delMessages(ctx context.Context, client *slack.Client, channelID string, ms pb := progressbar.Default(int64(len(msgs))) pb.Describe("deleting messages") - defer pb.Finish() + defer func() { _ = pb.Finish() }() l := network.NewLimiter(network.Tier3, network.DefLimits.Tier3.Burst, int(network.DefLimits.Tier3.Boost)) for _, m := range msgs { diff --git a/cmd/slackdump/internal/emoji/emojidl/emoji_test.go b/cmd/slackdump/internal/emoji/emojidl/emoji_test.go index 8f3ea24c..94a11e66 100644 --- a/cmd/slackdump/internal/emoji/emojidl/emoji_test.go +++ b/cmd/slackdump/internal/emoji/emojidl/emoji_test.go @@ -13,7 +13,6 @@ import ( "reflect" "sync" "testing" - "time" "go.uber.org/mock/gomock" @@ -37,10 +36,6 @@ func setGlobalFetchFn(fn fetchFunc) { fetchFn = fn } -func init() { - rand.Seed(time.Now().UnixNano()) -} - func Test_fetchEmoji(t *testing.T) { type args struct { ctx context.Context diff --git a/cmd/slackdump/internal/export/v3.go b/cmd/slackdump/internal/export/v3.go index 514f447c..3078980c 100644 --- a/cmd/slackdump/internal/export/v3.go +++ b/cmd/slackdump/internal/export/v3.go @@ -38,7 +38,7 @@ func exportV3(ctx context.Context, sess *slackdump.Session, fsa fsadapter.FS, li } defer chunkdir.Close() if !lg.IsDebug() { - defer chunkdir.RemoveAll() + defer func() { _ = chunkdir.RemoveAll() }() } updFn := func() func(_ *slack.Channel, m *slack.Message) error { // hack: wrapper around the message update function, which does not @@ -63,7 +63,7 @@ func exportV3(ctx context.Context, sess *slackdump.Session, fsa fsadapter.FS, li progressbar.OptionSpinnerType(8)), lg.IsDebug(), ) - pb.RenderBlank() + _ = pb.RenderBlank() stream := sess.Stream( stream.OptOldest(params.Oldest), @@ -71,7 +71,7 @@ func exportV3(ctx context.Context, sess *slackdump.Session, fsa fsadapter.FS, li stream.OptResultFn(func(sr stream.Result) error { lg.Debugf("conversations: %s", sr.String()) pb.Describe(sr.String()) - pb.Add(1) + _ = pb.Add(1) return nil }), ) @@ -90,10 +90,10 @@ func exportV3(ctx context.Context, sess *slackdump.Session, fsa fsadapter.FS, li lg.Print("running export...") if err := ctr.Run(ctx, list); err != nil { - pb.Finish() + _ = pb.Finish() return err } - pb.Finish() + _ = pb.Finish() // at this point no goroutines are running, we are safe to assume that // everything we need is in the chunk directory. if err := conv.WriteIndex(); err != nil { diff --git a/cmd/slackdump/internal/export/v3_test.go b/cmd/slackdump/internal/export/v3_test.go index ed54e6f4..c64adc17 100644 --- a/cmd/slackdump/internal/export/v3_test.go +++ b/cmd/slackdump/internal/export/v3_test.go @@ -1,10 +1,7 @@ package export import ( - "bytes" - "compress/gzip" "context" - "io" "log" "net/http" "os" @@ -93,26 +90,3 @@ func Test_exportV3(t *testing.T) { } }) } - -func load(t *testing.T, filename string) io.ReadSeeker { - absPath, err := filepath.Abs(filename) - if err != nil { - t.Fatal(err) - } - t.Log("test file", absPath) - f, err := os.Open(absPath) - if err != nil { - t.Fatal(err) - } - defer f.Close() - gz, err := gzip.NewReader(f) - if err != nil { - t.Fatal(err) - } - var buf bytes.Buffer - _, err = io.Copy(&buf, gz) - if err != nil { - t.Fatal(err) - } - return bytes.NewReader(buf.Bytes()) -} diff --git a/cmd/slackdump/internal/golang/base/ui.go b/cmd/slackdump/internal/golang/base/ui.go index 04609638..6cf2e59d 100644 --- a/cmd/slackdump/internal/golang/base/ui.go +++ b/cmd/slackdump/internal/golang/base/ui.go @@ -15,7 +15,11 @@ func YesNoWR(w io.Writer, r io.Reader, message string) bool { for { fmt.Fprint(w, message, "? (y/N) ") var resp string - fmt.Fscanln(r, &resp) + _, err := fmt.Fscanln(r, &resp) + if err != nil { + fmt.Fprintln(w, "Please answer yes or no and press Enter or Return.") + continue + } resp = strings.TrimSpace(resp) if len(resp) > 0 { switch strings.ToLower(resp)[0] { diff --git a/cmd/slackdump/internal/list/users.go b/cmd/slackdump/internal/list/users.go index 605b9def..6e3ab60e 100644 --- a/cmd/slackdump/internal/list/users.go +++ b/cmd/slackdump/internal/list/users.go @@ -77,7 +77,6 @@ func (u *users) Retrieve(ctx context.Context, sess *slackdump.Session, m *cache. if err != nil { return err } - m.CacheUsers(sess.Info().TeamID, users) u.data = users return nil } diff --git a/cmd/slackdump/internal/ui/ask/timerange.go b/cmd/slackdump/internal/ui/ask/timerange.go index c4f58dfd..02e3b91f 100644 --- a/cmd/slackdump/internal/ui/ask/timerange.go +++ b/cmd/slackdump/internal/ui/ask/timerange.go @@ -25,7 +25,6 @@ func TimeRange() (oldest, latest time.Time, err error) { if oldest, err = ui.Time("Earliest message"); err != nil && !errors.Is(err, ui.ErrEmptyOptionalInput) { return } - err = nil if latest, err = ui.Time("Latest message"); err != nil && !errors.Is(err, ui.ErrEmptyOptionalInput) { return } diff --git a/cmd/slackdump/internal/ui/bubbles/btime/btime.go b/cmd/slackdump/internal/ui/bubbles/btime/btime.go index 5c1c38c6..50f2abb9 100644 --- a/cmd/slackdump/internal/ui/bubbles/btime/btime.go +++ b/cmd/slackdump/internal/ui/bubbles/btime/btime.go @@ -187,13 +187,6 @@ func (m *Model) Update(msg tea.Msg) (*Model, tea.Cmd) { return m, nil } -func (m *Model) whatIf(digit int, hasVal int) int { - whatIf := make([]int, len(m.entry)) - copy(whatIf, m.entry[:]) - whatIf[digit] = hasVal - return tupleVal(whatIf, m.cursor/2) -} - func (m *Model) updateTime() { hour := tupleVal(m.entry[:], 0) minute := tupleVal(m.entry[:], 1) diff --git a/cmd/slackdump/internal/wizard/config.go b/cmd/slackdump/internal/wizard/config.go deleted file mode 100644 index 0e719c34..00000000 --- a/cmd/slackdump/internal/wizard/config.go +++ /dev/null @@ -1,30 +0,0 @@ -package wizard - -import ( - "errors" -) - -// initFlags initializes flags based on the key-value pairs. -// Example: -// -// var ( -// enterpriseMode bool -// downloadFiles bool -// ) -// -// flags, err := initFlags(enterpriseMode, "enterprise", downloadFiles, "files") -// if err != nil { -// return err -// } -func initFlags(keyval ...any) ([]string, error) { - var flags []string - if len(keyval)%2 != 0 { - return flags, errors.New("initFlags: odd number of key-value pairs") - } - for i := 0; i < len(keyval); i += 2 { - if keyval[i].(bool) { - flags = append(flags, keyval[i+1].(string)) - } - } - return flags, nil -} diff --git a/downloader/deprecated_test.go b/downloader/deprecated_test.go index 079216ed..a61ec32b 100644 --- a/downloader/deprecated_test.go +++ b/downloader/deprecated_test.go @@ -33,6 +33,8 @@ var ( file9 = slack.File{ID: "f9", Name: "filename9.ext", URLPrivateDownload: "file9_url", Size: 900} ) +// TODO: figure out why this is deprecated. + func TestSession_SaveFileTo(t *testing.T) { tmpdir := t.TempDir() @@ -73,7 +75,7 @@ func TestSession_SaveFileTo(t *testing.T) { func(mc *mock_downloader.MockDownloader) { mc.EXPECT(). GetFile("file1_url", gomock.Any()). - SetArg(1, *fixtures.FilledFile(file1.Size)). // to mock the file size. + SetArg(1, *fixtures.FilledFile(t, file1.Size)). // to mock the file size. Return(nil) }, int64(file1.Size), @@ -171,7 +173,7 @@ func TestSession_saveFile(t *testing.T) { func(mc *mock_downloader.MockDownloader) { mc.EXPECT(). GetFile("file1_url", gomock.Any()). - SetArg(1, *fixtures.FilledFile(file1.Size)). + SetArg(1, *fixtures.FilledFile(t, file1.Size)). Return(nil) }, int64(file1.Size), diff --git a/go.mod b/go.mod index fdb716a0..0e7152d9 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.23 require ( github.com/MercuryEngineering/CookieMonster v0.0.0-20180304172713-1584578b3403 + github.com/ProtonMail/go-crypto v1.1.2 github.com/charmbracelet/bubbles v0.20.0 github.com/charmbracelet/bubbletea v1.1.2 github.com/charmbracelet/huh v0.6.0 @@ -36,7 +37,6 @@ require ( github.com/yuin/goldmark v1.7.8 github.com/yuin/goldmark-emoji v1.0.4 go.uber.org/mock v0.5.0 - golang.org/x/crypto v0.28.0 golang.org/x/sync v0.8.0 golang.org/x/term v0.25.0 golang.org/x/text v0.19.0 @@ -52,6 +52,7 @@ require ( github.com/charmbracelet/x/ansi v0.4.2 // indirect github.com/charmbracelet/x/exp/strings v0.0.0-20241101155414-3df16cb7eefd // indirect github.com/charmbracelet/x/term v0.2.0 // indirect + github.com/cloudflare/circl v1.3.7 // indirect github.com/deckarep/golang-set/v2 v2.6.0 // indirect github.com/denisbrodbeck/machineid v1.0.1 // indirect github.com/dustin/go-humanize v1.0.1 // indirect @@ -81,6 +82,7 @@ require ( github.com/ysmood/gson v0.7.3 // indirect github.com/ysmood/leakless v0.9.0 // indirect go.uber.org/multierr v1.11.0 // indirect + golang.org/x/crypto v0.28.0 // indirect golang.org/x/exp v0.0.0-20241009180824-f66d83c29e7c // indirect golang.org/x/net v0.30.0 // indirect golang.org/x/sys v0.26.0 // indirect diff --git a/go.sum b/go.sum index 13919d6c..c79a3e56 100644 --- a/go.sum +++ b/go.sum @@ -2,6 +2,8 @@ github.com/MakeNowJust/heredoc v1.0.0 h1:cXCdzVdstXyiTqTvfqk9SDHpKNjxuom+DOlyEeQ github.com/MakeNowJust/heredoc v1.0.0/go.mod h1:mG5amYoWBHf8vpLOuehzbGGw0EHxpZZ6lCpQ4fNJ8LE= github.com/MercuryEngineering/CookieMonster v0.0.0-20180304172713-1584578b3403 h1:EtZwYyLbkEcIt+B//6sujwRCnHuTEK3qiSypAX5aJeM= github.com/MercuryEngineering/CookieMonster v0.0.0-20180304172713-1584578b3403/go.mod h1:mM6WvakkX2m+NgMiPCfFFjwfH4KzENC07zeGEqq9U7s= +github.com/ProtonMail/go-crypto v1.1.2 h1:A7JbD57ThNqh7XjmHE+PXpQ3Dqt3BrSAC0AL0Go3KS0= +github.com/ProtonMail/go-crypto v1.1.2/go.mod h1:rA3QumHc/FZ8pAHreoekgiAbzpNsfQAosU5td4SnOrE= github.com/atotto/clipboard v0.1.4 h1:EH0zSVneZPSuFR11BlR9YppQTVDbh5+16AmcJi4g1z4= github.com/atotto/clipboard v0.1.4/go.mod h1:ZY9tmq7sm5xIbd9bOK4onWV4S6X0u6GY7Vn0Yu86PYI= github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k= @@ -14,8 +16,6 @@ github.com/charmbracelet/bubbles v0.20.0 h1:jSZu6qD8cRQ6k9OMfR1WlM+ruM8fkPWkHvQW github.com/charmbracelet/bubbles v0.20.0/go.mod h1:39slydyswPy+uVOHZ5x/GjwVAFkCsV8IIVy+4MhzwwU= github.com/charmbracelet/bubbletea v1.1.2 h1:naQXF2laRxyLyil/i7fxdpiz1/k06IKquhm4vBfHsIc= github.com/charmbracelet/bubbletea v1.1.2/go.mod h1:9HIU/hBV24qKjlehyj8z1r/tR9TYTQEag+cWZnuXo8E= -github.com/charmbracelet/huh v0.5.3 h1:3KLP4a/K1/S4dq4xFMTNMt3XWhgMl/yx8NYtygQ0bmg= -github.com/charmbracelet/huh v0.5.3/go.mod h1:OZC3lshuF+/y8laj//DoZdFSHxC51OrtXLJI8xWVouQ= github.com/charmbracelet/huh v0.6.0 h1:mZM8VvZGuE0hoDXq6XLxRtgfWyTI3b2jZNKh0xWmax8= github.com/charmbracelet/huh v0.6.0/go.mod h1:GGNKeWCeNzKpEOh/OJD8WBwTQjV3prFAtQPpLv+AVwU= github.com/charmbracelet/huh/spinner v0.0.0-20241028115900-20a4d21717a8 h1:g+Bz64hsMLTf3lAgUqI6Rj1YEAlm/HN39IuhyneCokc= @@ -32,6 +32,8 @@ github.com/charmbracelet/x/term v0.2.0 h1:cNB9Ot9q8I711MyZ7myUR5HFWL/lc3OpU8jZ4h github.com/charmbracelet/x/term v0.2.0/go.mod h1:GVxgxAbjUrmpvIINHIQnJJKpMlHiZ4cktEQCN6GWyF0= github.com/chengxilo/virtualterm v1.0.4 h1:Z6IpERbRVlfB8WkOmtbHiDbBANU7cimRIof7mk9/PwM= github.com/chengxilo/virtualterm v1.0.4/go.mod h1:DyxxBZz/x1iqJjFxTFcr6/x+jSpqN0iwWCOK1q10rlY= +github.com/cloudflare/circl v1.3.7 h1:qlCDlTPz2n9fu58M0Nh1J/JzcFpfgkFHHX3O35r5vcU= +github.com/cloudflare/circl v1.3.7/go.mod h1:sRTcRWXGLrKw6yIGJ+l7amYJFfAXbZG0kBSc8r4zxgA= github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= @@ -133,8 +135,6 @@ github.com/rusq/rbubbles v0.0.2 h1:U+rkywxtmBw0fdXABTCyND2YUZW9xydsxE12Co0tsFA= github.com/rusq/rbubbles v0.0.2/go.mod h1:wOrwl1AiCCmaL9fLnjKDajOP4IglSC84fH7a74VsnLk= github.com/rusq/secure v0.0.4 h1:svpiZHfHnx89eEDCCFI9OXG1Y8hL9kUWUG6fJbrWUOI= github.com/rusq/secure v0.0.4/go.mod h1:F1QilMKreuFRjov0UY7DZSIXn77/8RqMVGu2zV0RtqY= -github.com/rusq/slack v0.9.6-0.20240712095442-5a0e2e405a99 h1:dqEcNs9hMc2PiMwhw8+Zi3wF8GNUHIK5OItZ01iM0Vk= -github.com/rusq/slack v0.9.6-0.20240712095442-5a0e2e405a99/go.mod h1:9O0zQAFN6W47z4KpTQbe6vOHOzBO76vMg1+gthPwaTI= github.com/rusq/slack v0.9.6-0.20241104074952-d9b6e02955fa h1:meNaDH2eLwjAqvOxMlgb5+gaLz3Kufm9rVFkALhsCRs= github.com/rusq/slack v0.9.6-0.20241104074952-d9b6e02955fa/go.mod h1:9O0zQAFN6W47z4KpTQbe6vOHOzBO76vMg1+gthPwaTI= github.com/rusq/slackauth v0.5.1 h1:l+Gj96kYzHmljMYglRv76kgzuOJr/QbXDDA8JHyN71Q= diff --git a/internal/cache/auth.go b/internal/cache/auth.go index ca08a569..2245a815 100644 --- a/internal/cache/auth.go +++ b/internal/cache/auth.go @@ -159,7 +159,7 @@ func initProvider(ctx context.Context, cacheDir string, filename string, workspa if creds == nil || creds.IsEmpty() { if prov, err := tryLoad(ctx, credsFile); err != nil { msg := fmt.Sprintf("failed to load saved credentials: %s", err) - trace.Logf(ctx, "warn", msg) + trace.Log(ctx, "warn", msg) if auth.IsInvalidAuthErr(err) { lg.Println("authentication details expired, relogin is necessary") } diff --git a/internal/chunk/dirproc/dirproc.go b/internal/chunk/dirproc/dirproc.go index 2654497d..27f88ab1 100644 --- a/internal/chunk/dirproc/dirproc.go +++ b/internal/chunk/dirproc/dirproc.go @@ -37,16 +37,15 @@ func newDirProc(cd *chunk.Directory, name chunk.FileID) (*dirproc, error) { // Close closes the processor and the underlying chunk file. func (p *dirproc) Close() error { - if p.closed.Load() { + if !p.closed.CompareAndSwap(false, true) { return nil } var errs error if err := p.Recorder.Close(); err != nil { - errors.Join(errs, err) + errs = errors.Join(errs, err) } - p.closed.Store(true) if err := p.wc.Close(); err != nil { - errors.Join(errs, err) + errs = errors.Join(errs, err) } return errs } diff --git a/internal/chunk/dirproc/dirproc_test.go b/internal/chunk/dirproc/dirproc_test.go new file mode 100644 index 00000000..ee5005d6 --- /dev/null +++ b/internal/chunk/dirproc/dirproc_test.go @@ -0,0 +1,63 @@ +// Package dirproc is a processor that writes the data into gzipped files in a +// directory. Each conversation is output to a separate gzipped JSONL file. +// If a thread is given, the filename will have the thread ID in it. +package dirproc + +import ( + "sync/atomic" + "testing" + + "github.com/rusq/slackdump/v3/internal/chunk" +) + +type mockWriteCloser struct { + WriteCalled atomic.Bool + CloseCalled atomic.Bool +} + +func (m *mockWriteCloser) Write(p []byte) (n int, err error) { + m.WriteCalled.Store(true) + return 0, nil +} + +func (m *mockWriteCloser) Close() error { + m.CloseCalled.Store(true) + return nil +} + +func Test_dirproc_Close(t *testing.T) { + tests := []struct { + name string + fields *dirproc + prep func(d *dirproc) + wantErr bool + }{ + { + "already closed", + &dirproc{}, + func(d *dirproc) { + d.closed.Store(true) + }, + false, + }, + { + "close ok", + &dirproc{ + Recorder: &chunk.Recorder{}, + wc: &mockWriteCloser{}, + }, + nil, + false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if tt.prep != nil { + tt.prep(tt.fields) + } + if err := tt.fields.Close(); (err != nil) != tt.wantErr { + t.Errorf("dirproc.Close() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} diff --git a/internal/edge/edge.go b/internal/edge/edge.go index 1a22b4a2..265e234c 100644 --- a/internal/edge/edge.go +++ b/internal/edge/edge.go @@ -211,7 +211,9 @@ func (cl *Client) PostForm(ctx context.Context, path string, form url.Values) (* func (cl *Client) record(b []byte) { if cl.tape != nil { - cl.tape.Write(b) + if _, err := cl.tape.Write(b); err != nil { + logger.Default.Printf("error writing to tape: %s", err) + } } } diff --git a/internal/fixtures/api.go b/internal/fixtures/api.go index 6e629729..0fb8ab41 100644 --- a/internal/fixtures/api.go +++ b/internal/fixtures/api.go @@ -23,6 +23,6 @@ func TestServer(t *testing.T, code int, response []byte) *httptest.Server { t.Helper() return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(code) - w.Write(response) + _, _ = w.Write(response) })) } diff --git a/internal/fixtures/fixtures.go b/internal/fixtures/fixtures.go index 33a5e6df..c3cf8077 100644 --- a/internal/fixtures/fixtures.go +++ b/internal/fixtures/fixtures.go @@ -44,13 +44,18 @@ func FilledBuffer(sz int) *bytes.Buffer { } // FilledFile returns a file that filled with sz bytes of 0x00. -func FilledFile(sz int) *os.File { +func FilledFile(t *testing.T, sz int) *os.File { + t.Helper() f, err := os.CreateTemp("", "sdunit*") if err != nil { panic(err) } - f.Write(bytes.Repeat([]byte{0x00}, sz)) - f.Seek(0, io.SeekStart) + if _, err := f.Write(bytes.Repeat([]byte{0x00}, sz)); err != nil { + t.Fatal(err) + } + if _, err := f.Seek(0, io.SeekStart); err != nil { + t.Fatal(err) + } return f } diff --git a/internal/viewer/renderer/slack.go b/internal/viewer/renderer/slack.go index 3fd0fcff..7f07c92f 100644 --- a/internal/viewer/renderer/slack.go +++ b/internal/viewer/renderer/slack.go @@ -6,6 +6,7 @@ import ( "encoding/json" "fmt" "html/template" + "log" "log/slog" "os" "strings" @@ -118,8 +119,12 @@ func maybeprint(v any) { if debug { enc := json.NewEncoder(os.Stderr) enc.SetIndent("", " ") - enc.Encode(v) - os.Stderr.Sync() + if err := enc.Encode(v); err != nil { + log.Printf("error printing value: %s", err) + } + if err := os.Stderr.Sync(); err != nil { + log.Printf("error flushing stderr: %s", err) + } } } diff --git a/stream/stream_test.go b/stream/stream_test.go index 9578cc5e..c4d6ba7c 100644 --- a/stream/stream_test.go +++ b/stream/stream_test.go @@ -283,7 +283,11 @@ func Test_processLink(t *testing.T) { func TestStream_Users(t *testing.T) { ctx := context.Background() srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - w.Write([]byte(`{"ok":false,"error":"not_authed"}`)) + t.Helper() + _, err := w.Write([]byte(`{"ok":false,"error":"not_authed"}`)) + if err != nil { + t.Error(err) + } })) defer srv.Close() l := rateLimits{