Skip to content

Commit

Permalink
fix workspace list to use custom cache dir
Browse files Browse the repository at this point in the history
  • Loading branch information
rusq committed Jul 16, 2024
1 parent 0852ccd commit 4cb3038
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 3 deletions.
2 changes: 1 addition & 1 deletion cmd/slackdump/internal/workspace/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion cmd/slackdump/internal/workspace/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
5 changes: 4 additions & 1 deletion internal/cache/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
13 changes: 13 additions & 0 deletions internal/osext/dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package osext
import (
"errors"
"os"
"path/filepath"
)

var ErrNotADir = errors.New("not a directory")
Expand All @@ -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
}
68 changes: 68 additions & 0 deletions internal/osext/dir_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
}

0 comments on commit 4cb3038

Please sign in to comment.