Skip to content

Commit

Permalink
cache fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
rusq committed Jan 19, 2024
1 parent 804b24c commit a97b641
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 19 deletions.
11 changes: 1 addition & 10 deletions internal/cache/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,15 +123,6 @@ type Credentials interface {
AuthProvider(ctx context.Context, workspace string, opts ...auth.Option) (auth.Provider, error)
}

// InitProvider initialises the auth.Provider depending on provided slack
// credentials. It returns auth.Provider or an error. The logic diagram is
// available in the doc/diagrams/auth_flow.puml.
//
// Deprecated: Use [Manager.Auth].
func InitProvider(ctx context.Context, cacheDir string, workspace string, creds Credentials) (auth.Provider, error) {
return initProvider(ctx, cacheDir, defCredsFile, workspace, creds)
}

// initProvider initialises the auth.Provider depending on provided slack
// credentials. It returns auth.Provider or an error. The logic diagram is
// available in the doc/diagrams/auth_flow.puml.
Expand All @@ -147,7 +138,7 @@ func InitProvider(ctx context.Context, cacheDir string, workspace string, creds
// operating system on the same machine, unless it's a clone of the source
// operating system on which the credentials storage was created.
func initProvider(ctx context.Context, cacheDir string, filename string, workspace string, creds Credentials, opts ...auth.Option) (auth.Provider, error) {
ctx, task := trace.NewTask(ctx, "InitProvider")
ctx, task := trace.NewTask(ctx, "initProvider")
defer task.End()

credsFile := filename
Expand Down
2 changes: 1 addition & 1 deletion internal/cache/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ func TestInitProvider(t *testing.T) {
tt.expect(mc)

// test
got, err := InitProvider(tt.args.ctx, tt.args.cacheDir, tt.args.workspace, mc)
got, err := initProvider(tt.args.ctx, tt.args.cacheDir, defCredsFile, tt.args.workspace, mc)
if (err != nil) != tt.wantErr {
t.Errorf("InitProvider() error = %v, wantErr %v", err, tt.wantErr)
return
Expand Down
4 changes: 4 additions & 0 deletions internal/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ func save[T any](cacheDir, filename string, suffix string, uu []T) error {
return nil
}

// read reads the data from the reader r until it reaches the EOF and returns
// 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?
Expand All @@ -113,6 +115,8 @@ func read[T any](r io.Reader) ([]T, error) {
return tt, nil
}

// 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) {
filename = makeCacheFilename(cacheDir, filename, suffix)

Expand Down
29 changes: 21 additions & 8 deletions internal/cache/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,19 @@ import (
"github.com/rusq/slackdump/v3/types"
)

// Manager is the workspace manager.
// Manager is the workspace manager. It is an abstraction over the directory
// with files with credentials for Slack workspaces.
//
// There are several types of files that one could find in the managed
// directory:
// - "provider.bin" - the default workspace file, it contains the credentials
// for the "default" workspace. It exists for historical reasons, for
// migration from the previous version of the slackdump. It contains
// credentials for the workspace that slackdump v2 was authenticated in.
// - "*.bin" - other workspaces, the filename is the name of the workspace.
// - "workspace.txt" - a pointer to the current workspace, it contains the
// current workspace name.
// - "*.cache" - cache files, they contain the cache for users and channels.
type Manager struct {
dir string
authOptions []auth.Option
Expand Down Expand Up @@ -85,9 +97,8 @@ func maybeAppendExt(filename string, ext string) string {
return filename
}

// NewManager creates a new workspace manager over the directory dir.
// The cache directory is created with rwx------ permissions, if it does
// not exist.
// NewManager creates a new workspace manager over the directory dir. The
// directory is created with rwx------ permissions, if it does not exist.
//
// TODO: test with empty dir.
func NewManager(dir string, opts ...Option) (*Manager, error) {
Expand Down Expand Up @@ -126,6 +137,8 @@ func (m *Manager) Auth(ctx context.Context, name string, c Credentials) (auth.Pr
return initProvider(ctx, m.dir, m.filename(name), name, c, m.authOptions...)
}

// ErrWorkspace is the error returned by the workspace manager, it contains the
// workspace name, the error message and the underlying error.
type ErrWorkspace struct {
Workspace string
Message string
Expand All @@ -139,15 +152,15 @@ func (ew *ErrWorkspace) Error() string {
return fmt.Sprintf("workspace %q: %s (error: %s)", ew.Workspace, ew.Message, ew.Err)
}

func newErrNoWorkspace(name string) *ErrWorkspace {
return &ErrWorkspace{Workspace: name, Message: "no such workspace"}
}

// Unwrap returns the underlying error.
func (ew *ErrWorkspace) Unwrap() error {
return ew.Err
}

func newErrNoWorkspace(name string) *ErrWorkspace {
return &ErrWorkspace{Workspace: name, Message: "no such workspace"}
}

// Delete deletes the workspace file.
func (m *Manager) Delete(name string) error {
if !m.Exists(name) {
Expand Down

0 comments on commit a97b641

Please sign in to comment.