Skip to content

Commit

Permalink
PR review
Browse files Browse the repository at this point in the history
  • Loading branch information
rusq committed Jan 18, 2025
1 parent d9f9e4d commit d04ba68
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 27 deletions.
3 changes: 1 addition & 2 deletions cmd/slackdump/internal/format/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"github.com/rusq/slack"

"github.com/rusq/slackdump/v3/cmd/slackdump/internal/bootstrap"
"github.com/rusq/slackdump/v3/cmd/slackdump/internal/workspace"

"github.com/rusq/slackdump/v3/cmd/slackdump/internal/cfg"
"github.com/rusq/slackdump/v3/cmd/slackdump/internal/golang/base"
Expand Down Expand Up @@ -264,7 +263,7 @@ var errNoMatch = errors.New("no matching users")
func searchCache(ctx context.Context, cacheDir string, ids []string) ([]slack.User, error) {
_, task := trace.NewTask(ctx, "searchCache")
defer task.End()
m, err := workspace.CacheMgr()
m, err := cache.NewManager(cacheDir, cache.WithMachineID(cfg.MachineIDOvr))
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion internal/cache/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ type createOpener interface {

var _ createOpener = encryptedFile{}

// encryptedFile is the encrypted file container.
// encryptedFile is the encrypted file wrapper.
type encryptedFile struct {
// machineID is the machine ID override. If it is empty, the actual machine
// ID is used.
Expand Down
4 changes: 2 additions & 2 deletions internal/cache/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,8 @@ func TestInitProvider(t *testing.T) {

// resetting credentials
credsFile := filepath.Join(testDir, defCredsFile)
container := encryptedFile{}
if err := saveCreds(container, credsFile, storedProv); err != nil {
co := encryptedFile{}
if err := saveCreds(co, credsFile, storedProv); err != nil {
t.Fatal(err)
}

Expand Down
18 changes: 13 additions & 5 deletions internal/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,14 @@ func writeSlice[T any](w io.Writer, tt []T) error {

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

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

// load loads the data from the file in the cache directory, and returns
// the data as a slice of T.
func load[T any](cacheDir, filename string, suffix string, maxAge time.Duration) ([]T, error) {
func load[T any](cacheDir, filename, suffix string, maxAge time.Duration, machineID string) ([]T, error) {
var opts []encio.Option
if machineID != "" {
opts = append(opts, encio.WithID(machineID))
}
filename = makeCacheFilename(cacheDir, filename, suffix)

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

f, err := encio.Open(filename)
f, err := encio.Open(filename, opts...)
if err != nil {
return nil, fmt.Errorf("failed to open %s: %w", filename, err)
}
Expand Down
9 changes: 5 additions & 4 deletions internal/cache/channelcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ import (
"time"

"github.com/rusq/slack"

"github.com/rusq/slackdump/v3/types"
)

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

// saveChannels saves channels to a file, naming the file based on the
// filename and the suffix. The file will be saved in the dirname.
func saveChannels(dirname, filename string, suffix string, cc []slack.Channel) error {
return save(dirname, filename, suffix, cc)
func (m *Manager) saveChannels(dirname, filename string, suffix string, cc []slack.Channel) error {
return save(dirname, filename, suffix, cc, m.machineID)
}
6 changes: 4 additions & 2 deletions internal/cache/channelcache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import (

"github.com/rusq/encio"
"github.com/rusq/slack"
"github.com/stretchr/testify/assert"

"github.com/rusq/slackdump/v3/internal/fixtures"
"github.com/rusq/slackdump/v3/types"
"github.com/stretchr/testify/assert"
)

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

assert.NoError(t, saveChannels(dir, testfile, testSuffix, testChannels))
var m Manager
assert.NoError(t, m.saveChannels(dir, testfile, testSuffix, testChannels))

reopenedF, err := encio.Open(makeCacheFilename(dir, testfile, testSuffix))
if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions internal/cache/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -409,22 +409,22 @@ func (m *Manager) WalkUsers(userFn func(path string, r io.Reader) error) error {

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

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

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

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

// CreateAndSelect creates a new workspace with the given provider and selects
Expand Down
8 changes: 4 additions & 4 deletions internal/cache/usercache.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ func ReadUsers(r io.Reader) (types.Users, error) {

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

// saveUsers saves the users to a file, naming the file based on the filename
// and the suffix. The file will be saved in the cache directory.
func saveUsers(dirname, filename string, suffix string, uu types.Users) error {
return save(dirname, filename, suffix, []slack.User(uu))
func (m *Manager) saveUsers(dirname, filename string, suffix string, uu types.Users) error {
return save(dirname, filename, suffix, []slack.User(uu), m.machineID)
}
9 changes: 6 additions & 3 deletions internal/cache/usercache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ func TestSaveUserCache(t *testing.T) {
dir := t.TempDir()
testfile := "test.json"

assert.NoError(t, saveUsers(dir, testfile, testSuffix, testUsers))
var m Manager
assert.NoError(t, m.saveUsers(dir, testfile, testSuffix, testUsers))

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

func gimmeTempFileWithUsers(t *testing.T, dir string) string {
f := gimmeTempFile(t, dir)
if err := saveUsers("", f, testSuffix, testUsers); err != nil {
var m Manager
if err := m.saveUsers("", f, testSuffix, testUsers); err != nil {
t.Fatal(err)
}
return f
Expand Down

0 comments on commit d04ba68

Please sign in to comment.