Skip to content

Commit d04ba68

Browse files
committed
PR review
1 parent d9f9e4d commit d04ba68

File tree

9 files changed

+40
-27
lines changed

9 files changed

+40
-27
lines changed

cmd/slackdump/internal/format/format.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import (
1616
"github.com/rusq/slack"
1717

1818
"github.com/rusq/slackdump/v3/cmd/slackdump/internal/bootstrap"
19-
"github.com/rusq/slackdump/v3/cmd/slackdump/internal/workspace"
2019

2120
"github.com/rusq/slackdump/v3/cmd/slackdump/internal/cfg"
2221
"github.com/rusq/slackdump/v3/cmd/slackdump/internal/golang/base"
@@ -264,7 +263,7 @@ var errNoMatch = errors.New("no matching users")
264263
func searchCache(ctx context.Context, cacheDir string, ids []string) ([]slack.User, error) {
265264
_, task := trace.NewTask(ctx, "searchCache")
266265
defer task.End()
267-
m, err := workspace.CacheMgr()
266+
m, err := cache.NewManager(cacheDir, cache.WithMachineID(cfg.MachineIDOvr))
268267
if err != nil {
269268
return nil, err
270269
}

internal/cache/auth.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ type createOpener interface {
252252

253253
var _ createOpener = encryptedFile{}
254254

255-
// encryptedFile is the encrypted file container.
255+
// encryptedFile is the encrypted file wrapper.
256256
type encryptedFile struct {
257257
// machineID is the machine ID override. If it is empty, the actual machine
258258
// ID is used.

internal/cache/auth_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,8 +222,8 @@ func TestInitProvider(t *testing.T) {
222222

223223
// resetting credentials
224224
credsFile := filepath.Join(testDir, defCredsFile)
225-
container := encryptedFile{}
226-
if err := saveCreds(container, credsFile, storedProv); err != nil {
225+
co := encryptedFile{}
226+
if err := saveCreds(co, credsFile, storedProv); err != nil {
227227
t.Fatal(err)
228228
}
229229

internal/cache/cache.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,14 @@ func writeSlice[T any](w io.Writer, tt []T) error {
8282

8383
// save saves the users to a file, naming the file based on the filename
8484
// and the suffix. The file will be saved in the cache directory.
85-
func save[T any](cacheDir, filename string, suffix string, uu []T) error {
85+
func save[T any](cacheDir, filename string, suffix string, uu []T, machineID string) error {
8686
filename = makeCacheFilename(cacheDir, filename, suffix)
8787

88-
f, err := encio.Create(filename)
88+
var opts []encio.Option
89+
if machineID != "" {
90+
opts = append(opts, encio.WithID(machineID))
91+
}
92+
f, err := encio.Create(filename, opts...)
8993
if err != nil {
9094
return fmt.Errorf("failed to create file %s: %w", filename, err)
9195
}
@@ -101,7 +105,7 @@ func save[T any](cacheDir, filename string, suffix string, uu []T) error {
101105
// it as a slice of T.
102106
func read[T any](r io.Reader) ([]T, error) {
103107
dec := json.NewDecoder(r)
104-
var tt = make([]T, 0, 500) // 500 T. reasonable?
108+
tt := make([]T, 0, 500) // 500 T. reasonable?
105109
for {
106110
var t T
107111
if err := dec.Decode(&t); err != nil {
@@ -117,14 +121,18 @@ func read[T any](r io.Reader) ([]T, error) {
117121

118122
// load loads the data from the file in the cache directory, and returns
119123
// the data as a slice of T.
120-
func load[T any](cacheDir, filename string, suffix string, maxAge time.Duration) ([]T, error) {
124+
func load[T any](cacheDir, filename, suffix string, maxAge time.Duration, machineID string) ([]T, error) {
125+
var opts []encio.Option
126+
if machineID != "" {
127+
opts = append(opts, encio.WithID(machineID))
128+
}
121129
filename = makeCacheFilename(cacheDir, filename, suffix)
122130

123131
if err := checkCacheFile(filename, maxAge); err != nil {
124132
return nil, fmt.Errorf("%s: %w", filename, err)
125133
}
126134

127-
f, err := encio.Open(filename)
135+
f, err := encio.Open(filename, opts...)
128136
if err != nil {
129137
return nil, fmt.Errorf("failed to open %s: %w", filename, err)
130138
}

internal/cache/channelcache.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@ import (
44
"time"
55

66
"github.com/rusq/slack"
7+
78
"github.com/rusq/slackdump/v3/types"
89
)
910

1011
// loadChannels tries to load channels from the file. If the file does not exist
1112
// or is older than maxAge, it returns an error.
12-
func loadChannels(dirname, filename string, suffix string, maxAge time.Duration) ([]slack.Channel, error) {
13-
uu, err := load[slack.Channel](dirname, filename, suffix, maxAge)
13+
func (m *Manager) loadChannels(dirname, filename string, suffix string, maxAge time.Duration) ([]slack.Channel, error) {
14+
uu, err := load[slack.Channel](dirname, filename, suffix, maxAge, m.machineID)
1415
if err != nil {
1516
return nil, err
1617
}
@@ -19,6 +20,6 @@ func loadChannels(dirname, filename string, suffix string, maxAge time.Duration)
1920

2021
// saveChannels saves channels to a file, naming the file based on the
2122
// filename and the suffix. The file will be saved in the dirname.
22-
func saveChannels(dirname, filename string, suffix string, cc []slack.Channel) error {
23-
return save(dirname, filename, suffix, cc)
23+
func (m *Manager) saveChannels(dirname, filename string, suffix string, cc []slack.Channel) error {
24+
return save(dirname, filename, suffix, cc, m.machineID)
2425
}

internal/cache/channelcache_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ import (
55

66
"github.com/rusq/encio"
77
"github.com/rusq/slack"
8+
"github.com/stretchr/testify/assert"
9+
810
"github.com/rusq/slackdump/v3/internal/fixtures"
911
"github.com/rusq/slackdump/v3/types"
10-
"github.com/stretchr/testify/assert"
1112
)
1213

1314
// testChannels is a test fixture for channels.
@@ -19,7 +20,8 @@ func TestSaveChannels(t *testing.T) {
1920
dir := t.TempDir()
2021
testfile := "test-chans.json"
2122

22-
assert.NoError(t, saveChannels(dir, testfile, testSuffix, testChannels))
23+
var m Manager
24+
assert.NoError(t, m.saveChannels(dir, testfile, testSuffix, testChannels))
2325

2426
reopenedF, err := encio.Open(makeCacheFilename(dir, testfile, testSuffix))
2527
if err != nil {

internal/cache/manager.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -409,22 +409,22 @@ func (m *Manager) WalkUsers(userFn func(path string, r io.Reader) error) error {
409409

410410
// LoadUsers loads user cache file no older than maxAge for teamID.
411411
func (m *Manager) LoadUsers(teamID string, maxAge time.Duration) ([]slack.User, error) {
412-
return loadUsers(m.dir, m.userFile, teamID, maxAge)
412+
return m.loadUsers(m.dir, m.userFile, teamID, maxAge)
413413
}
414414

415415
// CacheUsers saves users to user cache file for teamID.
416416
func (m *Manager) CacheUsers(teamID string, uu []slack.User) error {
417-
return saveUsers(m.dir, m.userFile, teamID, uu)
417+
return m.saveUsers(m.dir, m.userFile, teamID, uu)
418418
}
419419

420420
// LoadChannels loads channel cache no older than maxAge.
421421
func (m *Manager) LoadChannels(teamID string, maxAge time.Duration) ([]slack.Channel, error) {
422-
return loadChannels(m.dir, m.channelFile, teamID, maxAge)
422+
return m.loadChannels(m.dir, m.channelFile, teamID, maxAge)
423423
}
424424

425425
// CacheChannels saves channels to cache.
426426
func (m *Manager) CacheChannels(teamID string, cc []slack.Channel) error {
427-
return saveChannels(m.dir, m.channelFile, teamID, cc)
427+
return m.saveChannels(m.dir, m.channelFile, teamID, cc)
428428
}
429429

430430
// CreateAndSelect creates a new workspace with the given provider and selects

internal/cache/usercache.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ func ReadUsers(r io.Reader) (types.Users, error) {
1919

2020
// loadUsers tries to load the users from the file. If the file does not exist
2121
// or is older than maxAge, it returns an error.
22-
func loadUsers(dirname, filename string, suffix string, maxAge time.Duration) (types.Users, error) {
23-
uu, err := load[slack.User](dirname, filename, suffix, maxAge)
22+
func (m *Manager) loadUsers(dirname, filename string, suffix string, maxAge time.Duration) (types.Users, error) {
23+
uu, err := load[slack.User](dirname, filename, suffix, maxAge, m.machineID)
2424
if err != nil {
2525
return nil, err
2626
}
@@ -29,6 +29,6 @@ func loadUsers(dirname, filename string, suffix string, maxAge time.Duration) (t
2929

3030
// saveUsers saves the users to a file, naming the file based on the filename
3131
// and the suffix. The file will be saved in the cache directory.
32-
func saveUsers(dirname, filename string, suffix string, uu types.Users) error {
33-
return save(dirname, filename, suffix, []slack.User(uu))
32+
func (m *Manager) saveUsers(dirname, filename string, suffix string, uu types.Users) error {
33+
return save(dirname, filename, suffix, []slack.User(uu), m.machineID)
3434
}

internal/cache/usercache_test.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ func TestSaveUserCache(t *testing.T) {
2525
dir := t.TempDir()
2626
testfile := "test.json"
2727

28-
assert.NoError(t, saveUsers(dir, testfile, testSuffix, testUsers))
28+
var m Manager
29+
assert.NoError(t, m.saveUsers(dir, testfile, testSuffix, testUsers))
2930

3031
reopenedF, err := encio.Open(makeCacheFilename(dir, testfile, testSuffix))
3132
if err != nil {
@@ -64,7 +65,8 @@ func TestLoadUserCache(t *testing.T) {
6465
}
6566
for _, tt := range tests {
6667
t.Run(tt.name, func(t *testing.T) {
67-
got, err := loadUsers("", tt.args.filename, testSuffix, tt.args.maxAge)
68+
var m Manager
69+
got, err := m.loadUsers("", tt.args.filename, testSuffix, tt.args.maxAge)
6870
if (err != nil) != tt.wantErr {
6971
t.Errorf("Session.loadUserCache() error = %v, wantErr %v", err, tt.wantErr)
7072
return
@@ -187,7 +189,8 @@ func gimmeTempFile(t *testing.T, dir string) string {
187189

188190
func gimmeTempFileWithUsers(t *testing.T, dir string) string {
189191
f := gimmeTempFile(t, dir)
190-
if err := saveUsers("", f, testSuffix, testUsers); err != nil {
192+
var m Manager
193+
if err := m.saveUsers("", f, testSuffix, testUsers); err != nil {
191194
t.Fatal(err)
192195
}
193196
return f

0 commit comments

Comments
 (0)