Skip to content

Commit

Permalink
Merge pull request #306 from rusq/i288
Browse files Browse the repository at this point in the history
Fix custom cache directory logic
  • Loading branch information
rusq authored Jul 25, 2024
2 parents 0852ccd + d928b16 commit 03d4d73
Show file tree
Hide file tree
Showing 7 changed files with 185 additions and 19 deletions.
65 changes: 48 additions & 17 deletions cmd/slackdump/internal/workspace/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"io"
"os"
"runtime/trace"
"sort"
Expand Down Expand Up @@ -78,8 +79,7 @@ func runList(ctx context.Context, cmd *base.Command, args []string) error {

}

formatter(m, current, entries)
return nil
return formatter(os.Stdout, m, current, entries)
}

const defMark = "=>"
Expand All @@ -94,19 +94,23 @@ var hdrItems = []hdrItem{
{"error", 5},
}

func printAll(m manager, current string, wsps []string) {
ctx, task := trace.NewTask(context.Background(), "printAll")
func printAll(w io.Writer, m manager, current string, wsps []string) error {
ctx, task := trace.NewTask(context.TODO(), "printAll")
defer task.End()

tw := tabwriter.NewWriter(os.Stdout, 2, 8, 1, ' ', 0)
ew := &errWriter{w: w}
tw := tabwriter.NewWriter(ew, 2, 8, 1, ' ', 0)
defer tw.Flush()

fmt.Fprintln(tw, printHeader(hdrItems...))
fmt.Fprintln(tw, makeHeader(hdrItems...))

rows := wspInfo(ctx, m, current, wsps)
for _, row := range rows {
fmt.Fprintln(tw, strings.Join(row, "\t"))
if _, err := fmt.Fprintln(tw, strings.Join(row, "\t")); err != nil {
return err
}
}
return ew.Err()
}

func wspInfo(ctx context.Context, m manager, current string, wsps []string) [][]string {
Expand Down Expand Up @@ -180,7 +184,13 @@ func (h *hdrItem) Underline(char ...string) string {
return strings.Repeat(char[0], h.Size())
}

func printHeader(hi ...hdrItem) string {
// makeHeader creates header, separating columns with tabs and underlining
// them with dashes.
// Example:
//
// C name filename modified team user error
// - ---- -------- -------- ---- ---- -----
func makeHeader(hi ...hdrItem) string {
var sb strings.Builder
for i, h := range hi {
if i > 0 {
Expand All @@ -206,15 +216,34 @@ func userInfo(ctx context.Context, m manager, name string) (*slack.AuthTestRespo
return prov.Test(ctx)
}

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])
type errWriter struct {
w io.Writer
err error
}

func (ew *errWriter) Write(p []byte) (n int, err error) {
if ew.err != nil {
return 0, nil
}
n, ew.err = ew.w.Write(p)
return n, ew.err
}

func (ew *errWriter) Err() error {
return ew.err
}

func printFull(w io.Writer, m manager, current string, wsps []string) error {
ew := &errWriter{w: w}
fmt.Fprintf(ew, "Workspaces in %q:\n\n", cfg.CacheDir())
for _, row := range simpleList(m, current, wsps) {
fmt.Fprintf(ew, "%s (file: %s, last modified: %s)\n", row[0], row[1], row[2])
}
fmt.Printf("\nCurrent workspace is marked with ' %s '.\n", defMark)
fmt.Fprintf(ew, "\nCurrent workspace is marked with ' %s '.\n", defMark)
return ew.Err()
}

func simpleList(_ context.Context, m manager, current string, wsps []string) [][]string {
func simpleList(m manager, current string, wsps []string) [][]string {
var rows = make([][]string, 0, len(wsps))
for _, name := range wsps {
timestamp := "unknown"
Expand All @@ -233,11 +262,13 @@ func simpleList(_ context.Context, m manager, current string, wsps []string) [][
return rows
}

func printBare(_ manager, current string, workspaces []string) {
func printBare(w io.Writer, _ manager, current string, workspaces []string) error {
ew := &errWriter{w: w}
for _, name := range workspaces {
if current == name {
fmt.Print("*")
fmt.Fprint(ew, "*")
}
fmt.Println(name)
fmt.Fprintln(ew, name)
}
return ew.Err()
}
51 changes: 51 additions & 0 deletions cmd/slackdump/internal/workspace/list_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package workspace

import (
"bytes"
"testing"
)

func Test_printBare(t *testing.T) {
type args struct {
_ manager
current string
workspaces []string
}
tests := []struct {
name string
args args
wantW string
wantErr bool
}{
{
"Test 1",
args{
current: "current",
workspaces: []string{"workspace1", "workspace2", "current"},
},
"workspace1\nworkspace2\n*current\n",
false,
},
{
"Test 2",
args{
current: "workspace1",
workspaces: []string{"workspace1", "workspace2", "notcurrent"},
},
"*workspace1\nworkspace2\nnotcurrent\n",
false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
w := &bytes.Buffer{}
if err := printBare(w, nil, tt.args.current, tt.args.workspaces); (err != nil) != tt.wantErr {
t.Errorf("printBare() error = %v, wantErr %v", err, tt.wantErr)
return
}
if gotW := w.String(); gotW != tt.wantW {
t.Errorf("printBare() = %v, want %v", gotW, tt.wantW)
}
})
}
}
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
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 03d4d73

Please sign in to comment.