Skip to content

Commit

Permalink
api cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
rusq committed Mar 24, 2023
1 parent 8249bc9 commit 6f02abb
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 24 deletions.
19 changes: 13 additions & 6 deletions cmd/slackdump/internal/dump/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,14 @@ func RunDump(ctx context.Context, cmd *base.Command, args []string) error {
return fmt.Errorf("file template error: %w", err)
}

sess, err := slackdump.New(ctx, prov, slackdump.WithLogger(dlog.FromContext(ctx)))
fs, err := fsadapter.New(cfg.BaseLocation)
if err != nil {
base.SetExitStatus(base.SApplicationError)
return err
}
defer fs.Close()

sess, err := slackdump.New(ctx, prov, slackdump.WithLogger(dlog.FromContext(ctx)), slackdump.WithFilesystem(fs))
if err != nil {
base.SetExitStatus(base.SApplicationError)
return err
Expand All @@ -113,14 +120,14 @@ func RunDump(ctx context.Context, cmd *base.Command, args []string) error {
dumpFn = dumpv2
}

if err := dumpFn(ctx, sess, list, tmpl); err != nil {
if err := dumpFn(ctx, sess, fs, list, tmpl); err != nil {
base.SetExitStatus(base.SApplicationError)
return err
}
return nil
}

func dumpv3(ctx context.Context, sess *slackdump.Session, list *structures.EntityList, t *nametmpl.Template) error {
func dumpv3(ctx context.Context, sess *slackdump.Session, fs fsadapter.FS, list *structures.EntityList, t *nametmpl.Template) error {
ctx, task := trace.NewTask(ctx, "dumpv3")
defer task.End()

Expand All @@ -141,7 +148,7 @@ func dumpv3(ctx context.Context, sess *slackdump.Session, list *structures.Entit
}
lg.Debugf("using temporary directory: %s", tmpdir)

tf := transform.NewStandard(sess.Filesystem(), transform.WithNameFn(t.Execute))
tf := transform.NewStandard(fs, transform.WithNameFn(t.Execute))
var eg errgroup.Group
for _, link := range p.List.Include {
lg.Printf("fetching %q", link)
Expand Down Expand Up @@ -180,7 +187,7 @@ func convertChunks(ctx context.Context, tf transform.Interface, statefile string
return nil
}

func dumpv2(ctx context.Context, sess *slackdump.Session, list *structures.EntityList, t *nametmpl.Template) error {
func dumpv2(ctx context.Context, sess *slackdump.Session, fs fsadapter.FS, list *structures.EntityList, t *nametmpl.Template) error {
for _, link := range list.Include {
conv, err := sess.Dump(ctx, link, time.Time(cfg.Oldest), time.Time(cfg.Latest))
if err != nil {
Expand All @@ -190,7 +197,7 @@ func dumpv2(ctx context.Context, sess *slackdump.Session, list *structures.Entit
if err != nil {
return err
}
if err := save(ctx, sess.Filesystem(), name, conv); err != nil {
if err := save(ctx, fs, name, conv); err != nil {
return err
}
}
Expand Down
10 changes: 8 additions & 2 deletions cmd/slackdump/internal/export/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"time"

"github.com/rusq/dlog"
"github.com/rusq/fsadapter"

"github.com/rusq/slackdump/v2"
"github.com/rusq/slackdump/v2/auth"
Expand Down Expand Up @@ -65,13 +66,18 @@ func runExport(ctx context.Context, cmd *base.Command, args []string) error {
if err != nil {
return err
}
fsa, err := fsadapter.New(cfg.BaseLocation)
if err != nil {
return err
}

options.List = list
options.Logger = dlog.FromContext(ctx)

var expfn = exportV3
if compat {
return exportV2(ctx, sess, list, options)
expfn = exportV2
}

return exportV3(ctx, sess, list, options)
return expfn(ctx, sess, fsa, list, options)
}
5 changes: 3 additions & 2 deletions cmd/slackdump/internal/export/v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ package export
import (
"context"

"github.com/rusq/fsadapter"
"github.com/rusq/slackdump/v2"
"github.com/rusq/slackdump/v2/export"
"github.com/rusq/slackdump/v2/internal/structures"
)

func exportV2(ctx context.Context, sess *slackdump.Session, list *structures.EntityList, options export.Config) error {
exp := export.New(sess, options)
func exportV2(ctx context.Context, sess *slackdump.Session, fs fsadapter.FS, list *structures.EntityList, options export.Config) error {
exp := export.New(sess, fs, options)
return exp.Run(ctx)
}
3 changes: 2 additions & 1 deletion cmd/slackdump/internal/export/v3.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"sync"

"github.com/rusq/dlog"
"github.com/rusq/fsadapter"
"github.com/rusq/slackdump/v2"
"github.com/rusq/slackdump/v2/cmd/slackdump/internal/export/expproc"
"github.com/rusq/slackdump/v2/export"
Expand All @@ -15,7 +16,7 @@ import (
"github.com/slack-go/slack"
)

func exportV3(ctx context.Context, sess *slackdump.Session, list *structures.EntityList, options export.Config) error {
func exportV3(ctx context.Context, sess *slackdump.Session, fs fsadapter.FS, list *structures.EntityList, options export.Config) error {
lg := dlog.FromContext(ctx)
tmpdir, err := os.MkdirTemp("", "slackdump-*")
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/slackdump/internal/export/wizard.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func wizExport(ctx context.Context, cmd *base.Command, args []string) error {
return err
}
// TODO v3
exp := export.New(sess, options)
exp := export.New(sess, fsa, options)

// run export
return exp.Run(ctx)
Expand Down
7 changes: 6 additions & 1 deletion cmd/slackdump/internal/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ type listFunc func(ctx context.Context, sess *slackdump.Session) (a any, filenam
// list authenticates and creates a slackdump instance, then calls a listFn.
// listFn must return the object from the api, a JSON filename and an error.
func list(ctx context.Context, listFn listFunc) error {
// TODO fix users saving JSON to a text file within archive
if listType == format.CUnknown {
return errors.New("unknown listing format, seek help")
}
Expand Down Expand Up @@ -130,7 +131,11 @@ func list(ctx context.Context, listFn listFunc) error {
}

if !nosave {
if err := saveData(ctx, sess.Filesystem(), data, filename, format.CJSON, users); err != nil {
fsa, err := fsadapter.New(cfg.BaseLocation)
if err != nil {
return err
}
if err := saveData(ctx, fsa, data, filename, format.CJSON, users); err != nil {
return err
}
}
Expand Down
6 changes: 3 additions & 3 deletions export/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,18 @@ type Export struct {

// New creates a new Export instance, that will save export to the
// provided fs.
func New(sd *slackdump.Session, cfg Config) *Export {
func New(sd *slackdump.Session, fs fsadapter.FS, cfg Config) *Export {
if cfg.Logger == nil {
cfg.Logger = logger.Default
}
network.Logger = cfg.Logger

se := &Export{
fs: sd.Filesystem(),
fs: fs,
sd: sd,
lg: cfg.Logger,
opts: cfg,
dl: newFileExporter(cfg.Type, sd.Filesystem(), sd.Client(), cfg.Logger, cfg.ExportToken),
dl: newFileExporter(cfg.Type, fs, sd.Client(), cfg.Logger, cfg.ExportToken),
}
return se
}
Expand Down
5 changes: 3 additions & 2 deletions internal/app/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
type dump struct {
sess *slackdump.Session
cfg config.Params
fs fsadapter.FS

log logger.Interface
}
Expand Down Expand Up @@ -61,7 +62,7 @@ func newDump(ctx context.Context, cfg config.Params, prov auth.Provider, fs fsad
return nil, err
}

return &dump{sess: sess, cfg: cfg, log: cfg.Logger()}, nil
return &dump{sess: sess, cfg: cfg, log: cfg.Logger(), fs: fs}, nil
}

// Dump dumps the input, if dumpfiles is true, it will save the files into a
Expand Down Expand Up @@ -90,7 +91,7 @@ func (app *dump) Dump(ctx context.Context) (int, error) {

total := 0
if err := app.cfg.Input.Producer(func(channelID string) error {
if err := app.dumpOne(ctx, app.sess.Filesystem(), tmpl, channelID, app.sess.Dump); err != nil {
if err := app.dumpOne(ctx, app.fs, tmpl, channelID, app.sess.Dump); err != nil {
app.log.Printf("error processing: %q (conversation will be skipped): %s", channelID, err)
return config.ErrSkip
}
Expand Down
2 changes: 1 addition & 1 deletion internal/app/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func Export(ctx context.Context, cfg config.Params, prov auth.Provider) error {

cfg.Logger().Printf("Export: staring export to: %s", cfg.ExportName)

e := export.New(sess, makeExportOptions(cfg))
e := export.New(sess, fs, makeExportOptions(cfg))
if err := e.Run(ctx); err != nil {
return err
}
Expand Down
5 changes: 0 additions & 5 deletions slackdump.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,11 +162,6 @@ func (s *Session) Client() *slack.Client {
return s.client.(*slack.Client)
}

// Filesystem returns the filesystem adapter used by the session.
func (s *Session) Filesystem() fsadapter.FS {
return s.fs
}

// CurrentUserID returns the user ID of the authenticated user.
func (s *Session) CurrentUserID() string {
return s.wspInfo.UserID
Expand Down

0 comments on commit 6f02abb

Please sign in to comment.