From 4cb3038b679bc1d478adff8b3d0d37156b497cb4 Mon Sep 17 00:00:00 2001 From: Rustam Gilyazov <16064414+rusq@users.noreply.github.com> Date: Tue, 16 Jul 2024 21:22:52 +1000 Subject: [PATCH] fix workspace list to use custom cache dir --- cmd/slackdump/internal/workspace/auth.go | 2 +- cmd/slackdump/internal/workspace/list.go | 2 +- internal/cache/manager.go | 5 +- internal/osext/dir.go | 13 +++++ internal/osext/dir_test.go | 68 ++++++++++++++++++++++++ 5 files changed, 87 insertions(+), 3 deletions(-) create mode 100644 internal/osext/dir_test.go diff --git a/cmd/slackdump/internal/workspace/auth.go b/cmd/slackdump/internal/workspace/auth.go index 85658697..c87e617d 100644 --- a/cmd/slackdump/internal/workspace/auth.go +++ b/cmd/slackdump/internal/workspace/auth.go @@ -16,7 +16,7 @@ import ( const baseCommand = "slackdump workspace" -var flagmask = cfg.OmitAll +var flagmask = cfg.OmitAll &^ cfg.OmitCacheDir var CmdWorkspace = &base.Command{ Run: nil, diff --git a/cmd/slackdump/internal/workspace/list.go b/cmd/slackdump/internal/workspace/list.go index 9025c415..c8e2d3e6 100644 --- a/cmd/slackdump/internal/workspace/list.go +++ b/cmd/slackdump/internal/workspace/list.go @@ -209,7 +209,7 @@ func userInfo(ctx context.Context, m manager, name string) (*slack.AuthTestRespo func printFull(m manager, current string, wsps []string) { fmt.Printf("Workspaces in %q:\n\n", cfg.CacheDir()) for _, row := range simpleList(context.Background(), m, current, wsps) { - fmt.Printf("%s (file: %s, last modified: %s)", row[0], row[1], row[2]) + fmt.Printf("%s (file: %s, last modified: %s)\n", row[0], row[1], row[2]) } fmt.Printf("\nCurrent workspace is marked with ' %s '.\n", defMark) } diff --git a/internal/cache/manager.go b/internal/cache/manager.go index 2147eb76..461b2af8 100644 --- a/internal/cache/manager.go +++ b/internal/cache/manager.go @@ -17,6 +17,7 @@ import ( "github.com/rusq/slack" "github.com/rusq/slackdump/v3/auth" + "github.com/rusq/slackdump/v3/internal/osext" ) // Manager is the workspace manager. It is an abstraction over the directory @@ -312,7 +313,9 @@ func (m *Manager) filepath(name string) string { // name returns the workspace name from the filename. func (m *Manager) name(filename string) (string, error) { - if filedir := filepath.Dir(filename); !strings.EqualFold(filedir, m.dir) { + filedir := filepath.Dir(filename) + same, err := osext.Same(filedir, m.dir) + if err != nil || !same { return "", fmt.Errorf("incorrect directory: %s", filedir) } if filepath.Ext(filename) != wspExt { diff --git a/internal/osext/dir.go b/internal/osext/dir.go index c84ad170..f0963cba 100644 --- a/internal/osext/dir.go +++ b/internal/osext/dir.go @@ -3,6 +3,7 @@ package osext import ( "errors" "os" + "path/filepath" ) var ErrNotADir = errors.New("not a directory") @@ -17,3 +18,15 @@ func DirExists(dir string) error { } return nil } + +func Same(path1, path2 string) (bool, error) { + ap1, err := filepath.Abs(path1) + if err != nil { + return false, err + } + ap2, err := filepath.Abs(path2) + if err != nil { + return false, err + } + return ap1 == ap2, nil +} diff --git a/internal/osext/dir_test.go b/internal/osext/dir_test.go new file mode 100644 index 00000000..041fc220 --- /dev/null +++ b/internal/osext/dir_test.go @@ -0,0 +1,68 @@ +package osext + +import ( + "os" + "path/filepath" + "testing" +) + +func TestSame(t *testing.T) { + baseDir := t.TempDir() + + file1 := filepath.Join(baseDir, "file1") + file2 := filepath.Join(baseDir, "file2") + + wd, err := os.Getwd() + if err != nil { + t.Fatal(err) + } + // file1rel is the path relative to the current working directory (where + // the test is running). + file1rel, err := filepath.Rel(wd, file1) + if err != nil { + t.Fatal(err) + } + t.Logf("file1rel: %q", file1rel) + + type args struct { + path1 string + path2 string + } + tests := []struct { + name string + args args + want bool + wantErr bool + }{ + { + "same file", + args{file1, file1}, + true, + false, + }, + { + "same file relative", + args{file1, file1rel}, + true, + false, + }, + { + "different files", + args{file1, file2}, + false, + false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := Same(tt.args.path1, tt.args.path2) + if (err != nil) != tt.wantErr { + t.Errorf("Same() error = %v, wantErr %v", err, tt.wantErr) + return + } + if got != tt.want { + t.Errorf("Same() = %v, want %v", got, tt.want) + } + }) + } +}