Skip to content

Commit

Permalink
correct the file directory name and dedup
Browse files Browse the repository at this point in the history
  • Loading branch information
rusq committed Mar 1, 2023
1 parent 846feb6 commit a6c3aba
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 26 deletions.
8 changes: 7 additions & 1 deletion cmd/slackdump/internal/v1/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,11 @@ func run(ctx context.Context, p params) error {
ctx, task := trace.NewTask(ctx, "main.run")
defer task.End()

provider, err := cache.InitProvider(ctx, p.appCfg.SlackConfig.CacheDir, "", p.creds)
m, err := cache.NewManager(p.appCfg.SlackConfig.CacheDir)
if err != nil {
return err
}
provider, err := m.Auth(ctx, cfg.Workspace, p.creds)
if err != nil {
return err
} else {
Expand Down Expand Up @@ -327,6 +331,8 @@ func parseCmdLine(args []string) (params, error) {
fs.BoolVar(&p.appCfg.ListFlags.Users, "list-users", false, "list users and their IDs. ")
// - export
fs.StringVar(&p.appCfg.ExportName, "export", "", "`name` of the directory or zip file to export the Slack workspace to."+zipHint)
fs.StringVar(&cfg.Workspace, "workspace", "", "name of the Slack workspace to export")

fs.Var(&p.appCfg.ExportType, "export-type", "set the export type: 'standard' or 'mattermost' (default: standard)")
fs.StringVar(&p.appCfg.ExportToken, "export-token", osenv.Secret(envSlackFileToken, ""), "Slack token that will be added to all file URLs, (environment: "+envSlackFileToken+")")
// - emoji
Expand Down
4 changes: 0 additions & 4 deletions internal/app/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ const (
OutputTypeText = "text"
)

const (
FilenameTmplName = "fnt"
)

// ErrSkip should be returned if the [Producer] should skip the channel.
var ErrSkip = errors.New("skip")

Expand Down
19 changes: 6 additions & 13 deletions internal/app/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ import (
"io"
"os"
"runtime/trace"
"strings"
"text/template"
"time"

"github.com/rusq/fsadapter"
"github.com/rusq/slackdump/v2"
"github.com/rusq/slackdump/v2/auth"
"github.com/rusq/slackdump/v2/internal/app/config"
"github.com/rusq/slackdump/v2/internal/app/nametmpl"
"github.com/rusq/slackdump/v2/internal/structures"
"github.com/rusq/slackdump/v2/logger"
"github.com/rusq/slackdump/v2/types"
Expand Down Expand Up @@ -104,17 +104,6 @@ func (app *dump) Dump(ctx context.Context) (int, error) {

type dumpFunc func(context.Context, string, time.Time, time.Time, ...slackdump.ProcessFunc) (*types.Conversation, error)

// renderFilename returns the filename that is rendered according to the
// file naming template.
func renderFilename(tmpl *template.Template, c *types.Conversation) string {
var buf strings.Builder
if err := tmpl.ExecuteTemplate(&buf, config.FilenameTmplName, c); err != nil {
// this should nevar happen
panic(err)
}
return buf.String()
}

// dumpOneChannel dumps just one channel specified by channelInput. If
// generateText is true, it will also generate a ID.txt text file.
func (app *dump) dumpOne(ctx context.Context, fs fsadapter.FS, filetmpl *template.Template, channelInput string, fn dumpFunc) error {
Expand All @@ -123,7 +112,11 @@ func (app *dump) dumpOne(ctx context.Context, fs fsadapter.FS, filetmpl *templat
return err
}

return app.writeFiles(fs, renderFilename(filetmpl, cnv), cnv)
filename, err := nametmpl.ExecuteTemplate(filetmpl, cnv)
if err != nil {
return err
}
return app.writeFiles(fs, filename, cnv)
}

// writeFiles writes the conversation to disk. If text output is set, it will
Expand Down
8 changes: 8 additions & 0 deletions internal/app/nametmpl/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,11 @@ func Compile(t string) (*template.Template, error) {
}
return tmpl, nil
}

func ExecuteTemplate(tmpl *template.Template, c *types.Conversation) (string, error) {
var buf strings.Builder
if err := tmpl.ExecuteTemplate(&buf, filenameTmplName, c); err != nil {
return "", err
}
return buf.String(), nil
}
11 changes: 11 additions & 0 deletions internal/osext/osext.go
Original file line number Diff line number Diff line change
@@ -1,2 +1,13 @@
// Package osext provides some extended functionality for the os package.
package osext

import "fmt"

type Error struct {
File string
Err error
}

func (e *Error) Error() string {
return fmt.Sprintf("%w: %s", e.Err, e.File)
}
21 changes: 17 additions & 4 deletions internal/transform/standard.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package transform

import (
"context"
"encoding/json"
"fmt"
"path/filepath"
"runtime/trace"

"github.com/rusq/dlog"
"github.com/rusq/fsadapter"
Expand All @@ -19,18 +21,22 @@ type Standard struct {
fs fsadapter.FS
nameFn func(*types.Conversation) string
updateFileLink bool
seenFiles map[string]struct{}
}

// NewStandard returns a new Standard transformer, nameFn should return the
// filename for a given conversation. This is the name that the conversation
// will be written to the filesystem.
func NewStandard(fs fsadapter.FS, nameFn func(*types.Conversation) string) *Standard {
return &Standard{fs: fs, nameFn: nameFn, updateFileLink: true}
return &Standard{fs: fs, nameFn: nameFn, updateFileLink: true, seenFiles: make(map[string]struct{})}
}

func (s *Standard) Transform(st *state.State, basePath string) error {
func (s *Standard) Transform(ctx context.Context, st *state.State, basePath string) error {
ctx, task := trace.NewTask(ctx, "transform.Standard.Transform")
defer task.End()

if st == nil {
return fmt.Errorf("nil state")
return fmt.Errorf("fatal: nil state")
}
rsc, err := st.OpenChunks(basePath)
if err != nil {
Expand All @@ -45,7 +51,9 @@ func (s *Standard) Transform(st *state.State, basePath string) error {

allCh := pl.AllChannels()
for _, ch := range allCh {
rgn := trace.StartRegion(ctx, "transform.Standard.Transform: "+ch)
conv, err := s.conversation(pl, st, basePath, ch)
rgn.End()
if err != nil {
return err
}
Expand Down Expand Up @@ -105,8 +113,13 @@ func (s *Standard) transferFiles(st *state.State, basePath string, mm []types.Me
if fp == "" {
return fmt.Errorf("unable to generate the filename for: %v", mm[i].Files[j])
}
if _, ok := s.seenFiles[fp]; ok {
continue
} else {
s.seenFiles[fp] = struct{}{}
}
srcPath := filepath.Join(basePath, fp)
fsTrgPath := filepath.Join(ch, "attachments", filepath.Base(srcPath))
fsTrgPath := filepath.Join(ch, filepath.Base(srcPath))
if err := osext.MoveFile(srcPath, s.fs, fsTrgPath); err != nil {
dlog.Printf("file missing: %q", srcPath)
return fmt.Errorf("error moving %q to %q", srcPath, fsTrgPath)
Expand Down
6 changes: 5 additions & 1 deletion internal/transform/standard_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package transform

import (
"context"
"path/filepath"
"runtime/trace"
"testing"

"github.com/rusq/fsadapter"
Expand All @@ -12,6 +14,8 @@ import (
const whereTheTempIsAt = "../../tmp"

func TestStandard_Transform(t *testing.T) {
ctx, task := trace.NewTask(context.Background(), "TestStandard_Transform")
defer task.End()
// MANUAL
fs := fsadapter.NewDirectory(filepath.Join(whereTheTempIsAt, "manual"))
s := NewStandard(fs, func(c *types.Conversation) string {
Expand All @@ -21,7 +25,7 @@ func TestStandard_Transform(t *testing.T) {
if err != nil {
t.Fatalf("state.Load(): %s", err)
}
if err := s.Transform(st, whereTheTempIsAt); err != nil {
if err := s.Transform(ctx, st, whereTheTempIsAt); err != nil {
t.Fatal(err)
}
}
7 changes: 4 additions & 3 deletions tmp/Makefile
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
SHELL=/bin/sh

FILES=$(wildcard *.json.gz) $(wildcard *.state) $(wildcard C*) $(wildcard D*)
FILES=$(wildcard *.json.gz) $(wildcard *.state) $(wildcard C*) $(wildcard D*) manual

restore:
restore: $(FILES)
7z x backup/backup.7z

.PHONY: restore

clean: $(FILES)
-rm -rf $^
.PHONY: clean

backup: $(FILES)
7z a backup/backup.7z $^

0 comments on commit a6c3aba

Please sign in to comment.